-
Notifications
You must be signed in to change notification settings - Fork 0
/
d14.R
50 lines (45 loc) · 1.24 KB
/
d14.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
salt <- readLines("../AtariBasic/d14/INPUT.TXT")
solve <- function(part2 = FALSE) {
hash <- function(x, part2) {
md5 <- digest::digest(x, algo = "md5", serialize = FALSE)
if (part2) {
for (i in 1:2016) {
md5 <- digest::digest(md5, algo = "md5", serialize = FALSE)
}
}
md5
}
cache <- new.env(hash = TRUE)
i <- 0
key <- 0
while (TRUE) {
code <- paste0(salt, i)
md5 <- ifelse(exists(code, envir = cache), get0(code, envir = cache),
hash(code, part2))
digs <- c(strsplit(md5, "")[[1]], "z", "z")
trip <- digs[1:(length(digs)-2)] == digs[2:(length(digs)-1)] &
digs[2:(length(digs)-1)] == digs[3:(length(digs))]
if (any(trip)) {
dig <- digs[which(trip)[1]]
pent <- paste0(dig, dig, dig, dig, dig)
j <- i + 1
while (j <= i + 1000) {
code <- paste0(salt, j)
if (!exists(code, envir = cache)) {
assign(code, hash(code, part2), envir = cache)
}
md5 <- get0(code, envir = cache)
if (grepl(pent, md5)) {
key <- key + 1
if (key == 64) {
return(i)
}
break
}
j <- j + 1
}
}
i <- i + 1
}
}
c(solve(), solve(TRUE))