From 44f6aa0f53a630b8654b666632f006f970b906ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Thu, 5 Dec 2024 18:42:22 +0100 Subject: [PATCH] Solve 2024:5 p1-2 "Print Queue" In p2, it took some tries to find a correct way to rearrange incorrect pagesets. The code ended up just rearranging first found incorrect order and readding the updated pageset to queue. --- 2024-python/output/day_05.py | 77 ++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 2024-python/output/day_05.py diff --git a/2024-python/output/day_05.py b/2024-python/output/day_05.py new file mode 100644 index 0000000..46863a7 --- /dev/null +++ b/2024-python/output/day_05.py @@ -0,0 +1,77 @@ +from output import ints + + +def solve(data): + rules, pagelists = data.split("\n\n") + rules, pagelists = ( + set(map(lambda line: tuple(ints(line)), rules.splitlines())), + list(map(lambda line: ints(line), pagelists.splitlines())), + ) + + corrects = [] + incorrects = [] + adjusted = [] + + for pagelist in pagelists: + ok = True + + for i, page in enumerate(pagelist): + prevpages, nextpages = pagelist[:i], pagelist[i + 1 :] + + if not all((pp, page) in rules for pp in prevpages) or not all( + (page, np) in rules for np in nextpages + ): + ok = False + break + + if ok: + corrects.append(pagelist) + else: + incorrects.append(pagelist) + + while incorrects: + pagelist = incorrects.pop(0) + changed = False + + for i, page in enumerate(pagelist): + prevpages, nextpages = pagelist[:i], pagelist[i + 1 :] + + for prevpage in prevpages: + if (prevpage, page) in rules: + continue + + nextpages.append(prevpages.pop()) + pagelist = prevpages + [page] + nextpages + changed = True + break + + if not changed: + for nextpage in nextpages: + if (page, nextpage) in rules: + continue + prevpages.append(nextpages.pop()) + pagelist = prevpages + [page] + nextpages + changed = True + break + + if changed: + break + + if not changed: + adjusted.append(pagelist) + else: + incorrects.append(pagelist) + + p1 = sum(c[len(c) // 2] for c in corrects) + p2 = sum(r[len(r) // 2] for r in adjusted) + return p1, p2 + + +if __name__ == "__main__": + with open("./input/05.txt", "r") as f: + inp = f.read().strip() + + p1, p2 = solve(inp) + + print(p1) + print(p2)