Created
December 1, 2025 11:41
-
-
Save cstrachan88/6bca6cad352352d758f992c9513a2207 to your computer and use it in GitHub Desktop.
Advent of Code 2025 - Day 01 in Odin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package day01 | |
| import "core:fmt" | |
| import os "core:os/os2" | |
| import "core:strconv" | |
| import "core:strings" | |
| import "core:unicode/utf8" | |
| // 0 - 99 discrete | |
| // input contains sequence of rotations, one per line | |
| // L/R (left = lower), then distance which is number of clicks | |
| // 11 + R8 = 19 | |
| // 19 + L19 = 0 | |
| // 0 + L1 = 99 | |
| // dial start = 50 | |
| // --- Part 1: | |
| // Actual password is the number of times the dial is left pointing at 0 after any rotation in the sequence | |
| // --- Part 2: | |
| // actually supposed to count the number of times any click causes the dial to point at 0 | |
| test_input := ` | |
| L68 | |
| L30 | |
| R48 | |
| L5 | |
| R60 | |
| L55 | |
| L1 | |
| L99 | |
| R14 | |
| L82 | |
| ` | |
| // test_input := ` | |
| // L50 | |
| // L100 | |
| // R400 | |
| // ` | |
| main :: proc() { | |
| input_txt, err := os.read_entire_file_from_path("day01.txt", context.allocator) | |
| assert(err == nil) | |
| // input := test_input | |
| input := string(input_txt) | |
| trimmed := strings.trim_space(input) | |
| lines := strings.split_lines(trimmed) | |
| // part1_test(lines) | |
| // fmt.printfln("Part 1 result = %d", part1(lines)) | |
| // part2_test(lines) | |
| fmt.printfln("Part 2 result = %d", part2(lines)) | |
| } | |
| rotation_to_num :: proc(rotation: string) -> int { | |
| result: int = 0 | |
| sign: int = rotation[0] == 'L' ? -1 : 1 | |
| for i := 1; i < len(rotation); i += 1 { | |
| digit := rotation[i] - '0' | |
| result = result * 10 + int(digit) | |
| } | |
| return result * sign | |
| } | |
| part1_test :: proc(lines: []string) { | |
| start: int = 50 | |
| was_zero_count: int = 0 | |
| for line in lines { | |
| fmt.printf("%d + %s = ", start, line) | |
| start += rotation_to_num(line) | |
| for (start < 0) {start += 100} | |
| for (start > 99) {start -= 100} | |
| if start == 0 {was_zero_count += 1} | |
| fmt.printfln("%d", start) | |
| } | |
| fmt.printfln("Was zero count = %d", was_zero_count) | |
| } | |
| part1 :: proc(lines: []string) -> int { | |
| start: int = 50 | |
| was_zero_count: int = 0 | |
| for line in lines { | |
| start += rotation_to_num(line) | |
| for (start < 0) {start += 100} | |
| for (start > 99) {start -= 100} | |
| if start == 0 {was_zero_count += 1} | |
| } | |
| return was_zero_count | |
| } | |
| rotation_to_signed_val :: proc(rotation: string) -> (sign: int, val: int) { | |
| result: int = 0 | |
| sign = rotation[0] == 'L' ? -1 : 1 | |
| for i := 1; i < len(rotation); i += 1 { | |
| digit := rotation[i] - '0' | |
| result = result * 10 + int(digit) | |
| } | |
| return sign, result | |
| } | |
| part2_test :: proc(lines: []string) { | |
| start: int = 50 | |
| touched_zero_count: int = 0 | |
| // Two conditions | |
| // - pass 0 X number of times where X is 0.. | |
| // - land on 0 | |
| for line in lines { | |
| // Needed sign to track direction | |
| sign, val := rotation_to_signed_val(line) | |
| prev := start | |
| result := start + sign * val | |
| defer start = result | |
| // These are total rotations - no need to force result to be in 0-99 range | |
| curr_rotations := result / 100 | |
| prev_rotations := prev / 100 | |
| touched_zero_count += (curr_rotations - prev_rotations) * sign | |
| is_zero := result % 100 == 0 ? 1 : 0 | |
| was_zero := prev % 100 == 0 ? 1 : 0 | |
| if sign * result <= 0 { | |
| // Mult result by sign to get direction of travel | |
| // We touched zero in the direction of movement | |
| touched_zero_count += is_zero - was_zero | |
| } else if prev * result < 0 { | |
| // If sign is different, we know we passed through zero (mult quickly checks sign change) | |
| // Discount 1 if we started on zero | |
| touched_zero_count += 1 - was_zero | |
| } | |
| fmt.printfln( | |
| "%d + %s = %d (zero = %d) %d %d %d %d", | |
| prev, | |
| line, | |
| result, | |
| touched_zero_count, | |
| curr_rotations, | |
| prev_rotations, | |
| is_zero, | |
| was_zero, | |
| ) | |
| } | |
| fmt.printfln("Touched zero count = %d", touched_zero_count) | |
| } | |
| part2 :: proc(lines: []string) -> int { | |
| start: int = 50 | |
| touched_zero_count: int = 0 | |
| for line in lines { | |
| sign, val := rotation_to_signed_val(line) | |
| prev := start | |
| start += sign * val | |
| touched_zero_count += (start / 100 - prev / 100) * sign | |
| is_zero := start % 100 == 0 ? 1 : 0 | |
| was_zero := prev % 100 == 0 ? 1 : 0 | |
| if sign * start <= 0 { | |
| touched_zero_count += is_zero - was_zero | |
| } else if prev * start < 0 { | |
| touched_zero_count += 1 - was_zero | |
| } | |
| } | |
| return touched_zero_count | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment