-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03.R
36 lines (32 loc) · 936 Bytes
/
03.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
d <- as.matrix(read.fwf("03_data.txt", widths = rep(1, nchar(readLines("03_data.txt")[1]))))
#part 1 -----------------------------------------------------------------------
toInt <- function(x) strtoi(paste(x, collapse = ""), base = 2)
res1 <- as.integer(colSums(d) > nrow(d)/2)
res2 <- as.integer(res1 == 0)
toInt(res1) * toInt(res2)
#[1] 3687446
#part 2 -----------------------------------------------------------------------
res1 = d
ix = 1
while (nrow(res1) > 1) {
n = sum(res1[, ix] == 1)
if(n >= nrow(res1) - n){
res1 <- res1[ res1[, ix ] == 1, , drop = FALSE]
} else {
res1 <- res1[ res1[, ix ] == 0, , drop = FALSE]
}
ix <- ix + 1
}
res2 = d
ix = 1
while (nrow(res2) > 1) {
n = sum(res2[, ix] == 0)
if(n <= (nrow(res2) - n)){
res2 <- res2[ res2[, ix ] == 0, , drop = FALSE]
} else {
res2 <- res2[ res2[, ix ] == 1, , drop = FALSE]
}
ix <- ix + 1
}
toInt(res1) * toInt(res2)
# [1] 4406844