This commit is contained in:
2024-12-08 21:36:04 +01:00
parent 9ef0275a61
commit 0ba1f371ed
18 changed files with 1016 additions and 115 deletions

10
2024/03/findings.md Normal file
View File

@@ -0,0 +1,10 @@
# Day 3
Of course 1 min of regex freestyle (`mul\(\d+,\d+\)`) and it would have been done, i wanted to do it on my own.
NVM did it with Regex xD
Part 2 was fun, I basically computed an array of the `do()` and `don't()` index and simply looked up which one was further up.
## Other ways
I think a way more efficient way to go about it would to take more of a scan approach, where we keep track of the enabled/disabled status and parse as we go.

49
2024/03/main.py Normal file
View File

@@ -0,0 +1,49 @@
import re
re_mul = re.compile(r"mul\(\d+,\d+\)")
re_enable = re.compile(r"do\(\)")
re_disable = re.compile(r"don't\(\)")
def solve(raw: str) -> int:
# Part 1
part1 = 0
part2 = 0
enable = [m.end() for m in re_enable.finditer(raw)]
disable = [m.end() for m in re_disable.finditer(raw)]
for match in re_mul.finditer(raw):
# Part 1
[left, right] = match.group()[4:-1].split(",")
result = int(left) * int(right)
part1 += result
# Part 2
s = match.start()
last_enable = 0
for x in enable:
if x > s:
break
last_enable = x
last_disable = 0
for x in disable:
if x > s:
break
last_disable = x
disabled = last_disable != 0 and last_disable > last_enable
if disabled:
continue
part2 += result
return (part1, part2)
# Test
with open("./2024/03/test.txt", "r") as f:
result = solve(f.read().strip())
print(result)
# Input
with open("./2024/03/input.txt", "r") as f:
result = solve(f.read().strip())
print(result)