My Advent of Code solutions in Python.
kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code
python
1import os # NOQA
2import sys # NOQA
3import re # NOQA
4import math # NOQA
5import copy # NOQA
6import fileinput
7from string import ascii_uppercase, ascii_lowercase # NOQA
8from collections import Counter, defaultdict, deque, namedtuple # NOQA
9from itertools import count, product, permutations, combinations, combinations_with_replacement # NOQA
10
11from utils import parse_line, parse_nums, mul, all_unique, factors, memoize, primes # NOQA
12from utils import chunks, gcd, lcm, print_grid, min_max_xy # NOQA
13from utils import new_table, transposed, rotated # NOQA
14from utils import md5, sha256, knot_hash # NOQA
15from utils import VOWELS, CONSONANTS # NOQA
16from utils import Point, DIRS, DIRS_4, DIRS_8 # NOQA # N (0, 1) -> E (1, 0) -> S (0, -1) -> W (-1, 0)
17
18# Itertools Functions:
19# product('ABCD', repeat=2) AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD
20# permutations('ABCD', 2) AB AC AD BA BC BD CA CB CD DA DB DC
21# combinations('ABCD', 2) AB AC AD BC BD CD
22# combinations_with_replacement('ABCD', 2) AA AB AC AD BB BC BD CC CD DD
23
24tot = 0
25res = []
26board = {}
27table = new_table(None, width=2, height=4)
28
29# Uncomment for multi-group style inputs. :c
30# data = ''.join([line for line in fileinput.input()])
31# groups = [g.split('\n') for g in data.split('\n\n')]
32
33for y, line in enumerate(fileinput.input()):
34 line = line.strip()
35 nums = parse_nums(line)
36 data = parse_line(r'', line)
37 nums = [int(x) for x in line.split(',')]
38
39 for x, c in enumerate(line):
40 board[Point(x, y)] = c
41
42 if y == 0:
43 print(data)
44
45print nums
46
47
48
49seen = defaultdict(list)
50
51for i, n in enumerate(nums):
52 seen[n].append(i)
53
54print seen
55
56spoken = nums[-1]
57i = len(nums)
58
59while True:
60 # print spoken, i
61 if len(seen[spoken]) == 1:
62 new_spoke = 0
63 else:
64 new_spoke = i - seen[spoken][-2] - 1
65
66 # print "newspoke", new_spoke
67
68 spoken = new_spoke
69 seen[spoken].append(i)
70
71 i += 1
72 if i == 30000000:
73 break
74
75print spoken
76
77