-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd05.py
More file actions
56 lines (39 loc) · 1.22 KB
/
d05.py
File metadata and controls
56 lines (39 loc) · 1.22 KB
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
import string
from typing import List
from day import Day, register_day
@register_day(2018, 5)
class Day05(Day):
def part_one(self) -> str:
data = self.read_input()
units = list(data)
collapsed = collapse(units)
return str(len(collapsed))
def part_two(self) -> str:
data = self.read_input()
smallest = float("inf")
for char in list(string.ascii_lowercase):
new_data = data.replace(char, "").replace(char.upper(), "")
size = len(collapse(list(new_data)))
if size < smallest:
smallest = size
return str(smallest)
def collapse(original_units: List[str]) -> List[str]:
units = original_units.copy()
i = 0
while True:
if i < 0:
i = 0
curr_unit = units[i]
next_unit = units[i + 1]
if is_opposite(curr_unit, next_unit):
del units[i : i + 2]
i -= 1
continue
i += 1
if i == len(units) - 1:
break
return units
def is_opposite(left: str, right: str) -> bool:
return (left.lower() == right.lower()) and (
(left.isupper() and right.islower()) or (left.islower() and right.isupper())
)