day2 part 1 only
This commit is contained in:
parent
0da4e5acf9
commit
acb70df93e
1000
2024/full/day02.txt
Normal file
1000
2024/full/day02.txt
Normal file
File diff suppressed because it is too large
Load Diff
49
2024/python/day02.py
Normal file
49
2024/python/day02.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import matrix
|
||||||
|
import shared
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
|
||||||
|
def is_increasing(sequence):
|
||||||
|
return all(sequence[i] < sequence[i+1] for i in range(len(sequence) - 1))
|
||||||
|
|
||||||
|
def is_decreasing(sequence):
|
||||||
|
return all(sequence[i] > sequence[i+1] for i in range(len(sequence) - 1))
|
||||||
|
|
||||||
|
def parse_line(row):
|
||||||
|
if not(is_increasing(row) or is_decreasing(row)):
|
||||||
|
return 0
|
||||||
|
for previous, current in zip(row, row[1:]):
|
||||||
|
diff = abs(current - previous)
|
||||||
|
if diff >= 1 and diff <= 3:
|
||||||
|
continue
|
||||||
|
return 0
|
||||||
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
# @shared.profile
|
||||||
|
def part1(rows):
|
||||||
|
rows = [list(map(int, row.split())) for row in rows]
|
||||||
|
safe = 0
|
||||||
|
for row in rows:
|
||||||
|
safe += parse_line(row)
|
||||||
|
print(safe)
|
||||||
|
|
||||||
|
# @shared.profile
|
||||||
|
def part2(rows):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
rows = [row for row in shared.load_rows(2)]
|
||||||
|
with shared.elapsed_timer() as elapsed:
|
||||||
|
part1(rows)
|
||||||
|
print("🕒", elapsed())
|
||||||
|
|
||||||
|
rows = [row for row in shared.load_rows(2, True)]
|
||||||
|
with shared.elapsed_timer() as elapsed:
|
||||||
|
part2(rows)
|
||||||
|
print("🕒", elapsed())
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
6
2024/samples/day02.txt
Normal file
6
2024/samples/day02.txt
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
|
Loading…
Reference in New Issue
Block a user