-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.R
33 lines (30 loc) · 834 Bytes
/
main.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
library(purrr)
if (!exists("snailfish.parse", mode = "function")) source("snailfish.R")
args <- commandArgs(trailingOnly = TRUE)
if (length(args) != 2) {
print("incorrect number of arguments")
stop()
}
sns <- map(readLines(args[2]), ~ snailfish.parse(.))
if (args[[1]] == "part01") {
res <- reduce(sns[-1], ~ snailfish.add(.x, .y), .init = sns[[1]])
print(snailfish.toString(res))
print(snailfish.magnitude(res))
} else if (args[[1]] == "part02") {
max <- 0
for (i in seq_along(sns)) {
for (j in i:length(sns)) {
val <- snailfish.magnitude(snailfish.add(sns[[i]], sns[[j]]))
if (val > max) {
max <- val
}
val <- snailfish.magnitude(snailfish.add(sns[[j]], sns[[i]]))
if (val > max) {
max <- val
}
}
}
print(max)
} else {
print("unknown subcommand")
}