-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day5.R
80 lines (57 loc) · 1.93 KB
/
Day5.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
71
72
73
74
75
76
77
78
79
80
### IMPORT + FORMAT DATA ###
setwd("~/Other/Programming/Advent of code/2020/Data")
# read in data and substitute 1s/0s in place of letters
rawdata <- gsub("F|L", 0,
gsub("B|R", 1,
readLines("Day5data.txt")
)
)
n <- nrow(BPs)
BPs <- matrix(0, nrow = n, ncol = 10) # set up matrix for data
for (i in 1:n) {
BPs[i,] <- as.integer(unlist(strsplit(rawdata[i], split = ""))) # split strings and fill matrix
}
### FUNCTIONS ###
BProw <- function(BP) {
# DESCRIPTION #
# Takes a single Boarding Pass and gives the row number
# PARAMETERS #
# BP: Vector of length 10. First 7 digits correspond to Row number, last 3 are column number.
# RETURNS #
# Row number
return(sum(BP[1:7] * 2^(6:0)))
}
BPcol <- function(BP) {
# DESCRIPTION #
# Takes a single Boarding Pass and gives the column number
# PARAMETERS #
# BP: Vector of length 10. First 7 digits correspond to Row number, last 3 are column number.
# RETURNS #
# Column number
return(sum(BP[8:10] * 2^(2:0)))
}
BPseat <- function(r, c) {
# DESCRIPTION #
# Takes a row and column number and returns the seat ID
# PARAMETERS #
# r: row number
# c: column number
# RETURNS #
# Seat ID
return(r*8 + c)
}
### PART 1 ###
seatIDs <- numeric(n)
for (i in 1:n) {
seatIDs[i] <- BPseat(BProw(BPs[i,]),
BPcol(BPs[i,])
)
}
max(seatIDs)
### PART 2 ###
min(seatIDs[as.logical(1 - ((seatIDs+1) %in% seatIDs))]) + 1
# A much-needed explanation:
# The inner part checks which seats have the following seat missing
# It does this by checking which seats have the following seat *present*, and doing as.logical(1 - x)
# There should only be two: the highest seat number, and 1 less than your seat number
# So, take the minimum of these and add 1