-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc19_day8.R
40 lines (32 loc) · 1.22 KB
/
aoc19_day8.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
## Advent of Code 2019, Day 8
## https://adventofcode.com/2019/day/8
##
## Author: Maarten Demeyer <mpjdem@gmail.com>
## GitHub: https://github.com/mpjdem
## Website: https://www.mpjdem.xyz
## Get the input image
inp <- as.numeric(strsplit(readLines("input/input8.txt"), "")[[1]])
img <- array(inp, dim = c(25, 6, length(inp) / (25 * 6)))
## -- PART 1 --
layer_idx <- which.min(apply(img, 3, function(x) sum(x == 0)))
solution_1 <- sum(img[,,layer_idx] == 1) * sum(img[,,layer_idx] == 2)
cat("Solution to Part 1:", solution_1, "- ")
check_1 <- as.numeric(readLines("output/output8_1.txt"))
if (check_1 == solution_1) cat("correct!\n") else cat("wrong!\n")
## -- PART 2 --
stack_two_layers <- function(top, bottom) {
(top * (top != 2)) + (bottom * (top == 2))
}
img_layers_split <- lapply(seq(dim(img)[3]), function(x) img[,,x])
stacked_img <- Reduce(stack_two_layers, img_layers_split)
stacked_img[stacked_img == 0] <- "."
stacked_img[stacked_img == 1] <- "@"
solution_2 <- apply(stacked_img, 2, paste, collapse = "")
cat("Solution to Part 2:\n")
foo <- sapply(solution_2, function(x) cat(x, "\n"))
check_2 <- readLines("output/output8_2.txt")
if (identical(solution_2, check_2)) {
cat("correct!\n")
} else {
cat("wrong!\n")
}