-
Notifications
You must be signed in to change notification settings - Fork 0
/
d08.R
53 lines (47 loc) · 1.25 KB
/
d08.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
parse <- function(file) {
d <- lapply(readLines(file), function(x) utf8ToInt(x) - 48)
matrix(data = unlist(d), nrow = length(d), byrow = TRUE)
}
`%||%` <- function(a, b) {
if (is.na(a)) b else a
}
part1 <- function(d, vis = 0, size = dim(d)[1], range = 2:(size - 1)) {
for (j in range) {
for (i in range) {
me <- d[j, i]
if ((all(d[1:(j - 1), i] < me)) ||
(all(d[(j + 1):size, i] < me)) ||
(all(d[j, 1:(i - 1)] < me)) ||
(all(d[j, (i + 1):size] < me))) vis <- vis + 1
}
}
vis + (size * 4) - 4
}
loop <- function(d, limit, i = 1, ld = length(d)) {
while (d[i] < limit) {
i <- i + 1
if (i > ld) return(NA)
}
i
}
part2 <- function(d, size = dim(d)[1], best = 0, range = 2:(size - 1)) {
for (j in range) {
for (i in range) {
me <- d[j, i]
best <- max(best,
loop(d[j, (i - 1):1], me) %||% (i - 1) *
loop(d[j, (i + 1):size], me) %||% (size - i) *
loop(d[(j - 1):1, i], me) %||% (j - 1) *
loop(d[(j + 1):size, i], me) %||% (size - j))
}
}
best
}
test <- function(input = parse("../inputs/d08-test.txt")) {
stopifnot(part1(input) == 21)
stopifnot(part2(input) == 8)
}
test()
input <- parse("../inputs/d08-input.txt")
part1(input)
part2(input)