-
Notifications
You must be signed in to change notification settings - Fork 0
/
16.py
64 lines (55 loc) · 1.38 KB
/
16.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
print(chr(27)+'[2j')
print('\033c', end='')
f = open('16.input', 'r')
lines = [x.strip() for x in f.readlines()]
ticker_tape = """
children: 3
cats: 7
samoyeds: 2
pomeranians: 3
akitas: 0
vizslas: 0
goldfish: 5
trees: 3
cars: 2
perfumes: 1""".strip().split('\n')
message = {}
for t in ticker_tape:
message[t.split(':')[0]] = int(t.split(':')[1])
# Part 1
for line in lines:
head, tail = line.split(': ', 1)
sue_id = int(head.split(' ')[1])
parts = tail.split(', ')
is_match = True
for part in parts:
key, value = part.split(': ')
if message[key] != int(value):
is_match = False
break
if is_match:
print('Solution part 1:', sue_id)
break
# Part 2
for line in lines:
head, tail = line.split(': ', 1)
sue_id = int(head.split(' ')[1])
parts = tail.split(', ')
is_match = True
for part in parts:
key, value = part.split(': ')
if key in ['cats', 'trees']:
if message[key] >= int(value):
is_match = False
break
elif key in ['pomeranians', 'goldfish']:
if message[key] <= int(value):
is_match = False
break
else:
if message[key] != int(value):
is_match = False
break
if is_match:
print('Solution part 2:', sue_id)
break