day5 (painfully slow)

This commit is contained in:
Stefan Harmuth 2022-08-10 23:34:29 +02:00
parent d317513134
commit a5d1167aec
4 changed files with 81 additions and 1 deletions

View File

@ -28,7 +28,7 @@
"wrong": [],
"correct": 239
}
}
},
"4": {
"1": {
"wrong": [],
@ -41,5 +41,15 @@
],
"correct": 7887
}
},
"5": {
"1": {
"wrong": [],
"correct": 9348
},
"2": {
"wrong": [],
"correct": 4996
}
}
}

68
day05.py Normal file
View File

@ -0,0 +1,68 @@
from tools.aoc import AOCDay
from typing import Any
def react(polymer: str) -> str:
reacted = True
while reacted:
reacted = False
new_poly = ""
i = 0
while i < len(polymer) - 1:
if abs(ord(polymer[i]) - ord(polymer[i + 1])) != 32:
new_poly += polymer[i]
else:
i += 1
reacted = True
i += 1
if i < len(polymer):
new_poly += polymer[-1]
polymer = new_poly
return polymer
class Day(AOCDay):
inputs = [
[
(10, "test_input5_1"),
(9348, "input5")
],
[
(4, "test_input5_1"),
(4996, "input5")
]
]
def part1(self) -> Any:
polymer = self.getInput()
return len(react(polymer))
def part2(self) -> Any:
polymer = self.getInput()
tested = set()
min_length = len(polymer)
for c in polymer:
if c in tested:
continue
tested.add(c.upper())
tested.add(c.lower())
test_polymer = ""
for x in polymer:
if x != c.upper() and x != c.lower():
test_polymer += x
this_length = len(react(test_polymer))
if this_length < min_length:
min_length = this_length
return min_length
if __name__ == '__main__':
day = Day(2018, 5)
day.run(verbose=True)

1
inputs/input5 Normal file

File diff suppressed because one or more lines are too long

1
inputs/test_input5_1 Normal file
View File

@ -0,0 +1 @@
dabAcCaCBAcCcaDA