-
Notifications
You must be signed in to change notification settings - Fork 0
/
d16.R
68 lines (64 loc) · 1.74 KB
/
d16.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
parse_input <- function(f) {
lapply(readLines(f), function(x) strsplit(x, "")[[1]])
}
part1 <- function(d, sx = 1, sy = 1, sd = 1) {
wid <- length(d[[1]])
hei <- length(d)
mem <- matrix(0, nrow = hei, ncol = wid)
dx <- c(1, 0, -1, 0)
dy <- c(0, 1, 0, -1)
dirs <- c(1, 2, 4, 8)
beams <- list(list(x = sx, y = sy, dir = sd))
bno <- 1
beam <- beams[[bno]]
while (TRUE) {
if ((beam$y < 1) || (beam$y > hei) || (beam$x < 1) || (beam$x > hei) ||
(bitwAnd(mem[beam$y, beam$x], dirs[beam$d]) > 0)) {
bno <- bno + 1
if (bno > length(beams)) {
return(sum(mem > 0))
}
beam <- beams[[bno]]
next
}
mem[beam$y, beam$x] <- mem[beam$y, beam$x] + dirs[beam$d]
sym <- d[[beam$y]][beam$x]
if (sym == "\\") beam$dir <- c(2, 1, 4, 3)[beam$dir]
else if (sym == "/") beam$dir <- c(4, 3, 2, 1)[beam$dir]
else if (sym == "-") {
if (beam$dir %in% c(2, 4)) {
beams <- c(beams, list(list(x = beam$x + 1, y = beam$y, dir = 1)))
beam$dir <- 3
}
} else if (sym == "|") {
if (beam$dir %in% c(1, 3)) {
beams <- c(beams, list(list(x = beam$x, y = beam$y + 1, dir = 2)))
beam$dir <- 4
}
}
beam$x <- beam$x + dx[beam$dir]
beam$y <- beam$y + dy[beam$dir]
}
sum(mem > 0)
}
part2 <- function(d) {
wid <- length(d[[1]])
hei <- length(d)
best <- 0
for (i in seq_len(wid)) {
best <- max(best, part1(d, i, 1, 2), part1(d, i, hei, 4))
}
for (j in seq_len(hei)) {
best <- max(best, part1(d, 1, j, 1), part1(d, wid, j, 3))
}
best
}
test <- function() {
d <- parse_input("../inputs/d16-test.txt")
stopifnot(part1(d) == 46)
stopifnot(part2(d) == 51)
}
test()
d <- parse_input("../inputs/d16-input.txt")
part1(d)
part2(d)