Solve 2024:2 p1-2 "Red-Nosed Reports"
DDOS of adventofcode.com, did not get to open the puzzle until 06:05 (five minutes local time). Screwed up the zip() and lost valuable minutes by trying to replace it with itertools, only to find I have made a typo (did AB CD EF instead of AB BC CD). I also lost valuable minutes by tring to solve p1 with one nested loop. Stupid. For part 2, I created the issafe() helper to be able to remove items from reports and test, one at the time.
This commit is contained in:
parent
81741232af
commit
5deb351504
1 changed files with 34 additions and 0 deletions
34
2024-python/output/day_02.py
Normal file
34
2024-python/output/day_02.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
from output import ints
|
||||
|
||||
|
||||
def solve(data):
|
||||
reports = [ints(line) for line in data.splitlines()]
|
||||
p1 = 0
|
||||
p2 = 0
|
||||
|
||||
for report in reports:
|
||||
if issafe(report):
|
||||
p1 += 1
|
||||
|
||||
for i in range(len(report)):
|
||||
if issafe(report[:i] + report[i + 1 :]):
|
||||
p2 += 1
|
||||
break
|
||||
|
||||
return p1, p2
|
||||
|
||||
|
||||
def issafe(report):
|
||||
diffs = [a - b for a, b in zip(report, report[1:])]
|
||||
|
||||
return all(-3 <= d <= -1 for d in diffs) or all(1 <= d <= 3 for d in diffs)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("./input/02.txt", "r") as f:
|
||||
inp = f.read().strip()
|
||||
|
||||
p1, p2 = solve(inp)
|
||||
|
||||
print(p1)
|
||||
print(p2)
|
||||
Loading…
Add table
Reference in a new issue