-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.R
70 lines (52 loc) · 1.73 KB
/
day02.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
69
70
day02 <- readLines("puzzle_input/input_day02.txt")
process_colour <- function(game, colour_name) {
matches <- regexec(paste0("[0-9]+ ", colour_name), game)
colours <- substr(
game,
sapply(matches, function(x) x),
sapply(matches, function(x) x) +
sapply(matches, function(x) attr(x, "match.length") - 1)
)
colours <- as.numeric(gsub("[^0-9]+", "", colours))
colours <- ifelse(is.na(colours), 0, colours)
return(colours)
}
## PART 1 ----------------------------------------------------------------------
start <- Sys.time()
# Split into games and draws
games <-
lapply(
strsplit(day02, ": "), function(x) {
game_id <- as.numeric(gsub("[^0-9]", "", x[1]))
games <- trimws(unlist(strsplit(x[2], ";")))
}
)
# Extract colour counts per game x draw
colour_counts <- vector("list", length(games))
for (i_game in seq_along(games)) {
reds <- process_colour(games[[i_game]], "red")
blues <- process_colour(games[[i_game]], "blue")
greens <- process_colour(games[[i_game]], "green")
colour_counts[[i_game]] <- list(red = reds, blue = blues, green = greens)
}
# Games possible with
# 12 red cubes, 13 green cubes, and 14 blue cubes
possible_games <-
which(
sapply(colour_counts, function(x) all(x$red <= 12)) &
sapply(colour_counts, function(x) all(x$green <= 13)) &
sapply(colour_counts, function(x) all(x$blue <= 14))
)
sum(possible_games)
# 2348
Sys.time() - start
## PART 2 ----------------------------------------------------------------------
start <- Sys.time()
# Minimum set of cubes: Product of the maximum number for each colour
set_powers <-
sapply(colour_counts, function(x) {
prod(sapply(x, function(y) max(y)))
})
sum(set_powers)
# 76008
Sys.time() - start