Skip to content

Commit 1ad28d6

Browse files
committed
2018 Day 5
1 parent aadd2d7 commit 1ad28d6

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

2018/5.txt

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

2018/5a.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def polymerize(polymer):
2+
l = polymer[:]
3+
i = 1
4+
changed = False
5+
while True:
6+
first = l[i - 1]
7+
second = l[i]
8+
if first == second.swapcase():
9+
del l[i - 1]
10+
del l[i - 1]
11+
changed = True
12+
else:
13+
i += 1
14+
if i >= len(l):
15+
# We went through whole string
16+
if changed:
17+
# If we have changed something, we go over it again
18+
changed = False
19+
i = 1
20+
else:
21+
# If nothing changed, the string is finished
22+
break
23+
return len(l)
24+
25+
26+
with open("5.txt") as f:
27+
l = f.read()
28+
l = tuple(l.rstrip())
29+
30+
polymers = tuple(
31+
list(filter(lambda x: x not in c, l)) for c in ("aA", "bB", "cC", "dD")
32+
)
33+
34+
print(min(map(polymerize, polymers)))

2018/5b.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
with open("5.txt") as f:
2+
l = f.read()
3+
l = list(l.rstrip())
4+
i = 1
5+
changed = False
6+
while True:
7+
first = l[i - 1]
8+
second = l[i]
9+
if first == second.swapcase():
10+
del l[i - 1]
11+
del l[i - 1]
12+
changed = True
13+
else:
14+
i += 1
15+
if i >= len(l):
16+
# We went through whole string
17+
if changed:
18+
# If we have changed something, we go over it again
19+
changed = False
20+
i = 1
21+
else:
22+
# If nothing changed, the string is finished
23+
break
24+
25+
26+
print(len(l))

0 commit comments

Comments
 (0)