-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay9-Encoding_Error
More file actions
43 lines (34 loc) · 1.17 KB
/
Day9-Encoding_Error
File metadata and controls
43 lines (34 loc) · 1.17 KB
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
Codes <- read.table("day09.csv")
Codes <- as.vector(Codes[,1])
# Part 1: Who is the First false number?
ValidateNumber <- function(i = 26, nPreamble = 25) {
CurrentCheck <- i
PossibleSums <- NULL
for (j in (CurrentCheck-nPreamble):(CurrentCheck-1)) {
for (i in (CurrentCheck-nPreamble):(CurrentCheck-1)) {
PossibleSums <- c(PossibleSums, Codes[i]+Codes[j])
}
}
isValid <- Codes[CurrentCheck] %in% PossibleSums
return(isValid)
}
i = 26
CheckPosition <- ValidateNumber(i)
while(CheckPosition) {
CheckPosition <- ValidateNumber(i)
i <- i+1 }
InvalidPosition <- i-1 # invalid position is before the +1
ValidateNumber(InvalidPosition)
Codes[InvalidPosition] # first false
for (i in 26:length(Codes)) {
if(ValidateNumber(i)==FALSE) print(Codes[i])
}
# No other numbers are invalid.
# Part 2: Searching for the invalid sequence (sums the invalid number)
for (k in 1:20) {
for (i in 1:(InvalidPosition-1)) {
if(sum(Codes[i:(i+k)]) == Codes[InvalidPosition]) {
InvalidSequence <- Codes[i:(i+k)]
print(InvalidSequence)
} } }
min(InvalidSequence) + max(InvalidSequence) # Encryption Weakness