From a06dcab3ec93e630e77f9a1a24e871d5788d9e02 Mon Sep 17 00:00:00 2001 From: Joscha Date: Tue, 6 Dec 2022 15:26:52 +0100 Subject: [PATCH] [py] Port 2018_05 --- py/2018/05/solve.py | 84 -------------------------------------- py/2018/05/test_input.txt | 1 - py/2018/05/test_inputs.txt | 5 --- py/aoc/__init__.py | 3 +- py/aoc/y2018/d05.py | 79 +++++++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 91 deletions(-) delete mode 100644 py/2018/05/solve.py delete mode 100644 py/2018/05/test_input.txt delete mode 100644 py/2018/05/test_inputs.txt create mode 100644 py/aoc/y2018/d05.py diff --git a/py/2018/05/solve.py b/py/2018/05/solve.py deleted file mode 100644 index ead1665..0000000 --- a/py/2018/05/solve.py +++ /dev/null @@ -1,84 +0,0 @@ -import sys - -# PART 1 - -def load_polymer(filename): - pols = [] - with open(filename, "r") as f: - for line in f: - pols.append(list(line[:-1])) - return pols - -def naive_react(pol): - for i in range(len(pol) - 1): - if pol[i].lower() == pol[i+1].lower() and pol[i] != pol[i+1]: - print("Naive react says: More reacting is possible.") - -def react(pol): - while True: - i = 0 - while i < len(pol) - 1: - pol_cur, pol_next = pol[i], pol[i+1] - if pol_cur.lower() == pol_next.lower() and pol_cur != pol_next: - del pol[i] - del pol[i] - i = max(0, i - 1) - else: - i += 1 - else: - return - -#def react(pol): -# start = 0 -# while True: -# i = start -# while i < len(pol) - 1: -# if pol[i].lower() == pol[i+1].lower() and pol[i] != pol[i+1]: -# del pol[i] -# del pol[i] -# start = max(0, start - 1) -# break -# elif i == start + 1: -# start = i -# i += 1 -# else: -# return - -def result(pol): - l = pol.copy() - #print("".join(l)) - react(l) - #print("->", "".join(l)) - naive_react(l) - return len(l) - -# PART 2 - -def removable_chars(pol): - return set(c.lower() for c in pol) - -def remove(pol, char): - return [c for c in pol if c.lower() != char.lower()] - -def find_obstructing(pol): - results = [] - chars = removable_chars(pol) - for c in sorted(chars): - l = remove(pol, c) - n = result(l) - print("Removed", c, "-> length", n) - results.append(n) - return min(results) - -def main(filename): - print(f"Solutions for {filename}") - pols = load_polymer(filename) - for pol in pols: - length = result(pol) - print(f"Part 1: {length}") - best_result = find_obstructing(pol) - print(f"Part 2: {best_result}") - -if __name__ == "__main__": - for filename in sys.argv[1:]: - main(filename) diff --git a/py/2018/05/test_input.txt b/py/2018/05/test_input.txt deleted file mode 100644 index 8637b35..0000000 --- a/py/2018/05/test_input.txt +++ /dev/null @@ -1 +0,0 @@ -dabAcCaCBAcCcaDA diff --git a/py/2018/05/test_inputs.txt b/py/2018/05/test_inputs.txt deleted file mode 100644 index 93de6bd..0000000 --- a/py/2018/05/test_inputs.txt +++ /dev/null @@ -1,5 +0,0 @@ -aA -abBA -abAB -aabAAB -dabAcCaCBAcCcaDA diff --git a/py/aoc/__init__.py b/py/aoc/__init__.py index 2e871aa..d2e0d1b 100644 --- a/py/aoc/__init__.py +++ b/py/aoc/__init__.py @@ -2,7 +2,7 @@ import sys import argparse from pathlib import Path -from .y2018 import d01, d02, d03, d04 +from .y2018 import d01, d02, d03, d04, d05 from .y2020 import d10 from .y2021 import d14 from .y2022 import d01, d02, d03, d04, d05, d06 @@ -12,6 +12,7 @@ DAYS = { "2018_02": y2018.d02.solve, "2018_03": y2018.d03.solve, "2018_04": y2018.d04.solve, + "2018_05": y2018.d05.solve, "2020_10": y2020.d10.solve, "2021_14": y2021.d14.solve, "2022_01": y2022.d01.solve, diff --git a/py/aoc/y2018/d05.py b/py/aoc/y2018/d05.py new file mode 100644 index 0000000..4f9aee5 --- /dev/null +++ b/py/aoc/y2018/d05.py @@ -0,0 +1,79 @@ +# PART 1 + + +def naive_react(pol): + for i in range(len(pol) - 1): + if pol[i].lower() == pol[i + 1].lower() and pol[i] != pol[i + 1]: + print("Naive react says: More reacting is possible.") + + +def react(pol): + while True: + i = 0 + while i < len(pol) - 1: + pol_cur, pol_next = pol[i], pol[i + 1] + if pol_cur.lower() == pol_next.lower() and pol_cur != pol_next: + del pol[i] + del pol[i] + i = max(0, i - 1) + else: + i += 1 + else: + return + + +# def react(pol): +# start = 0 +# while True: +# i = start +# while i < len(pol) - 1: +# if pol[i].lower() == pol[i+1].lower() and pol[i] != pol[i+1]: +# del pol[i] +# del pol[i] +# start = max(0, start - 1) +# break +# elif i == start + 1: +# start = i +# i += 1 +# else: +# return + + +def result(pol): + l = pol.copy() + # print("".join(l)) + react(l) + # print("->", "".join(l)) + naive_react(l) + return len(l) + + +# PART 2 + + +def removable_chars(pol): + return set(c.lower() for c in pol) + + +def remove(pol, char): + return [c for c in pol if c.lower() != char.lower()] + + +def find_obstructing(pol): + results = [] + chars = removable_chars(pol) + for c in sorted(chars): + l = remove(pol, c) + n = result(l) + # print("Removed", c, "-> length", n) + results.append(n) + return min(results) + + +def solve(inputstr): + pols = [list(line) for line in inputstr.splitlines()] + for pol in pols: + length = result(pol) + print(f"Part 1: {length}") + best_result = find_obstructing(pol) + print(f"Part 2: {best_result}")