-
Notifications
You must be signed in to change notification settings - Fork 0
/
day5puzzle.py
43 lines (35 loc) · 1.02 KB
/
day5puzzle.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
with open('polymer.txt', 'r') as file:
polymer = file.read().strip()
#puzzle 1
def react(polymer):
chain = []
for unit in polymer:
if len(chain) < 1:
chain.append(unit)
else:
cur = chain[-1]
if ((cur.isupper() and unit.islower()) or (cur.islower() and unit.isupper())) and cur.lower() == unit.lower():
chain.pop(-1)
else:
chain.append(unit)
return len(chain)
print(react('aA'))
print(react('abBA'))
print(react('abAB'))
print(react('aabAAB'))
print(react('dabAcCaCBAcCcaDA'))
print(react(polymer))
#puzzle 2
def whichunit(polymer):
bestunit = ''
lowest = len(polymer)
typs = ''.join(set(polymer.lower()))
for typ in typs:
reduced_polymer = polymer.replace(typ, '').replace(typ.upper(), '')
length = react(reduced_polymer)
if length < lowest:
bestunit = typ
lowest = length
return bestunit, lowest
print(whichunit('dabAcCaCBAcCcaDA'))
print(whichunit(polymer))