-
Notifications
You must be signed in to change notification settings - Fork 0
/
day25.R
160 lines (135 loc) · 3.62 KB
/
day25.R
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
# --- Day 25: Cryostasis ---
library(tidyverse)
library(gtools)
library(tictoc)
source("intcode.R")
options(digits = 22, scipen=999)
# read the input
dt <- read_lines("day25.txt") %>%
str_split(",", simplify = TRUE) %>%
as.numeric()
# initialize the computer
ic <- intcode_create(dt) %>%
intcode_run_to_input()
part_one_run <- function(ic, ...) {
commands <- c(...)
# read in the commands passed as ... and execute them
for (cmd in commands) {
ic <- ic %>%
intcode_clear_output() %>%
intcode_add_ascii(cmd) %>%
intcode_run_to_input()
}
ic
}
# function to allow us to play the game
part_one_play <- function(ic, ...) {
ic <- part_one_run(ic, c(...))
while (ic$state != "complete") {
previous_state <- ic
ic <- intcode_ascii_output(ic)
input <- readline()
if (input == "SAVE" || input == "") {
cat("SAVING\n")
return(list(ic = previous_state, commands = commands))
}
commands <- c(commands, input)
ic <- ic %>%
intcode_add_ascii(input) %>%
intcode_run_to_input()
}
ic <- intcode_ascii_output(ic)
return(list(ic = previous_state, commands = commands))
}
# I solved this by experimentation: I searched the 3 directions available at the
# start and picked up every item along the way. The following items ended up
# causing issues though, so I left them out:
# * lava
# * infinite loop
# * escape pod
# * photons
# * giant electromagnet
# we could build a BFS to explore the game, but it's more fun to play :-)
# use part_one_play(ic) to play
# explore south
commands <- list(
s = c("south",
"east",
"take boulder",
"west",
"west",
"east",
"north"),
w = c("west",
"take hypercube",
"south",
"north",
"west",
"take space law space brochure",
"west",
"north",
"take shell",
"west",
"take mug",
"south",
"take festive hat",
"north",
"east",
"south",
"west",
"east",
"east",
"east",
"east"),
e = c("east",
"north",
"west",
"north",
"take whirled peas",
"west",
"west",
"take astronaut ice cream",
"south"))
ic <- ic %>%
# commands are only split for ease of figuring out what was going on when I
# was playing
part_one_run(commands$s,
commands$w,
commands$e)
# find the list of items we selected from the list of commands
items <- flatten_chr(commands) %>%
keep(str_starts, "take") %>%
# remove the "take " from the start
str_sub(6)
# get all the combinations, though we are skipping the combination of drop
# nothing (assume that this wont work!)
cm <- list()
for (i in 1:8) {
cms <- combn(items, i)
for (c in 1:ncol(cms)) {
cm[[length(cm)+1]] <- cms[,c]
}
}
# iterate through all the combinations, but start from the centre and work out
# in both directions
i <- 0
for (v in c(0, rep(1:127, each = 2) * c(1,-1)) + 128) {
i <- i + 1
# show progress
cat("\014(",str_pad(v, 3),") ", str_pad(i, 3), "/",length(cm),"\n", sep = "")
# get the current input
input <- cm[[v]] %>%
map_chr(~paste("drop", .x))
# show what we are doing at this iteration
paste(">", input, collapse = "\n") %>% cat()
ic.t <- part_one_run(ic, input, "south")
# if we have found the solution then the computer will have halted
if (ic.t$state == "complete") {
# we have found the solution
cat("\nFound!\n")
# show the output
ic.t <- intcode_ascii_output(ic.t, FALSE)
# break out of the loop
break()
}
}