-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday24.R
226 lines (192 loc) · 6.61 KB
/
day24.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
library(magrittr)
test_input <- c(
"sesenwnenenewseeswwswswwnenewsewsw",
"neeenesenwnwwswnenewnwwsewnenwseswesw",
"seswneswswsenwwnwse",
"nwnwneseeswswnenewneswwnewseswneseene",
"swweswneswnenwsewnwneneseenw",
"eesenwseswswnenwswnwnwsewwnwsene",
"sewnenenenesenwsewnenwwwse",
"wenwwweseeeweswwwnwwe",
"wsweesenenewnwwnwsenewsenwwsesesenwne",
"neeswseenwwswnwswswnw",
"nenwswwsewswnenenewsenwsenwnesesenew",
"enewnwewneswsewnwswenweswnenwsenwsw",
"sweneswneswneneenwnewenewwneswswnese",
"swwesenesewenwneswnwwneseswwne",
"enesenwswwswneneswsenwnewswseenwsese",
"wnwnesenesenenwwnenwsewesewsesesew",
"nenewswnwewswnenesenwnesewesw",
"eneswnwswnwsenenwnwnwwseeswneewsenese",
"neswnwewnwnwseenwseesewsenwsweewe",
"wseweeenwnesenwwwswnew"
)
real_input <- readLines("./inputs/day24-input.txt")
#- LOGIC ----------------------------------------------------------------------#
const_BLACK <- FALSE
const_WHITE <- TRUE
const_MOVES <- list(
e = c(+2, +0),
ne = c(+1, +1),
nw = c(-1, +1),
w = c(-2, +0),
sw = c(-1, -1),
se = c(+1, -1))
#' parse line of directions
#'
#' returns vector of instructions how to move across hex-grid
parse_line <- function(line) {
if (line == "") character()
else {
ch1 <- substring(line, 1, 1)
ch2 <- substring(line, 1, 2)
instruction <- if (ch2 %in% names(const_MOVES)) ch2 else ch1
remaining <- substring(line, nchar(instruction) + 1)
c(instruction, parse_line(remaining))
}
}
#' coordinate text
coord_txt <- function(tile_coordinate)
paste0("(", paste(tile_coordinate, collapse = ","), ")")
#' parse input
#' returns list of vectors with instructions of e, ne, nw, w, sw, se
parse_input <- function(input) input %>% Map(f = parse_line)
floor_tile_color <- function(floor, tile_coordinate) {
coord_txt <- coord_txt(tile_coordinate)
if(is.null(floor[[coord_txt]])) const_WHITE
else floor[[coord_txt]]$color
}
#' flip tile on the floor
#'
#' returns new configuration of the floor
flip_floor_tile <- function(floor, tile_coordinate) {
coord_txt <- coord_txt(tile_coordinate)
color <- floor_tile_color(floor, tile_coordinate)
# if BLACK then WHITE; if WHITE then BLACK
new_color <- xor(color, TRUE)
tile <- list(
coord = tile_coordinate,
color = new_color)
floor %>% magrittr::inset2(coord_txt, tile)
}
#' identify final coordinate of a tile based on given move instructions
definitive_tile_coordinate <- function(instructions) {
instructions %>%
Map(f = function(x) const_MOVES[[x]]) %>%
Reduce(f = `+`, init = c(0, 0))
}
#' identify adjacent tiles coordinates
adjacent_tile_coordinates <- function(tile_coordinate) {
const_MOVES %>% Map(f = function(x) tile_coordinate + x)
}
#' update color of a floor tile for a new day
daily_tile_update <- function(floor, tile_coordinate) {
color <- floor_tile_color(floor, tile_coordinate)
adjacent_tile_colors <-
adjacent_tile_coordinates(tile_coordinate) %>%
Map(f = function(x) floor_tile_color(floor, x)) %>%
unlist()
nr_adj_black <- sum(adjacent_tile_colors == const_BLACK)
nr_adj_white <- sum(adjacent_tile_colors == const_WHITE)
new_color <-
if (color == const_BLACK & (nr_adj_black == 0 | nr_adj_black > 2))
const_WHITE
else if (color == const_WHITE & nr_adj_black == 2)
const_BLACK
else
color
tile <- list(coord = tile_coordinate, color = new_color)
tile
}
daily_floor_update <- function(floor) {
adjacent_tile_coordinates <-
floor %>%
Reduce(f = function(z, x) c(z, adjacent_tile_coordinates(x$coord)), list())
used_tile_coordibates <- floor %>% Map(f = function(x) x$coord)
all_tile_coordinates <-
c(adjacent_tile_coordinates, used_tile_coordibates) %>%
Reduce(f = function(z, x) {
z %>% magrittr::inset2(coord_txt(x), x)
}, init = list())
new_floor <-
all_tile_coordinates %>%
# calculate tiles for all involved tiles
Map(f = function(tile_coordinate) daily_tile_update(floor, tile_coordinate)) %>%
# make a floor out of tiles
Reduce(f = function(floor, tile) {
floor %>% magrittr::inset2(coord_txt(tile$coord), tile)
}, init = list()) %>%
# keep only black tiles (for performance reasons)
# because white tiles are default
Filter(f = function(tile) tile$color == const_BLACK)
new_floor
}
plot_floor <- function(floor) {
x <- floor %>% Reduce(f = function(z, x) c(z, x$coord[1]), init = integer())
y <- floor %>% Reduce(f = function(z, x) c(z, x$coord[2]), init = integer())
plot(x, y, pch = 16)
}
#- SOLUTION PART 1 ------------------------------------------------------------#
day24_part1_solution <- function(input) {
tiles <-
input %>%
parse_input() %>%
Map(f = definitive_tile_coordinate)
empty_floor <- list()
tiled_floor <-
tiles %>% Reduce(f = flip_floor_tile, init = empty_floor)
plot_floor(tiled_floor)
tiled_floor %>% Filter(f = function(x) x$color == const_BLACK) %>% length()
}
test_output_part1 <- 10
test_result <- day24_part1_solution(test_input)
print(paste(
"test result:", test_result,
"valid:", test_result == test_output_part1))
real_result_part1 <- day24_part1_solution(real_input)
print(format(real_result_part1, scientific = FALSE))
#- SOLUTION PART 2 ------------------------------------------------------------#
day24_part2_solution <- function(input, days) {
tiles <-
input %>%
parse_input() %>%
Map(f = definitive_tile_coordinate)
empty_floor <- list()
tiled_floor <-
tiles %>% Reduce(f = flip_floor_tile, init = empty_floor)
floor_after_days <-
head((1:days), days) %>%
Reduce(f = function(floor, day) {
new_floor <- daily_floor_update(floor)
if (day %% 10 == 0) {
print(paste(Sys.time(), "Day:", day, "Floor_size:", length(floor)))
}
new_floor
}, init = tiled_floor)
plot_floor(floor_after_days)
floor_after_days %>%
Filter(f = function(x) x$color == const_BLACK) %>%
length()
}
test_output_part2 <- 15
test_result <- day24_part2_solution(test_input, 1)
print(paste(
"test result:", test_result,
"valid:", test_result == test_output_part2))
test_output_part2 <- 37
test_result <- day24_part2_solution(test_input, 10)
print(paste(
"test result:", test_result,
"valid:", test_result == test_output_part2))
test_output_part2 <- 132
test_result <- day24_part2_solution(test_input, 20)
print(paste(
"test result:", test_result,
"valid:", test_result == test_output_part2))
test_output_part2 <- 566
test_result <- day24_part2_solution(test_input, 50)
print(paste(
"test result:", test_result,
"valid:", test_result == test_output_part2))
real_result_part2 <- day24_part2_solution(real_input, 100)
print(format(real_result_part2, scientific = FALSE))