first 3 days
This commit is contained in:
24
2/1.py
Normal file
24
2/1.py
Normal file
@@ -0,0 +1,24 @@
|
||||
def safe(levels: list[int]) -> bool:
|
||||
i = 0
|
||||
# figure out the diffs
|
||||
diffs = []
|
||||
while i < len(levels) - 1:
|
||||
curr, nxt = levels[i], levels[i+1]
|
||||
diffs.append(curr - nxt)
|
||||
i += 1
|
||||
if 0 in diffs:
|
||||
return False
|
||||
neg = len(list(filter(lambda d: d < 0, diffs)))
|
||||
pos = len(list(filter(lambda d: d > 0, diffs)))
|
||||
big = len(list(filter(lambda d: abs(d) > 3, diffs)))
|
||||
if big > 0:
|
||||
return False
|
||||
return (neg == 0 and pos == len(diffs)) or (neg == len(diffs) and pos == 0)
|
||||
|
||||
|
||||
|
||||
with open('1.input') as file:
|
||||
reports = [[int(i) for i in line.split()] for line in file]
|
||||
|
||||
|
||||
print(sum([1 if r else 0 for r in [safe(report) for report in reports]]))
|
||||
30
2/2.py
Normal file
30
2/2.py
Normal file
@@ -0,0 +1,30 @@
|
||||
def error_count(levels: list[int]) -> bool:
|
||||
i = 0
|
||||
# figure out the diffs
|
||||
diffs = []
|
||||
while i < len(levels) - 1:
|
||||
curr, nxt = levels[i], levels[i+1]
|
||||
diffs.append(curr - nxt)
|
||||
i += 1
|
||||
neg = len(list(filter(lambda d: d < 0, diffs)))
|
||||
pos = len(list(filter(lambda d: d > 0, diffs)))
|
||||
big = len(list(filter(lambda d: abs(d) > 3, diffs)))
|
||||
zer = len(list(filter(lambda d: d == 0, diffs)))
|
||||
errors = big + zer
|
||||
errors += int((neg == 0 and pos == len(diffs))) + int((neg == len(diffs) and pos == 0))
|
||||
return errors
|
||||
|
||||
|
||||
|
||||
with open('data.input') as file:
|
||||
reports = [[int(i) for i in line.split()] for line in file]
|
||||
|
||||
|
||||
|
||||
# 0 or 1 is now tolerable
|
||||
safe = 0
|
||||
for report in reports:
|
||||
# print(error_count(report))
|
||||
safe += int(error_count(report) <= 1)
|
||||
else:
|
||||
print('safe', safe)
|
||||
1000
2/data.input
Normal file
1000
2/data.input
Normal file
File diff suppressed because it is too large
Load Diff
1000
2/new.input
Normal file
1000
2/new.input
Normal file
File diff suppressed because it is too large
Load Diff
6
2/sample.input
Normal file
6
2/sample.input
Normal file
@@ -0,0 +1,6 @@
|
||||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9
|
||||
Reference in New Issue
Block a user