-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.py
61 lines (50 loc) · 1.5 KB
/
day1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from collections import defaultdict
DATA = open('day1.txt').read()
NAMES = ['one', 'two', 'three', 'four',
'five', 'six', 'seven', 'eight', 'nine']
class Node:
def __init__(self):
self.value = None
self.childs = defaultdict(Node)
def insert(self, s, value=None):
node = self
for c in s:
node = node.childs[c]
node.value = value if value else s
def find(self, s):
node = self
for c in s:
if c not in node.childs:
return None
node = node.childs[c]
if node.value:
return node.value
return None
def run(part2=False):
trie, trie_reversed = Node(), Node()
for i in range(10):
trie.insert(str(i + 1))
trie_reversed.insert(str(i + 1))
if part2:
for i, name in enumerate(NAMES):
trie.insert(name, str(i + 1))
trie_reversed.insert(reversed(name), str(i + 1))
result = 0
for line in DATA.splitlines():
first_digit = last_digit = None
for i in range(len(line)):
first_digit = trie.find(line[i:])
if first_digit:
break
for i in reversed(range(len(line))):
last_digit = trie_reversed.find(line[i::-1])
if last_digit:
break
result += int(first_digit + last_digit)
return result
result = run()
print(result)
assert (result == 55017)
result = run(True)
print(result)
assert (result == 53539)