day 1
This commit is contained in:
commit
2c739b8f5f
41
day01.py
Normal file
41
day01.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
f = open("day1input.txt", "r")
|
||||||
|
a = f.readlines()
|
||||||
|
b = list(map(str.strip, a))
|
||||||
|
c = list(map(lambda x: -int(x[1:]) if x[0] == "L" else int(x[1:]), b))
|
||||||
|
|
||||||
|
|
||||||
|
def part1(data: list[str], pos: int = 50, clicks: int = 100) -> int:
|
||||||
|
total = pos
|
||||||
|
zeroes = 0
|
||||||
|
for i in data:
|
||||||
|
total = (total + i) % clicks
|
||||||
|
if total == 0:
|
||||||
|
zeroes += 1
|
||||||
|
return zeroes
|
||||||
|
|
||||||
|
|
||||||
|
def part2(data: list[str], pos: int = 50, clicks: int = 100) -> int:
|
||||||
|
total = pos
|
||||||
|
zeroes = 0
|
||||||
|
|
||||||
|
for i in data:
|
||||||
|
if i >= 0:
|
||||||
|
m, total = divmod((total + i), clicks)
|
||||||
|
zeroes += m
|
||||||
|
else:
|
||||||
|
newtotal = (total + i) % clicks
|
||||||
|
if total == 0:
|
||||||
|
zeroes += -(i // clicks) - 1
|
||||||
|
elif -i > total:
|
||||||
|
zeroes += -((total + i) // clicks)
|
||||||
|
if newtotal == 0:
|
||||||
|
zeroes += 1
|
||||||
|
elif -i == total:
|
||||||
|
zeroes += 1
|
||||||
|
total = newtotal
|
||||||
|
return zeroes
|
||||||
|
|
||||||
|
print(f"Solution to part 1: {part1(c)}")
|
||||||
|
print(f"Solution to part 2: {part2(c)}")
|
||||||
4107
day01input.txt
Normal file
4107
day01input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user