-
Notifications
You must be signed in to change notification settings - Fork 1
/
07.py
60 lines (52 loc) · 1.64 KB
/
07.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
"""--- Day 7: Internet Protocol Version 7 ---"""
with open ("input/07.txt", "r") as f:
part_1 = part_2 = 0
for instruction in f:
abba_bad = abba_good = False
in_square_brackets = False
aba_potentials = []
bab_potentials = []
# do the work
for i in range(0, len(instruction) - 3):
# common to both parts
if instruction[i] == "[":
in_square_brackets = True
continue
if instruction[i] == "]":
in_square_brackets = False
continue
# part 2
if instruction[i] == instruction[i+2]:
if instruction[i] != instruction[i+1]:
if in_square_brackets:
aba_potentials.append(instruction[i:i+3])
else:
bab_potentials.append(instruction[i:i+3])
# part 1
if instruction[i] != instruction[i+3]: # a__b
continue
if instruction[i] == instruction[i+1]: # aa__
continue
if instruction[i+1] != instruction[i+2]: # _ab_
continue
if in_square_brackets:
abba_bad = True
abba_good = True
# ugly hack to get the final 3 chars
if len(instruction) > 2:
if instruction[i+1] == instruction[i+3]:
if instruction[i+1] != instruction[i+2]:
if in_square_brackets:
aba_potentials.append(instruction[i+1:i+4])
else:
bab_potentials.append(instruction[i+1:i+4])
if abba_good and not abba_bad:
part_1 += 1
if len(aba_potentials) > 0:
for aba in aba_potentials:
inverse = aba[1] + aba[0] + aba[1]
if inverse in bab_potentials:
part_2 += 1
break
print(part_1)
print(part_2)