summaryrefslogtreecommitdiff
path: root/2024/aoc2024-d02.py
blob: 655fdc121cc112c562a04c70780c318cf2b11e38 (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
#advent of code 2024
#day 02
part1 = 0;
ToCheck = [];

f = open("02.in","r");
for l in f:
	levels = [int(lvl) for lvl in l.split()];
	if (levels[1]-levels[0]) < 0:
		levels.reverse();
	dMin = 4;
	dMax = 0;
	for i in range(1,len(levels)):
		d = levels[i] - levels[i-1];
		dMin = min(dMin,d);
		dMax = max(dMax,d);
	if dMax <=3 and dMin >= 1:
		part1 += 1;
	else:
		ToCheck.append(levels);
f.close();

correction = 0;
for levels in ToCheck:
	isOK = False;
	for x in range(len(levels)):
		NewLevels = [levels[y] for y in range(len(levels)) if y !=x]
		for _ in ["n","r"]: #normal and reversed, reverse and the end of first iteration
			dMin = 4;
			dMax = 0;	
			for i in range(1,len(NewLevels)):
				d = NewLevels[i] - NewLevels[i-1];
				dMin = min(dMin,d);
				dMax = max(dMax,d);
			if dMax <=3 and dMin >= 1: isOK = True;
			NewLevels.reverse();
	if isOK: correction += 1;

part2 = part1 + correction;
print("part 1 = ", part1);
print("part 2 = ", part2);