Skip to content

Commit 99ea07a

Browse files
committed
Solution to day three, parts one and two
1 parent d8945dd commit 99ea07a

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

01/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from collections import Counter
2-
from utils import load_input
2+
from utils import load_input_lines
33

4-
location_ids = load_input('01')
4+
location_ids = load_input_lines('01')
55

66
def puzzle_one() -> int:
77
left, right = [], []

02/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from utils import load_input
1+
from utils import load_input_lines
22

3-
reports = load_input('02')
3+
reports = load_input_lines('02')
44

55

66
def is_safe(a: int, b: int, is_increasing: bool) -> bool:

03/main.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import re
2+
from utils import load_input_full
3+
4+
input_program = load_input_full('03')
5+
6+
7+
def puzzle_one(program: str) -> int:
8+
groups = re.findall(r"mul\((\d+),(\d+)\)", program)
9+
return sum(int(group[0]) * int(group[1]) for group in groups)
10+
11+
def find_conditional(conditional: str, start: int) -> int:
12+
try:
13+
return input_program.index(conditional, start)
14+
except ValueError:
15+
return len(input_program)
16+
17+
def puzzle_two() -> int:
18+
enabled = True
19+
start = 0
20+
res = 0
21+
22+
while start < len(input_program):
23+
24+
if enabled:
25+
end = find_conditional("don't()", start)
26+
res += puzzle_one(input_program[start:end])
27+
start = end + len("don't()")
28+
enabled = False
29+
else:
30+
start = find_conditional("do()", start) + len("do()")
31+
enabled = True
32+
33+
return res
34+
35+
if __name__ == "__main__":
36+
print(puzzle_one(program=input_program))
37+
print(puzzle_two())

utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
TOP = dirname(abspath(__file__))
44

5-
def load_input(puzzle_number: str) -> list[str]:
5+
6+
def load_input_lines(puzzle_number: str) -> list[str]:
7+
filepath = join(TOP, puzzle_number, 'input.txt')
8+
with open(filepath) as file:
9+
return file.read().splitlines()
10+
11+
def load_input_full(puzzle_number: str) -> str:
612
filepath = join(TOP, puzzle_number, 'input.txt')
713
with open(filepath) as file:
8-
return file.read().splitlines()
14+
return file.read()

0 commit comments

Comments
 (0)