-
Notifications
You must be signed in to change notification settings - Fork 0
/
day03.R
53 lines (35 loc) · 1.15 KB
/
day03.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
## PART 1 ----------------------------------------------------------------------
day3 <- readLines("inputs/day03.txt")
tic <- Sys.time()
# split in individual characters
day3 <- strsplit(day3, split = "")
# Split each list into two compartments and compare these
target_items <-
sapply(day3, function(x) {
intersect(
x[1:(length(x)/2)],
x[(length(x)/2 + 1):length(x)]
)
})
# Score items
# Lowercase item types a through z have priorities 1 through 26.
# Uppercase item types A through Z have priorities 27 through 52.
score <- c(letters, LETTERS)
sum(match(target_items, score))
# 7990
Sys.time() - tic
## PART 2 ----------------------------------------------------------------------
rm(list = ls())
day3 <- readLines("inputs/day03.txt")
tic <- Sys.time()
# split in individual characters
day3 <- strsplit(day3, split = "")
# Split list into groups of 3 elfs
day3 <- split(day3, rep(1:100, each = 3))
# Intersection of the three sub-lists (elfs) per list (elf group)
target_items <- sapply(day3, function(x) Reduce(intersect, x))
# Score items
score <- c(letters, LETTERS)
sum(match(target_items, score))
# 2602
Sys.time() - tic