From 09f5a715aaf99a4f5ad161588554f8939690c5f3 Mon Sep 17 00:00:00 2001 From: dece Date: Tue, 15 Dec 2020 13:33:00 +0100 Subject: [PATCH] Day 15 --- 2020/day15.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2020/day15.py diff --git a/2020/day15.py b/2020/day15.py new file mode 100644 index 0000000..32e4861 --- /dev/null +++ b/2020/day15.py @@ -0,0 +1,32 @@ +import sys +from collections import defaultdict + + +def main(): + nl = list(map(lambda s: int(s), sys.stdin.read().rstrip().split(","))) + + # Part 1 + print("Part 1:", get_nth(2020, nl)) + # Part 2 + print("Part 2:", get_nth(30000000, nl)) + + +def get_nth(limit, starters): # pretty horrible complexity but i'm behind sched + said = defaultdict(list) + for turn in range(1, limit + 1): + if turn <= len(starters): # starting number shit special case. + n = starters[turn - 1] + else: + last_spoken = n + if len(said[last_spoken]) == 1: + n = 0 + else: + last_turn_said = said[last_spoken][-2] + n = turn - 1 - last_turn_said + said[n].append(turn) + said[n] = said[n][-2:] + return n + + +if __name__ == "__main__": + main()