File tree Expand file tree Collapse file tree 4 files changed +49
-6
lines changed Expand file tree Collapse file tree 4 files changed +49
-6
lines changed Original file line number Diff line number Diff line change 1
1
from collections import Counter
2
- from utils import load_input
2
+ from utils import load_input_lines
3
3
4
- location_ids = load_input ('01' )
4
+ location_ids = load_input_lines ('01' )
5
5
6
6
def puzzle_one () -> int :
7
7
left , right = [], []
Original file line number Diff line number Diff line change 1
- from utils import load_input
1
+ from utils import load_input_lines
2
2
3
- reports = load_input ('02' )
3
+ reports = load_input_lines ('02' )
4
4
5
5
6
6
def is_safe (a : int , b : int , is_increasing : bool ) -> bool :
Original file line number Diff line number Diff line change
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 ())
Original file line number Diff line number Diff line change 2
2
3
3
TOP = dirname (abspath (__file__ ))
4
4
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 :
6
12
filepath = join (TOP , puzzle_number , 'input.txt' )
7
13
with open (filepath ) as file :
8
- return file .read (). splitlines ()
14
+ return file .read ()
You can’t perform that action at this time.
0 commit comments