From eaedf6b76bf7bc6bb564a3c2a650f1fe4a438281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20M=C3=A5rdbrink?= Date: Wed, 1 Oct 2025 12:09:02 +0200 Subject: [PATCH] Day 2 --- d02/main.odin | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 d02/main.odin diff --git a/d02/main.odin b/d02/main.odin new file mode 100644 index 0000000..2d25540 --- /dev/null +++ b/d02/main.odin @@ -0,0 +1,73 @@ +package main + +import "core:math" +import "core:slice" +import "core:strconv" +import "core:strings" +import "core:bytes" +import "core:fmt" +import "core:os" + +import "../util" + +part_1 :: proc(lines: []string) { + result := 0 + + for line in lines { + line_parts := strings.split(line, " ") + + password := line_parts[2] + policy_letter := strings.trim_suffix(line_parts[1], ":") + + range_parts := strings.split(line_parts[0], "-") + policy_min, _ := strconv.parse_int(range_parts[0]); + policy_max, _ := strconv.parse_int(range_parts[1]); + + password_policy_count := strings.count(password, policy_letter) + + if password_policy_count <= policy_max && password_policy_count >= policy_min { + result += 1 + } + } + + fmt.printfln("Part 1: %d", result) +} + +part_2 :: proc(lines: []string) { + result := 0 + + for line in lines { + line_parts := strings.split(line, " ") + + password := line_parts[2] + policy_letter := strings.trim_suffix(line_parts[1], ":") + + range_parts := strings.split(line_parts[0], "-") + policy_min, _ := strconv.parse_int(range_parts[0]); + policy_max, _ := strconv.parse_int(range_parts[1]); + + password_policy_count := strings.count(password, policy_letter) + + fst_hit := password[policy_min-1] == policy_letter[0] + snd_hit := password[policy_max-1] == policy_letter[0] + + if fst_hit ~ snd_hit { + result += 1 + } + } + + fmt.printfln("Part 2: %d", result) +} + +main :: proc() { + context.allocator = context.temp_allocator + + INPUT :: #load("input.txt", string) + lines := strings.split(INPUT, "\n") + lines = lines[:len(lines)-1] + + part_1(lines) + part_2(lines) + + free_all(context.temp_allocator) +}