-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_05.R
More file actions
92 lines (67 loc) · 2.27 KB
/
day_05.R
File metadata and controls
92 lines (67 loc) · 2.27 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
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
81
82
83
84
85
86
87
88
library(tidyverse)
library(binaryLogic)
# read input, split by empty lines (2 or more consecutive newlines)
seats <- tibble( boardingPass= str_split(
read_file(file="./data/day_05/boarding_passes.txt"),
pattern="\\n{1,}|(\\r\\n){1,}", simplify = FALSE)[[1]])
#seats <- tibble( boardingPass= str_split(
# read_file(file="./data/day_05/test_data.txt"),
# pattern="\\n{1,}|(\\r\\n){1,}", simplify = FALSE)[[1]])
# FBBBFFFLRL
getrow <- function(bp) {
s <- str_split(bp,"")[[1]][1:7]
seatRow <- 0:127
for(i in 1:7) {
numRows <- length(seatRow)
if ( s[i] == "F") {
seatRow <- seatRow[1:(numRows/2)]
} else {
seatRow <- seatRow[(numRows/2+1):numRows]
}
}
return(seatRow)
}
getSeat <- function(bp) {
s <- str_split(bp,"")[[1]][8:10]
seat <- 0:7
for(i in 1:3) {
numSeats <- length(seat)
if ( s[i] == "L") {
seat <- seat[1:(numSeats/2)]
} else {
seat <- seat[(numSeats/2+1):numSeats]
}
}
return(seat)
}
seats <- seats %>%
mutate(row = map (boardingPass, ~ getrow(.))) %>% unnest(cols=c(row)) %>%
mutate(seat = map (boardingPass, ~ getSeat(.))) %>% unnest(cols=c(seat)) %>%
mutate(seat_id = row*8 + seat) %>%
arrange(desc(seat_id))
highest_seat_id <- seats$seat_id[1]
highest_seat_id
# Your puzzle answer was 888.
# The first half of this puzzle is complete! It provides one gold star: *
seats <- seats %>% mutate (gap = seat_id - lead(seat_id,n=1,default = 0))
unique(seats$gap)
# gaps have values of 1,2,89. We are in a gap of 2...
neighbor_seat_id <- (seats %>% filter(gap==2))$seat_id[1]
area <- seats %>% filter(seat_id>neighbor_seat_id-5 & seat_id < neighbor_seat_id+5)
area
myseat <- neighbor_seat_id-1
myseat
# Your puzzle answer was 522.
# Both parts of this puzzle are complete! They provide two gold stars: **
# much better answer from someone else below !
# should have recognized that the boarding pass and seat_id is just a number
# represented in binary digits by B|R as 1 and F|L as 0.
# setdiff was pretty slick too.
library(tidyverse)
q1 <- read_lines("data/day_05/boarding_passes.txt") %>%
str_replace_all("B|R", "1") %>%
str_replace_all("F|L", "0") %>%
strtoi(base = 2) %>%
as_tibble() %>%
summarize(part1 = max(value),
part2 = setdiff(min(value):max(value),value))