|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# [Day 25: Cryostasis](https://adventofcode.com/2019/day/25) |
| 3 | + |
| 4 | +import argparse |
| 5 | +import re |
| 6 | +import sys |
| 7 | +from functools import reduce |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +sys.path.append(Path(__file__).parent.parent.as_posix()) |
| 11 | +from intcode.Intcode import Computer # noqa |
| 12 | + |
| 13 | +parser = argparse.ArgumentParser() |
| 14 | +parser.add_argument("-v", "--verbose", action="store_true") |
| 15 | +parser.add_argument("-m", "--manual", action="store_true", help="play the game") |
| 16 | +parser.add_argument("input", nargs="?", default="input.txt") |
| 17 | +args = parser.parse_args() |
| 18 | + |
| 19 | +software = Path(args.input).read_text() |
| 20 | + |
| 21 | + |
| 22 | +computer = Computer() |
| 23 | +computer.load(software) |
| 24 | +computer.start() |
| 25 | + |
| 26 | + |
| 27 | +if not args.manual: |
| 28 | + if reduce(lambda a, b: a ^ b, computer.program) != -2251798974787211: |
| 29 | + print("work only for my puzzle input", file=sys.stderr) |
| 30 | + exit(2) |
| 31 | + |
| 32 | + # explore the spacecraft and take items - works only for my puzzle input |
| 33 | + explore_cmds = ( |
| 34 | + "east,east,take semiconductor,north,take planetoid,west,take food ration,west,west," |
| 35 | + + "take monolith,east,east,north,take space law space brochure,north,north," |
| 36 | + + "take weather machine,south,south,south,east,north,take antenna,east,north," |
| 37 | + + "south,north,west,east,south,south,east,north,south,west,east,west,south,east," |
| 38 | + + "south,south,south,east,inv,west,west,west,north,north,west,north,north,south," |
| 39 | + + "west,north,east,take jam,west,south,east,south,east,south,south,east,inv" |
| 40 | + ).split(",") |
| 41 | + |
| 42 | + # try all combinations of items - works only for my puzzle input |
| 43 | + solve_cmds = [] |
| 44 | + items = "food ration,weather machine,antenna,space law space brochure,jam,semiconductor,planetoid,monolith".split( |
| 45 | + "," |
| 46 | + ) |
| 47 | + for k in range(8): |
| 48 | + solve_cmds.append("drop " + items[k]) |
| 49 | + |
| 50 | + previous = 0 |
| 51 | + for i in range(1, 256): |
| 52 | + for k in range(8): |
| 53 | + if i & (1 << k) != 0: |
| 54 | + if previous & (1 << k) == 0: |
| 55 | + solve_cmds.append("take " + items[k]) |
| 56 | + if i & (1 << k) == 0: |
| 57 | + if (previous & (1 << k)) != 0: |
| 58 | + solve_cmds.append("drop " + items[k]) |
| 59 | + solve_cmds.append("inv") |
| 60 | + solve_cmds.append("east") |
| 61 | + previous = i |
| 62 | + |
| 63 | + # let's go |
| 64 | + for cmd in explore_cmds + solve_cmds: |
| 65 | + if args.verbose: |
| 66 | + print(f"> {cmd}") |
| 67 | + |
| 68 | + computer.input.extend(map(ord, cmd)) |
| 69 | + computer.input.append(10) |
| 70 | + state = computer.resume() |
| 71 | + |
| 72 | + t = "".join(map(chr, computer.output)) |
| 73 | + if args.verbose: |
| 74 | + print(f"\033[2m{t}\033[0m") |
| 75 | + |
| 76 | + computer.flush_io() |
| 77 | + |
| 78 | + if state != "read": |
| 79 | + answer = re.search( |
| 80 | + r"Oh, hello! You should be able to get in by typing (\d+) on the keypad at the main airlock.", t |
| 81 | + ) |
| 82 | + if answer: |
| 83 | + answer = answer[1] |
| 84 | + break |
| 85 | + |
| 86 | + print(answer) |
| 87 | + |
| 88 | +else: |
| 89 | + shortcuts = { |
| 90 | + "n": "north", |
| 91 | + "s": "south", |
| 92 | + "e": "east", |
| 93 | + "w": "west", |
| 94 | + } |
| 95 | + |
| 96 | + log = Path("moves.log").open("w") |
| 97 | + |
| 98 | + state = computer.run() |
| 99 | + while state == "read": |
| 100 | + t = "".join(map(chr, computer.output)) |
| 101 | + print(f"\033[2m{t}\033[0m") |
| 102 | + computer.flush_io() |
| 103 | + |
| 104 | + # 't' to take the current item |
| 105 | + w = "" |
| 106 | + take = "" |
| 107 | + for line in t.splitlines(): |
| 108 | + if line.startswith("Items here:"): |
| 109 | + w = "items" |
| 110 | + elif line.startswith("- ") and w == "items": |
| 111 | + take = "take " + line[2:] |
| 112 | + break |
| 113 | + else: |
| 114 | + w = "" |
| 115 | + |
| 116 | + while True: |
| 117 | + value = input("input> ") |
| 118 | + if value.strip() == "": |
| 119 | + continue |
| 120 | + |
| 121 | + if take and value == "t": |
| 122 | + value = take |
| 123 | + else: |
| 124 | + value = shortcuts.get(value, value) |
| 125 | + |
| 126 | + print(value, file=log) |
| 127 | + log.flush() |
| 128 | + |
| 129 | + computer.input.extend(map(ord, value)) |
| 130 | + computer.input.append(10) |
| 131 | + break |
| 132 | + |
| 133 | + state = computer.resume() |
| 134 | + |
| 135 | + t = "".join(map(chr, computer.output)) |
| 136 | + print(f"\033[2m{t}\033[0m") |
0 commit comments