Skip to content

Commit 8f96d20

Browse files
committed
script improvements
1 parent 66dab45 commit 8f96d20

File tree

3 files changed

+311
-75
lines changed

3 files changed

+311
-75
lines changed

2019/day25/play.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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")

scripts/Dockerfile

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM debian:bookworm
22

33
RUN apt-get update && \
44
apt-get upgrade -y && \
5-
apt-get install -y vim curl wget sudo && \
5+
apt-get install -y vim curl wget sudo sqlite3 && \
66
apt-get install -y build-essential cmake gdb python3-full && \
77
apt-get install -y clang llvm
88

@@ -28,20 +28,19 @@ RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \
2828
chmod 777 /usr/local/cargo
2929

3030
# PyPy
31-
RUN curl -sL https://downloads.python.org/pypy/pypy3.10-v7.3.15-linux64.tar.bz2 | tar -C /opt -xj && \
32-
/opt/pypy3.10-v7.3.15-linux64/bin/pypy3 -mensurepip
31+
RUN curl -sL https://downloads.python.org/pypy/pypy3.10-v7.3.15-aarch64.tar.bz2 | tar -C /opt -xj && \
32+
/opt/pypy3.10-v7.3.15-aarch64/bin/pypy3 -mensurepip
3333

3434
# Python 3.10 → 3.13
3535
COPY allpython.sh /
3636
RUN /allpython.sh
3737

3838
# Virtual Environments
39-
COPY requirements.txt /
4039
RUN /opt/python/Python-3.10.13/bin/python3.10 -mvenv /venv/py3.10
4140
RUN /opt/python/Python-3.11.7/bin/python3.11 -mvenv /venv/py3.11
4241
RUN /opt/python/Python-3.12.1/bin/python3.12 -mvenv /venv/py3.12
4342
RUN /opt/python/Python-3.13.0a3/bin/python3.13 -mvenv /venv/py3.13
44-
RUN /opt/pypy3.10-v7.3.15-linux64/bin/pypy3.10 -mvenv /venv/pypy3.10
43+
RUN /opt/pypy3.10-v7.3.15-aarch64/bin/pypy3.10 -mvenv /venv/pypy3.10
4544
RUN python3 -mvenv /venv/python
4645

4746
# User environment

0 commit comments

Comments
 (0)