-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd13.R
44 lines (42 loc) · 1.08 KB
/
d13.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
n <- as.integer(readLines("../AtariBasic/d13/INPUT.TXT"))
solve <- function(part2 = FALSE) {
x <- 1
y <- 1
history <- new.env(hash = TRUE)
assign("1,1", 0, envir = history)
dx <- c(1, 0, -1, 0)
dy <- c(0, 1, 0, -1)
queue <- list(c(1, 1, 0))
head <- 1
tail <- 2
while (TRUE) {
if (head > length(queue)) {
browser()
}
p <- queue[[head]]
head <- head + 1
for (i in 1:4) {
p2 <- p
p2[1] <- p2[1] + dx[i]
p2[2] <- p2[2] + dy[i]
p2[3] <- p2[3] + 1
if ((!part2) && (p2[1] == 31) && (p2[2] == 39)) {
return(p2[3])
}
if ((part2) && (p2[3] == 51)) {
return(length(queue))
}
if ((p2[1] < 0) || (p2[2] < 0) ||
(exists(paste(p2[1], p2[2], sep=","), envir = history))) {
next
}
v <- n + (p2[1]*p2[1]) + (3*p2[1]) + (2*p2[1]*p2[2]) + p2[2] + (p2[2]*p2[2])
if (sum(as.integer(intToBits(v))) %% 2 == 0) {
queue[[tail]] <- p2
tail <- tail + 1
}
assign(paste(p2[1], p2[2], sep=","), 0, envir = history)
}
}
}
c(solve(), solve(TRUE))