Skip to content

Commit

Permalink
Day 3! This one was irritating because I was using the wrong test inp…
Browse files Browse the repository at this point in the history
…ut on b for ages
  • Loading branch information
m4xic committed Dec 3, 2024
1 parent 534bf34 commit ef3fa3c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
13 changes: 13 additions & 0 deletions 03/03a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import re

with open('input', 'r') as f: instructions = f.read()

results = re.findall(r"mul\([0-9]{1,3},[0-9]{1,3}\)", instructions)

total = 0
for result in results:
a, b = result.replace("mul(","").replace(")","").split(",")
print(f"{result=} {a=} {b=} {int(a) * int(b)=}")
total += int(a) * int(b)

print(total)
40 changes: 40 additions & 0 deletions 03/03b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import re

with open('input', 'r') as f: instructions = f.read()

total = 0

do = True
print(instructions)
while instructions:
#print(total)
if do:
# First check there's not a dont() coming up
print(instructions[:7])
if instructions[:7] == "don't()":
do = False
continue
# Then check if there's a valid mul(xxx,yyy) coming up
else:
match = re.search(r"mul\([0-9]{1,3},[0-9]{1,3}\)", instructions[:12])
if match:
#print(match.group())
a, b = match.group().replace("mul(","").replace(")","").split(",")
total += int(a) * int(b)
#print(match.span())
instructions = instructions[match.span()[1]:]
continue
else:
instructions = instructions[1:]
continue

else:
# Check if there's a do() coming up
print(instructions[:4])
if instructions[:4] == "do()":
do = True
continue
else:
instructions = instructions[1:]

print(total)

0 comments on commit ef3fa3c

Please sign in to comment.