-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.R
66 lines (61 loc) · 1.6 KB
/
day11.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
library(tidyverse)
input <- tibble(loc = readLines("data/2021/day11.txt")) %>%
mutate(
row = row_number(),
value = str_split(loc, "")
) %>%
unnest(value) %>%
mutate(value = as.numeric(value)) %>%
group_by(row) %>%
mutate(col = row_number()) %>%
ungroup()
adj <- tibble(
x = c(-1, 1, 0, 0),
y = c(0, 0, -1, 1)
) %>%
bind_rows(
tibble(
x = c(-1, -1, 1, 1),
y = c(-1, 1, -1, 1)
)
)
neighbors <- function(df, df2) {
df %>%
crossing(adj) %>%
mutate(
row2 = row + x,
col2 = col + y
) %>%
inner_join(df2, by = c(row2 = "row", col2 = "col"), suffix = c("", "2")) %>%
filter(row != row2 | col != col2) %>%
select(-x, -y)
}
# part 1
# simulate 100 steps of the the dumbo octopuses energy/flashes
# how many total flashes are there after 100 steps?
p1 <- 0
for (i in 1:1000) { # use 1:100 for part 1
input <- input %>%
mutate(
value = value + 1,
flash = value > 9,
has_flashed = flash
)
while (any(input$flash)) {
p1 <- p1 + sum(input$flash) # part 1
input <- input %>%
neighbors(input) %>%
group_by(row, col, value, flash, has_flashed) %>%
summarize(value = first(value) + sum(flash2), .groups = "drop") %>%
mutate(
flash = value > 9 & !has_flashed,
has_flashed = flash | has_flashed
)
# part 2
# what is the first step during which all octopuses flash?
if (all(input$has_flashed | input$flash)) {
stop(paste0("All octopuses have flashed as of step ", i))
}
}
input <- mutate(input, value = ifelse(has_flashed, 0, value))
}