From 1675cf97f328abb86151c9562ea1e521be3cd1c7 Mon Sep 17 00:00:00 2001 From: JalonSolov Date: Wed, 11 Dec 2024 13:51:11 -0500 Subject: [PATCH] 2024 Day 3 --- 2024/03/JalonSolov.v | 40 ++++++++++++++++++++++++++++++++++++ known/2024/03/JalonSolov.out | 2 ++ 2 files changed, 42 insertions(+) create mode 100644 2024/03/JalonSolov.v create mode 100644 known/2024/03/JalonSolov.out diff --git a/2024/03/JalonSolov.v b/2024/03/JalonSolov.v new file mode 100644 index 0000000..7f75620 --- /dev/null +++ b/2024/03/JalonSolov.v @@ -0,0 +1,40 @@ +import regex +import os + +instructions := os.read_file('instructions-part2.input')! + +mut mul_re := regex.regex_opt(r"(mul\(\d{1,3},\d{1,3}\))|(do(n't)?\(\))")! +mut mul_total := 0 +mut enabled_total := 0 + +mut enabled := true + +mut index := 0 + +for index < instructions.len { + start, end := mul_re.find_from(instructions, index) + if start >= 0 { + group := instructions[start..end] + match group[0] { + `m` { + result := group[4..group.index(',')?].int() * group[group.index(',')? + 1..].int() + mul_total += result + if enabled { + enabled_total += result + } + } + `d` { + enabled = (group == 'do()') + } + else { + // should never happen + } + } + index = end + } else { + break + } +} + +println('Part 1: ${mul_total}') +println('Part 2: ${enabled_total}') diff --git a/known/2024/03/JalonSolov.out b/known/2024/03/JalonSolov.out new file mode 100644 index 0000000..0fa4903 --- /dev/null +++ b/known/2024/03/JalonSolov.out @@ -0,0 +1,2 @@ +Part 1: 161 +Part 2: 48