-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.nim
208 lines (170 loc) · 4.74 KB
/
solution.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import ../../lib/imports
import ../day9/programs
proc waitKey(prompt = "") {.inline.} =
discard readLineFromStdin(prompt)
proc getOutput(): string =
while queues[1].len > 0:
result &= queues[1].popFirst.char
proc writeInput(s: string) =
for ch in s:
queues[0].addLast ch.ord
queues[0].addLast '\n'.ord
proc walkInteractive(input: string) =
initQueues(2)
let p = newProgram(input, 0, 1)
while true:
discard p.stepOver
echo getOutput()
writeInput readLineFromStdin("")
when defined(interactive):
block:
let input = readFile("input").strip
input.walkInteractive
quit()
type
CellInfo = object
name: string
dirs: seq[string]
item: string
var hasAlert: bool
proc getCellInfo(output: string): CellInfo =
var name = ""
var dirs = newSeq[string]()
var item = ""
var items = newSeq[string]()
var readDoors, readItems = false
for line in output.split("\n"):
if line.startsWith("== "):
name = line[3 ..< ^3]
elif line == "Doors here lead:":
readDoors = true
elif line == "Items here:":
readItems = true
elif line.strip.len == 0:
readDoors = false
readItems = false
elif line.startsWith("A loud, robotic voice says") and ("lighter" in line or "heavier" in line):
hasAlert = true
else:
if readDoors:
dirs.add line[2 .. ^1]
elif readItems:
items.add line[2 .. ^1]
if items.len == 1: item = items[0]
CellInfo(name: name, dirs: dirs, item: item)
const DIRS = @["north", "east", "south", "west"]
proc rev(dir: string): string {.inline.} =
DIRS[(DIRS.find(dir) + 2) mod 4]
const ignoredItems = @[
# You're launched into space! Bye!
"escape pod",
"infinite loop",
# It is suddenly completely dark! You are eaten by a Grue!
"photons",
# The giant electromagnet is stuck to you. You can't move!!
"giant electromagnet",
# The molten lava is way too hot! You melt!
"molten lava",
].toHashSet
proc collectItems(input: string): seq[string] =
initQueues(2)
let p = newProgram(input, 0, 1)
# (dir, visiting)
var st = newSeq[(string, bool)]()
var firstVisit = true
while true:
discard p.stepOver
let cellInfo = getOutput().getCellInfo
# echo cellInfo
if cellInfo.name == "": break
if cellInfo.item != "":
let item = cellInfo.item
if item notin ignoredItems:
writeInput &"take {item}"
result.add item
discard p.stepOver
discard getOutput()
let fromDir = if st.len > 0: st[^1][0].rev else: ""
for dir in cellInfo.dirs:
if dir == fromDir: continue
st.add (dir, false)
if cellInfo.name == "Security Checkpoint":
if firstVisit:
firstVisit = false
discard st.pop
else:
discard p.stepOver
return
while st.len > 0 and st[^1][1]:
let (dir, _) = st.pop
let ndir = dir.rev
writeInput ndir
if st.len == 0: continue
discard p.stepOver
discard getOutput()
if st.len > 0:
let dir = st[^1][0]
st[^1][1] = true
writeInput dir
when defined(collect):
block:
let input = readFile("input").strip
echo input.collectItems
quit()
proc tryItems(input: string, targetItems: seq[string]) =
initQueues(2)
let p = newProgram(input, 0, 1)
# (dir, visiting)
var st = newSeq[(string, bool)]()
var firstVisit = true
while true:
discard p.stepOver
let output = getOutput()
echo output
let cellInfo = output.getCellInfo
# echo cellInfo
if cellInfo.name == "": break
if cellInfo.item != "":
let item = cellInfo.item
if item in targetItems:
writeInput &"take {item}"
discard p.stepOver
discard getOutput()
let fromDir = if st.len > 0: st[^1][0].rev else: ""
for dir in cellInfo.dirs:
if dir == fromDir: continue
st.add (dir, false)
if cellInfo.name == "Security Checkpoint":
if firstVisit:
firstVisit = false
discard st.pop
else:
discard p.stepOver
discard getOutput()
if hasAlert: return
while st.len > 0 and st[^1][1]:
let (dir, _) = st.pop
let ndir = dir.rev
writeInput ndir
if st.len == 0: continue
discard p.stepOver
discard getOutput()
if st.len > 0:
let dir = st[^1][0]
st[^1][1] = true
writeInput dir
proc part1(input: string) =
let input = readFile("input").strip
let items = input.collectItems
for i in 0 ..< (1 shl items.len):
var targetItems = newSeq[string]()
for j in 0 ..< items.len:
if i.testBit(j): targetItems.add items[j]
hasAlert = false
tryItems(input, targetItems)
if not hasAlert:
echo targetItems
break
when isMainModule and not defined(test):
let input = readFile("input").strip
part1(input)