Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 548e8b5

Browse files
committed
2018 day 5 part 1
1 parent 9c8bdaf commit 548e8b5

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

aoc2018/day5.input

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

aoc2018/day5.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python
2+
"""
3+
--- Day 5: Alchemical Reduction ---
4+
5+
You've managed to sneak in to the prototype suit manufacturing lab. The Elves are making decent progress, but are still struggling with the suit's size reduction capabilities.
6+
7+
While the very latest in 1518 alchemical technology might have solved their problem eventually, you can do better. You scan the chemical composition of the suit's material and discover that it is formed by extremely long polymers (one of which is available as your puzzle input).
8+
9+
The polymer is formed by smaller units which, when triggered, react with each other such that two adjacent units of the same type and opposite polarity are destroyed. Units' types are represented by letters; units' polarity is represented by capitalization. For instance, r and R are units with the same type but opposite polarity, whereas r and s are entirely different types and do not react.
10+
11+
For example:
12+
13+
In aA, a and A react, leaving nothing behind.
14+
In abBA, bB destroys itself, leaving aA. As above, this then destroys itself, leaving nothing.
15+
In abAB, no two adjacent units are of the same type, and so nothing happens.
16+
In aabAAB, even though aa and AA are of the same type, their polarities match, and so nothing happens.
17+
18+
Now, consider a larger example, dabAcCaCBAcCcaDA:
19+
20+
dabAcCaCBAcCcaDA The first 'cC' is removed.
21+
dabAaCBAcCcaDA This creates 'Aa', which is removed.
22+
dabCBAcCcaDA Either 'cC' or 'Cc' are removed (the result is the same).
23+
dabCBAcaDA No further actions can be taken.
24+
25+
After all possible reactions, the resulting polymer contains 10 units.
26+
27+
How many units remain after fully reacting the polymer you scanned?
28+
29+
"""
30+
from __future__ import print_function
31+
import os
32+
import re
33+
34+
35+
def solve(data, flag=False):
36+
# print("data", data)
37+
done = False
38+
while not done:
39+
for idx, (a, b) in enumerate(zip(data, data[1:])):
40+
if a != b and a.lower() == b.lower():
41+
# print(f"removing {a} {b} ({idx})")
42+
data = data[:idx] + data[idx+2:]
43+
# print("data", data)
44+
break
45+
else:
46+
done = True
47+
return len(data)
48+
49+
50+
if __name__ == "__main__":
51+
this_dir = os.path.dirname(__file__)
52+
with open(os.path.join(this_dir, "day5.input")) as f:
53+
data = f.read().strip()
54+
print(solve(data, False))
55+
# print(solve(data, True))

aoc2018/test_aoc.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import pytest
77

8-
from . import day1, day2, day3, day4
8+
from . import day1, day2, day3, day4, day5
99

1010

1111
@pytest.mark.parametrize(
@@ -74,3 +74,9 @@ def test_day_3(data, answer, flag):
7474
[1518-11-05 00:55] wakes up""".splitlines(), 4455, True),])
7575
def test_day_4(data, answer, flag):
7676
assert day4.solve(data, flag) == answer
77+
78+
@pytest.mark.parametrize('data,answer,flag', [
79+
("dabAcCaCBAcCcaDA", 10, False),
80+
])
81+
def test_day_5(data, answer, flag):
82+
assert day5.solve(data, flag) == answer

0 commit comments

Comments
 (0)