-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.R
70 lines (50 loc) · 1.61 KB
/
day01.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
day01 <- readLines("./puzzle_input/input_day01.txt")
## PART 1 ----------------------------------------------------------------------
start <- Sys.time()
# Get rid of anything that's not a digit
nums <- gsub("[^0-9]", "", day01)
first <- substr(nums, 1, 1)
last <- substr(nums, nchar(nums), nchar(nums))
calib_num <- as.numeric(paste0(first, last))
sum(calib_num)
day01 <- readLines("./puzzle_input/input_day01.txt")
# 53386
Sys.time() - start
## PART 2 ----------------------------------------------------------------------
# As always, the AoC punishes lazy solutions in part 2 ...
start <- Sys.time()
num_lookup <- 1:9
names(num_lookup) <-
c("one", "two", "three", "four", "five", "six", "seven", "eight", "nine")
num_matches_first <-
regexec(paste0("[0-9]|", paste0(names(num_lookup), collapse = "|")), day01)
first <-
substr(
day01,
sapply(num_matches_first, function(x) x),
sapply(num_matches_first, function(x) x + attr(x, "match.length") - 1)
)
# It ain't ugly regex if it works ...
num_matches_last <-
gregexec(
paste0("^.*([0-9]|", paste0(names(num_lookup), collapse = "|"), ").*$"),
day01
)
last <-
substr(
day01,
sapply(num_matches_last, function(x) x[nrow(x), 1]),
sapply(
num_matches_last,
function(x) x[nrow(x), 1] + attr(x, "match.length")[nrow(x), 1] - 1
)
)
# Replace words with numbers
for (i_num in seq_along(num_lookup)) {
first[first == names(num_lookup)[i_num]] <- num_lookup[i_num]
last[last == names(num_lookup)[i_num]] <- num_lookup[i_num]
}
calib_num <- as.numeric(paste0(first, last))
sum(calib_num)
# 53312
Sys.time() - start