-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution5.R
More file actions
70 lines (60 loc) · 1.7 KB
/
solution5.R
File metadata and controls
70 lines (60 loc) · 1.7 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
library(tidyverse)
decode_text <- function(code) {
# decode text "x1,y1 -> x2,y2" into a one-row data frame
coord <- code %>%
str_replace("->", ",") %>%
str_split(",") %>% unlist() %>%
as.numeric() %>%
setNames(c("x1", "y1", "x2", "y2")) %>%
as_tibble_row()
return(coord)
}
make_vent_line <- function(x1, y1, x2, y2) {
# make data frame of vent line points
if (x1 == x2) {
line <- tibble(x = x1, y = seq(y1, y2))
} else if (y1 == y2) {
line <- tibble(x = seq(x1, x2), y = y1)
} else {
stop("invalid line")
}
return(line)
}
# read file
vent0 <- read_delim("5.txt", delim = "\n", col_names = FALSE) %>%
setNames("code") %>%
mutate(id = 1:n()) %>% relocate(id)
# add coordinates and vent lines to vent data frame
vent <- vent0 %>%
mutate(coord = code %>% map(decode_text)) %>%
unnest(coord) %>%
filter(x1 == x2 | y1 == y2) %>%
mutate(line = select(., x1:y2) %>% pmap(make_vent_line))
# unnest the lines
ventlines <- vent %>%
select(id, code, line) %>%
unnest(line)
# calculate overlaps and print
overlaps <- ventlines %>%
group_by(x, y) %>% summarize(ct = n(), .groups = "drop") %>%
filter(ct > 1)
print(overlaps)
# part 2
make_vent_line2 <- function(x1, y1, x2, y2) {
# make data frame of vent line points
line <- tibble(x = seq(x1, x2), y = seq(y1, y2))
return(line)
}
vent <- vent0 %>%
mutate(coord = code %>% map(decode_text)) %>%
unnest(coord) %>%
mutate(line = select(., x1:y2) %>% pmap(make_vent_line2))
# unnest the lines
ventlines <- vent %>%
select(id, code, line) %>%
unnest(line)
# calculate overlaps and print
overlaps <- ventlines %>%
group_by(x, y) %>% summarize(ct = n(), .groups = "drop") %>%
filter(ct > 1)
print(overlaps)