mirror of
https://github.com/cupcakearmy/advent-of-code.git
synced 2025-09-05 23:00:38 +00:00
2024
This commit is contained in:
10
2024/03/findings.md
Normal file
10
2024/03/findings.md
Normal 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
49
2024/03/main.py
Normal 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)
|
Reference in New Issue
Block a user