From 8acee0934c6a9977436578ec0206a94554bfab11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20M=C3=A5rdbrink?= Date: Mon, 6 Oct 2025 23:35:38 +0200 Subject: [PATCH] Day 12 --- d12/main.odin | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 d12/main.odin diff --git a/d12/main.odin b/d12/main.odin new file mode 100644 index 0000000..99ad457 --- /dev/null +++ b/d12/main.odin @@ -0,0 +1,151 @@ +package main + +import "core:unicode/utf8" +import "core:math" +import "core:slice" +import "core:strconv" +import "core:strings" +import "core:bytes" +import "core:fmt" +import "core:os" + +import "../util" + +Type :: enum { + East, + North, + West, + South, + + Left, + Right, + + Forward +} + +Instruction :: struct { + type: Type, + units: int, +} + +part_1 :: proc(lines: []string) { + result := 0 + + instructions := slice.mapper(lines, proc(line: string) -> (instruction: Instruction) { + switch line[0] { + case 'N': instruction.type = .North + case 'S': instruction.type = .South + case 'E': instruction.type = .East + case 'W': instruction.type = .West + case 'L': instruction.type = .Left + case 'R': instruction.type = .Right + case 'F': instruction.type = .Forward + } + + ok: bool + instruction.units, ok = strconv.parse_int(line[1:]); assert(ok) + return instruction + }) + + direction := Type.East + location := [2]int{} + for instruction in instructions { + switch instruction.type { + case .North: location.y += instruction.units + case .South: location.y -= instruction.units + + case .East: location.x += instruction.units + case .West: location.x -= instruction.units + + case .Left: + turn_units := instruction.units / 90 + direction_int := (int(direction) + turn_units) % 4 + direction = Type(direction_int) + case .Right: + turn_units := instruction.units / 90 + direction_int := (int(direction) - turn_units + 4) % 4 + direction = Type(direction_int) + + case .Forward: + #partial switch direction { + case .North: location.y += instruction.units + case .South: location.y -= instruction.units + case .East: location.x += instruction.units + case .West: location.x -= instruction.units + } + } + } + + result = abs(location.x) + abs(location.y) + fmt.printfln("Part 1: %d", result) +} + +part_2 :: proc(lines: []string) { + result := 0 + + instructions := slice.mapper(lines, proc(line: string) -> (instruction: Instruction) { + switch line[0] { + case 'N': instruction.type = .North + case 'S': instruction.type = .South + case 'E': instruction.type = .East + case 'W': instruction.type = .West + case 'L': instruction.type = .Left + case 'R': instruction.type = .Right + case 'F': instruction.type = .Forward + } + + ok: bool + instruction.units, ok = strconv.parse_int(line[1:]); assert(ok) + return instruction + }) + + ship_location := [2]int{ 0, 0 } + waypoint_location := [2]int{ 10, 1 } + for instruction in instructions { + switch instruction.type { + case .North: waypoint_location.y += instruction.units + case .South: waypoint_location.y -= instruction.units + + case .East: waypoint_location.x += instruction.units + case .West: waypoint_location.x -= instruction.units + + case .Left: + turns := instruction.units / 90 + for _ in 0..