blob: 864cb906da88ad5ad5910e4893f227b9b412ea1f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#advent of code 2025
#day 06
import re
def calculate(values,op):
if op=="+":
answer=sum(values);
elif op=="*":
answer=1;
for value in values:
answer*=value;
return answer;
part1=0;
part2=0;
PuzzleInput=open("06.in","r");
problems=dict();
biggerproblems=dict();
operators=[];
for y,line in enumerate(PuzzleInput):
line=line.replace("\n","");
vals=re.findall(r'(\d+|\*|\+)', line);
if y==0: #reserve space for each column
for n in range(len(vals)):
problems[n]=[];
for n in range(len(line)):
biggerproblems[n]=[];
for x,val in enumerate(vals):
problems[x].append(val);
if vals.count("+")+vals.count("*")==0:
for x,c in enumerate(line):
biggerproblems[x].append(c);
else:
operators=[op for op in vals];
PuzzleInput.close();
#part 1
for n in problems:
problems[n]=[[int(num) for num in problems[n][:-1]],problems[n][-1]];
part1+=calculate(*problems[n]);
#part 2
subproblem=[];
for n in biggerproblems:
problemline=biggerproblems[n];
if problemline.count(" ")==len(problemline):
operation=operators.pop(0);
part2+=calculate(subproblem,operation);
subproblem=[];
continue;
subproblem.append(int("".join(problemline)));
#last column doesn't have it's own whitespace so it's outside the loop
operation=operators.pop(0);
part2+=calculate(subproblem,operation);
print("part 1",part1);
print("part 2",part2);
|