From e822334261b74295a5788f02da1bf25cea0c61a7 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Tue, 15 Dec 2020 11:27:54 +0100 Subject: [PATCH] 2020 day15/python: add solution --- 2020/README.md | 1 + 2020/data/day15.expected | 2 ++ 2020/data/day15.input | 1 + 2020/day15/day15.py | 27 +++++++++++++++++++++++++++ 4 files changed, 31 insertions(+) create mode 100644 2020/data/day15.expected create mode 100644 2020/data/day15.input create mode 100644 2020/day15/day15.py diff --git a/2020/README.md b/2020/README.md index 70d023d..3074ff9 100644 --- a/2020/README.md +++ b/2020/README.md @@ -19,5 +19,6 @@ https://adventofcode.com/2020/ |11 | `**` | | | |13 | | `**` | | |14 | | `**` | | +|15 | `**` | | | `test.sh` can be used to run all solutions and automatically compares them to (my) puzzle inputs and the expected outputs. diff --git a/2020/data/day15.expected b/2020/data/day15.expected new file mode 100644 index 0000000..6e90eaa --- /dev/null +++ b/2020/data/day15.expected @@ -0,0 +1,2 @@ +496 +883 diff --git a/2020/data/day15.input b/2020/data/day15.input new file mode 100644 index 0000000..1f8b084 --- /dev/null +++ b/2020/data/day15.input @@ -0,0 +1 @@ +2,0,1,7,4,14,18 diff --git a/2020/day15/day15.py b/2020/day15/day15.py new file mode 100644 index 0000000..eb63221 --- /dev/null +++ b/2020/day15/day15.py @@ -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))