diff options
Diffstat (limited to '2024/aoc2024-d02.py')
-rw-r--r-- | 2024/aoc2024-d02.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/2024/aoc2024-d02.py b/2024/aoc2024-d02.py new file mode 100644 index 0000000..655fdc1 --- /dev/null +++ b/2024/aoc2024-d02.py @@ -0,0 +1,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); |