summaryrefslogtreecommitdiff
path: root/2025/aoc2025-d05.py
blob: 5b9c21ca075e35b69e602ce5babbf73f6f132e3e (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
#advent of code 2025
#day 05
#p1 elementary school tier iterate over list
#p2 collapse ranges from input: trims it down by ~50%, that's why part 2 runs first
#lo1 looks a lot like lol
part1=0;
part2=0;
PuzzleInput=open("05.in","r").read().split("\n\n");
fresh=[ [int(num) for num in nums.split("-") ] for nums in PuzzleInput[0].split("\n")];
ingredients=[int(food) for food in PuzzleInput[1].split("\n")];
#part 2
changes=True;
while changes:
	fresh=sorted(fresh);
	changes=False;
	for n1,range1 in enumerate(fresh):
		lo1,hi1=range1;
		for n2,range2 in enumerate(fresh):
			if n1==n2: continue;
			lo2,hi2=range2;
			if lo1 in range(lo2,hi2+1) or hi1 in range(lo2,hi2+1):
				fresh[n1]=[min(lo1,lo2),max(hi1,hi2)];
				fresh.pop(n2);
				changes=True;
				break;
		if changes: break;
part2=sum([ranHi+1-ranLo for ranLo,ranHi in fresh]);
#part 1 
for ingr in ingredients:
	for lo,hi in fresh:
		if ingr in range(lo,hi+1):
			part1+=1;
			break;

print("part 1",part1);
print("part 2",part2);