-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp11.R
More file actions
87 lines (77 loc) · 1.58 KB
/
p11.R
File metadata and controls
87 lines (77 loc) · 1.58 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
library(tidyverse)
library(bit64)
mark_empties <- function(is_empty_row, is_empty_col) {
if (is_empty_row && is_empty_col)
return(1:4)
if (is_empty_row || is_empty_col)
return(1:2)
1
}
grid <- read_csv("inputs/11.txt", col_names = "grid_data")
WIDTH <- grid[1,, drop = TRUE] %>% str_length()
grid_long <-
grid %>%
mutate(
y = seq_len(n()),
.before = grid_data
) %>%
separate_wider_position(
cols = grid_data,
widths = setNames(rep(1, WIDTH), seq_len(WIDTH))
) %>%
pivot_longer(
cols = -y,
names_to = "x",
values_to = "sym",
names_transform = as.integer
) %>%
mutate(
is_gal = sym == "#",
is_void = sym == ".",
) %>%
group_by(x) %>%
mutate(
is_empty_row = all(is_void)
) %>%
group_by(y) %>%
mutate(
is_empty_col = all(is_void)
) %>%
ungroup() %>%
mutate(
gal_id = if_else(
is_gal,
cumsum(is_gal),
NA_integer_
),
empty_flags =
map2(
is_empty_row, is_empty_col,
mark_empties
)
) %>%
unchop(empty_flags) %>%
group_by(x) %>%
mutate(
new_y = seq_len(n())
) %>%
group_by(y) %>%
mutate(
new_x = seq_len(n())
) %>%
ungroup() %>%
filter(is_gal) %>% {
cross_join(
.,
select(., gal_id, new_x, new_y, x, y)
)} %>%
filter(gal_id.x < gal_id.y) %>%
mutate(
orig_dist = abs(x.x - x.y) + abs(y.x - y.y),
dist_p1 = abs(new_x.x - new_x.y) + abs(new_y.x - new_y.y),
dist_diff = dist - orig_dist,
## trick for p2
dist_p2 = orig_dist + dist_diff * (1e6 - 1)
)
10422930
699909023130