Compare commits
2 commits
5bc5c4042e
...
cbedbccd57
Author | SHA1 | Date | |
---|---|---|---|
dece | cbedbccd57 | ||
dece | a6e7e49dca |
34
2020/day10.py
Normal file
34
2020/day10.py
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
lines = sorted([int(line.rstrip()) for line in sys.stdin])
|
||||||
|
|
||||||
|
# Part 1
|
||||||
|
diffs = {1: 0, 3: 0}
|
||||||
|
jojo = 0
|
||||||
|
for v in lines:
|
||||||
|
d = v - jojo
|
||||||
|
diffs[d] += 1
|
||||||
|
jojo = v
|
||||||
|
print("Part 1:", diffs[1] * (diffs[3] + 1))
|
||||||
|
|
||||||
|
# Part 2 - yes recursion is fine but have you tried caching your damn values
|
||||||
|
print("Part 2:", rec(0, lines, 0, len(lines), {}))
|
||||||
|
|
||||||
|
|
||||||
|
def rec(i, l, n, z, c):
|
||||||
|
if i >= z - 1:
|
||||||
|
return 1
|
||||||
|
if (p := c.get((i, n))) is not None:
|
||||||
|
return p
|
||||||
|
p = 0
|
||||||
|
for ni, nv in enumerate(l[i:i+3]):
|
||||||
|
if n < nv <= n + 3:
|
||||||
|
p += rec(i + ni + 1, l, nv, z, c)
|
||||||
|
c[(i, n)] = p
|
||||||
|
return p
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
|
@ -1,9 +1,9 @@
|
||||||
import itertools
|
import itertools
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
with open("day9.txt", "rt") as f:
|
series = [int(line.rstrip()) for line in sys.stdin]
|
||||||
series = [int(line.rstrip()) for line in f.readlines()]
|
|
||||||
|
|
||||||
# Part 1
|
# Part 1
|
||||||
frame_len = 25
|
frame_len = 25
|
||||||
|
|
|
@ -4,9 +4,11 @@ import os
|
||||||
from datetime import date
|
from datetime import date
|
||||||
|
|
||||||
TEMPLATE = """\
|
TEMPLATE = """\
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
with open("{}", "rt") as f:
|
lines = [line.rstrip() for line in sys.stdin]
|
||||||
lines = [line.rstrip() for line in f.readlines()]
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -15,5 +17,5 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
day = date.today().day
|
day = date.today().day
|
||||||
with open(f"day{day}.py", "wt") as f:
|
with open(f"day{day}.py", "wt") as f:
|
||||||
f.write(TEMPLATE.format(f"day{day}.txt"))
|
f.write(TEMPLATE)
|
||||||
os.system(f"python ../fetch.py {day}")
|
os.system(f"python ../fetch.py {day}")
|
||||||
|
|
Loading…
Reference in a new issue