From b96161d57f868e32792b8af631fa76edd89b543c Mon Sep 17 00:00:00 2001 From: blenovo Date: Sun, 15 Dec 2024 21:35:41 +0100 Subject: finally restored git access so adding 2024, days 01-15 --- 2024/aoc2024-d02.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 2024/aoc2024-d02.py (limited to '2024/aoc2024-d02.py') 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); -- cgit v1.2.3