-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.R
62 lines (41 loc) · 1.56 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Day 8 - Part 1 ----------------------------------------------------------
# Several approaches possible here, but I took the lazy way
# Preprocessing
input <- readLines("2021/data/day8.txt", warn = F)
input <- strsplit(input, split = " ", fixed = T)
input <- do.call(rbind.data.frame, input)
# Only take whatever is after the | delimiter (always 4 numbers)
# count the number of characters
lengths <- sapply(input[,12:15], nchar)
# It is given that number 1 uses 2 segments, 4 uses 4, 7 uses 3, 8 uses 7
# The segment counts are unique to those numbers
# So simply check which numbers consist of the respective segment counts
sum(lengths %in% c(2,4,3,7))
# Day 8 - Part 2 ----------------------------------------------------------
input <- readLines("2021/data/day8.txt", warn = F)
input <- strsplit(input, split = " ", fixed = T)
input <- do.call(rbind.data.frame, input)
names(input) <- NULL
signal <- input[,1:10]
output <- input[,12:15]
decoder <- function(signal, output) {
for (i in 1:nrow(signal)) {
one <- signal[i,which(nchar(signal[i, ]) == 2)]
four <- signal[i,which(nchar(signal[i, ]) == 4)]
seven <- signal[i,which(nchar(signal[i, ]) == 3)]
eight <- signal[i,which(nchar(signal[i, ]) == 7)]
strsplit(signal[i,], "")
#zero <-
#two <-
#three <-
#five <-
#six <-
#nine <-
translate <- data.frame(var = c(zero, one, two, three, four, five, six, sever, eight, nine),
val = c(0:9))
output[]
}
number <- sum(output)
return(number)
}
# sum output