-
Notifications
You must be signed in to change notification settings - Fork 1
/
day17.Rmd
94 lines (92 loc) · 2.17 KB
/
day17.Rmd
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
---
title: "--- Day 17: Conway Cubes ---"
author: Fleur Kelpin
date: Dec 17, 2020
output: github_document
---
```{r message=FALSE, warning=FALSE}
library(tidyverse)
n <- 8
steps <- 6
input <- readLines("day17.txt") %>%
str_split("") %>%
unlist() %>%
map_lgl(~ . == "#") %>%
matrix(nrow = n, byrow = T)
```
# Part 1
Starting with your given initial configuration, simulate six cycles. How many
cubes are left in the active state after the sixth cycle?
```{r}
dim <- c(
n + 2 * (steps + 1),
n + 2 * (steps + 1),
1 + 2 * (steps + 1)
)
state <- array(F, dim = dim)
state[7 + 1:n, 7 + 1:n, 8] <- input
ds <- expand.grid(dx = -1:1, dy = -1:1, dz = -1:1)
innerxy <- 2:(dim[[1]] - 1)
innerzw <- 2:(dim[[3]] - 1)
```
```{r}
for (i in 1:6) {
pop <- reduce(1:nrow(ds),
function(arr, index) {
arr + state[
innerxy + ds$dx[[index]],
innerxy + ds$dy[[index]],
innerzw + ds$dz[[index]]
]
},
.init = array(0, dim = c(
2 * steps + n,
2 * steps + n,
2 * steps + 1
))
)
state[innerxy, innerxy, innerzw] <-
(!state[innerxy, innerxy, innerzw] & pop == 3) |
(state[innerxy, innerxy, innerzw] & between(pop, 3, 4))
}
sum(state)
```
# Part 2
For some reason, your simulated results don't match what the experimental energy
source engineers expected. Apparently, the pocket dimension actually has four
spatial dimensions, not three.
```{r}
dim <- c(
n + 2 * (steps + 1),
n + 2 * (steps + 1),
1 + 2 * (steps + 1),
1 + 2 * (steps + 1)
)
state <- array(F, dim = dim)
state[7 + (1:n), 7 + (1:n), 8, 8] <- input
ds <- expand.grid(dx = -1:1, dy = -1:1, dz = -1:1, dw = -1:1)
```
```{r}
for (i in 1:6) {
pop <- reduce(1:nrow(ds),
function(arr, index) {
arr + state[
innerxy + ds$dx[[index]],
innerxy + ds$dy[[index]],
innerzw + ds$dz[[index]],
innerzw + ds$dw[[index]]
]
},
.init = array(0, dim = c(
2 * steps + n,
2 * steps + n,
2 * steps + 1,
2 * steps + 1
))
)
state[innerxy, innerxy, innerzw, innerzw] <-
(!state[innerxy, innerxy, innerzw, innerzw] & pop == 3) |
(state[innerxy, innerxy, innerzw, innerzw] & between(pop, 3, 4))
}
sum(state)
```