-
Notifications
You must be signed in to change notification settings - Fork 0
/
day05.py
110 lines (83 loc) · 2.64 KB
/
day05.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import os
from itertools import tee
import logging as log
from pathlib import Path
from collections import Counter, deque
import string
import pytest
LOGLEVEL = os.environ.get("LOGLEVEL", "DEBUG").upper()
log.basicConfig(level=LOGLEVEL)
test_pattern = "dabAcCaCBAcCcaDA"
def react(pair):
x, y = pair
if x == y:
return False
if x == y.upper() or y == x.upper():
return True
return False
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip(a, b)
def can_react(i):
for pair in pairwise(i):
if react(pair):
return True
return False
def process_list(test_pattern):
paired_list = list(pairwise(test_pattern))
result_string = test_pattern
while can_react(result_string):
for pair in pairwise(result_string):
if react(pair):
reacted_string = "".join(pair)
half1, _, half2 = result_string.partition(reacted_string)
result_string = f"{half1}{half2}"
break
return result_string
def process_list2(test_pattern):
c = Counter()
paired_list = list(pairwise(test_pattern))
result_string = test_pattern
while can_react(result_string):
for pair in pairwise(result_string):
if react(pair):
reacted_string = "".join(pair)
c[reacted_string.lower()] += 1
half1, _, half2 = result_string.partition(reacted_string)
result_string = f"{half1}{half2}"
break
return result_string, c
def potential_pairs(pattern):
c = 0
for pair in pairwise(pattern):
if react(pair):
c += 1
return c
def process_stack(pattern):
"""Pop if reacts, add if not.
idea taken from: https://steadbytes.com/blog/advent-of-code-2018/05/"""
stack = deque()
for char in pattern:
if stack and react((char, stack[-1])):
stack.pop()
else:
stack.append(char)
return len(stack)
def process_stack2(pattern):
"""idea taken from: https://steadbytes.com/blog/advent-of-code-2018/05/"""
best = len(pattern)
for char in string.ascii_lowercase:
modified = pattern.replace(char, "")
modified = modified.replace(char.upper(), "")
modified_reacted_len = process_stack(modified)
best = min(best, modified_reacted_len)
return best
if __name__ == "__main__":
# part 1: 11590
# part 2: 4504
pattern = Path("day05_input.txt").read_text().strip()
result1 = process_stack(pattern)
result2 = process_stack2(pattern)
print(result1, result2)