day15: add python solution
This commit is contained in:
parent
96ce02443c
commit
f36f28dac1
4 changed files with 31 additions and 0 deletions
|
@ -19,5 +19,6 @@ https://adventofcode.com/2020/
|
||||||
|11 | `**` | | |
|
|11 | `**` | | |
|
||||||
|13 | | `**` | |
|
|13 | | `**` | |
|
||||||
|14 | | `**` | |
|
|14 | | `**` | |
|
||||||
|
|15 | `**` | | |
|
||||||
|
|
||||||
`test.sh` can be used to run all solutions and automatically compares them to (my) puzzle inputs and the expected outputs.
|
`test.sh` can be used to run all solutions and automatically compares them to (my) puzzle inputs and the expected outputs.
|
||||||
|
|
2
data/day15.expected
Normal file
2
data/day15.expected
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
496
|
||||||
|
883
|
1
data/day15.input
Normal file
1
data/day15.input
Normal file
|
@ -0,0 +1 @@
|
||||||
|
2,0,1,7,4,14,18
|
27
day15/day15.py
Normal file
27
day15/day15.py
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def run_game(init, iterations):
|
||||||
|
last_seen = {}
|
||||||
|
previous = None
|
||||||
|
|
||||||
|
for i, n in enumerate(init):
|
||||||
|
if previous is not None:
|
||||||
|
last_seen[previous] = i-1
|
||||||
|
previous = n
|
||||||
|
|
||||||
|
for i in range(i+1, iterations):
|
||||||
|
if previous in last_seen:
|
||||||
|
x = i - last_seen[previous] - 1
|
||||||
|
else:
|
||||||
|
x = 0
|
||||||
|
last_seen[previous] = i-1
|
||||||
|
previous = x
|
||||||
|
|
||||||
|
return previous
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
init = [int(n) for n in sys.stdin.read().split(',')]
|
||||||
|
print(run_game(init, 2020))
|
||||||
|
print(run_game(init, 30000000))
|
Loading…
Reference in a new issue