-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.R
31 lines (27 loc) · 771 Bytes
/
11.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
library(tidyverse)
input <- read_lines("input11") %>%
str_split("") %>%
do.call(what = rbind)
expansion <- 1000000
for (i in 1:nrow(input))
if (all(input[i,] == "."))
input[i,] <- expansion
for (i in 1:ncol(input))
if (all(input[,i] == "." | input[,i] == expansion))
input[,i] <- expansion
stars <- which(input == "#")
res <- 0
for (star1 in stars) {
x1 <- (star1 - 1) %% nrow(input) + 1
y1 <- (star1 - 1) %/% nrow(input) + 1
for (star2 in stars) {
if (star1 <= star2)
next
x2 <- (star2 - 1) %% nrow(input) + 1
y2 <- (star2 - 1) %/% nrow(input) + 1
dist <- abs(x1 - x2) + abs(y1 - y2) + (expansion - 1) *
(sum(input[x1:x2,y1] == expansion) + sum(input[x1, y1:y2] == expansion))
res <- res + dist
}
}
print(res)