Skip to content

Commit c4a2a56

Browse files
author
Liz Fong-Jones
committed
add day25 solution.
1 parent 153495b commit c4a2a56

File tree

1 file changed

+84
-33
lines changed

1 file changed

+84
-33
lines changed

2019/day25.go

Lines changed: 84 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import (
66
"fmt"
77
"github.com/lizthegrey/adventofcode/2019/intcode"
88
"os"
9+
"sort"
910
"strings"
1011
"sync"
1112
)
1213

1314
var inputFile = flag.String("inputFile", "inputs/day25.input", "Relative file path to use as input.")
15+
var debug = flag.Bool("debug", true, "Whether to print debug output.")
1416

1517
var disallow = []string{
1618
"giant electromagnet",
@@ -60,46 +62,61 @@ func main() {
6062
return
6163
}
6264

65+
inputs := 0
66+
6367
input := make(chan int, 1)
6468
output, done := tape.Process(input)
6569
lines := make(chan string, 50)
6670
driver := make(chan string)
71+
reallyFinished := make(chan bool)
6772

6873
go func() {
6974
line := make([]byte, 0)
7075
for c := range output {
71-
if c > 255 {
72-
fmt.Println(c)
73-
return
76+
if *debug {
77+
fmt.Printf("%c", c)
7478
}
75-
fmt.Printf("%c", c)
7679

7780
if c == '\n' {
81+
if len(line) > 0 && line[0] == '"' {
82+
fmt.Println(string(line))
83+
fmt.Printf("Completed with %d inputs.\n", inputs)
84+
break
85+
}
7886
lines <- string(line)
7987
line = make([]byte, 0)
8088
} else {
8189
line = append(line, byte(c))
8290
}
8391
}
84-
fmt.Println()
92+
if *debug {
93+
fmt.Println()
94+
}
95+
<-done
96+
reallyFinished <- true
8597
}()
8698

87-
go func() {
88-
reader := bufio.NewReader(os.Stdin)
89-
for {
90-
line, err := reader.ReadString('\n')
91-
if line == "\n" || err != nil {
92-
return
93-
}
94-
for _, r := range line {
95-
input <- int(r)
99+
if *debug {
100+
go func() {
101+
reader := bufio.NewReader(os.Stdin)
102+
for {
103+
line, err := reader.ReadString('\n')
104+
if line == "\n" || err != nil {
105+
return
106+
}
107+
for _, r := range line {
108+
input <- int(r)
109+
}
96110
}
97-
}
98-
}()
111+
}()
112+
}
99113

100114
go func() {
101115
for l := range driver {
102-
fmt.Printf("[Automated]: %s\n", l)
116+
inputs++
117+
if *debug {
118+
fmt.Printf("[Automated]: %s\n", l)
119+
}
103120
for _, r := range l {
104121
input <- int(r)
105122
}
@@ -112,10 +129,13 @@ func main() {
112129
go func() {
113130
var loc Loc
114131
rooms := make(Maze)
115-
var items []string
132+
seenItems := make(map[string]bool)
133+
var allItems, items []string
116134
var outbound []Direction
117135
var q Queue
118136
var arrived *Exit
137+
checkpointTry := 0
138+
119139
parseMode := 0
120140
parse:
121141
for l := range lines {
@@ -142,27 +162,28 @@ func main() {
142162
}
143163
parseMode = 0
144164
} else if l[0] == '-' {
145-
// This is a list.
146-
// Doors here lead:
147-
// - north
148-
//
149-
// Items here:
150-
// - cake
151165
if parseMode == 1 {
152166
outbound = append(outbound, Direction(l[2:]))
153167
} else if parseMode == 2 {
154168
items = append(items, l[2:])
169+
} else if parseMode == 3 {
170+
// Do nothing.
155171
} else {
156172
fmt.Println("Didn't know what to do with a list outside parse mode.")
157173
}
158174
} else if l[len(l)-1] == ':' {
159175
// This is a menu ("Doors here lead:" or "Items here:").
160-
keyword := strings.Split(l, " ")[0]
176+
keywords := strings.Split(l[:len(l)-1], " ")
177+
keyword := keywords[0]
161178
switch keyword {
162179
case "Doors":
163180
parseMode = 1
164181
case "Items":
165-
parseMode = 2
182+
if keywords[1] == "here" {
183+
parseMode = 2
184+
} else {
185+
parseMode = 3
186+
}
166187
default:
167188
fmt.Println("Unknown menu type.")
168189
}
@@ -184,6 +205,10 @@ func main() {
184205
}
185206
if !skip {
186207
driver <- fmt.Sprintf("take %s", item)
208+
if !seenItems[item] {
209+
allItems = append(allItems, item)
210+
seenItems[item] = true
211+
}
187212
continue parse
188213
}
189214
}
@@ -197,37 +222,54 @@ func main() {
197222
}
198223
// Pick a direction to move.
199224
next := nextDirection(&q, loc, rooms)
200-
if next == q.target.dir && loc == q.target.src {
225+
if q.target != nil && next == q.target.dir && loc == q.target.src {
201226
arrived = q.target
202227
q.target = nil
203228
}
204229
if next == "dance" {
205-
done <- true
230+
// Now we can move onto phase 2 and access the security checkpoint.
231+
next = "west"
232+
sort.Strings(allItems)
233+
for _, v := range itemsToDrop(allItems, checkpointTry) {
234+
driver <- fmt.Sprintf("drop %s", v)
235+
<-lines
236+
<-lines
237+
<-lines
238+
<-lines
239+
}
240+
checkpointTry++
206241
}
207242
driver <- string(next)
208243
} else {
209-
fmt.Println("Got into somewhere we can't leave")
244+
if *debug {
245+
fmt.Println("Got into somewhere we can't leave")
246+
}
210247
}
211248
} else {
212249
// This is presumed to be flavor text or a response to command.
213250
// Just let the program print.
214251
}
215252
}
216253
}()
217-
<-done
254+
<-reallyFinished
218255
}
219256

220257
type Queue struct {
221258
mtx sync.Mutex
222259
list []Exit
223260
target *Exit
261+
sensor *Exit
224262
}
225263

226264
func notifyRoom(q *Queue, ex Exit, rooms Maze) {
227265
q.mtx.Lock()
266+
defer q.mtx.Unlock()
228267
q.list = append(q.list, ex)
268+
if ex.src == "Security Checkpoint" {
269+
q.sensor = &ex
270+
return
271+
}
229272
rooms[ex.src][ex.dir] = ex
230-
q.mtx.Unlock()
231273
}
232274

233275
func nextDirection(q *Queue, loc Loc, rooms Maze) Direction {
@@ -243,7 +285,6 @@ func nextDirection(q *Queue, loc Loc, rooms Maze) Direction {
243285
}
244286
// We've reached our destination and need a new destination.
245287
if len(q.list) == 0 {
246-
fmt.Println("Nothing left to explore. Result:")
247288
return "dance"
248289
}
249290

@@ -269,7 +310,6 @@ func nextDirection(q *Queue, loc Loc, rooms Maze) Direction {
269310

270311
func bfs(src Loc, dst Exit, rooms Maze) []Direction {
271312
// Perform a breadth-first search.
272-
273313
shortest := map[Loc][]Direction{
274314
src: []Direction{},
275315
}
@@ -299,3 +339,14 @@ func bfs(src Loc, dst Exit, rooms Maze) []Direction {
299339
worklist = worklist[1:]
300340
}
301341
}
342+
343+
func itemsToDrop(all []string, try int) []string {
344+
var ret []string
345+
for k := range all {
346+
if try%2 == 1 {
347+
ret = append(ret, all[k])
348+
}
349+
try >>= 1
350+
}
351+
return ret
352+
}

0 commit comments

Comments
 (0)