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.
34 lines
669 B
Python
34 lines
669 B
Python
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)
|