Skip to content

Commit 28e1d3e

Browse files
committed
Day 18: R (Part 1)
1 parent 3bb1174 commit 28e1d3e

File tree

6 files changed

+275
-0
lines changed

6 files changed

+275
-0
lines changed

18-r/input/input

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
[[[[9,5],[9,4]],[[6,5],[7,0]]],4]
2+
[[[5,2],[[7,2],1]],[[[7,5],[0,8]],[[6,9],[7,3]]]]
3+
[[[9,7],[0,1]],9]
4+
[1,[[7,3],[[3,7],[3,2]]]]
5+
[[9,[[0,8],7]],[[3,1],[[6,6],[9,0]]]]
6+
[4,[[4,4],[[7,7],1]]]
7+
[[[[6,2],[5,1]],[[3,3],9]],[7,[[5,7],[5,0]]]]
8+
[[[[4,8],[4,9]],[1,[9,3]]],[1,[1,[6,1]]]]
9+
[[[[4,7],[3,4]],[8,3]],[[3,7],[0,[1,8]]]]
10+
[[[6,[4,8]],[4,5]],[4,[1,3]]]
11+
[[[0,7],0],[[6,[1,8]],[9,[7,9]]]]
12+
[[[[4,8],[3,9]],[4,5]],[1,1]]
13+
[[[4,2],[0,[6,7]]],[[[1,8],2],[8,8]]]
14+
[[[[1,1],7],5],[[6,[5,6]],[6,[7,5]]]]
15+
[[[[3,2],5],[[5,3],1]],[[[0,4],[9,6]],9]]
16+
[[6,[7,6]],9]
17+
[[[[4,0],[0,1]],7],1]
18+
[[[[1,3],4],6],[[1,[4,2]],[1,4]]]
19+
[[[[6,9],[4,1]],[[6,3],[0,8]]],[[4,0],[[3,2],[2,9]]]]
20+
[[[3,6],[[2,0],[3,2]]],[2,5]]
21+
[[[[4,3],5],5],[[4,[4,0]],6]]
22+
[[[[4,0],3],[[3,5],8]],[[8,[4,4]],[[9,9],[4,1]]]]
23+
[[[2,7],6],1]
24+
[[[[5,3],[8,4]],[0,0]],4]
25+
[[[0,[8,1]],0],3]
26+
[[[6,5],[8,2]],[[[6,9],[6,1]],[9,9]]]
27+
[0,[[4,9],6]]
28+
[[9,[[9,9],4]],[[[4,7],1],2]]
29+
[[8,0],[[[0,7],6],[[6,4],2]]]
30+
[[1,[[2,4],8]],1]
31+
[[[[1,3],4],[[1,3],0]],[[[1,2],3],2]]
32+
[[[[2,1],2],[5,[2,8]]],[2,[[6,0],2]]]
33+
[[[8,[1,0]],[[6,7],[9,6]]],[[2,[9,7]],5]]
34+
[[[3,[2,0]],[[3,2],[0,0]]],[[[4,6],[9,4]],[[7,8],[5,1]]]]
35+
[[3,[[9,9],[7,2]]],[[1,3],[2,[3,2]]]]
36+
[4,[4,[[9,5],6]]]
37+
[[[[5,7],7],[[3,4],0]],[[9,[8,2]],[2,3]]]
38+
[[[[2,1],[5,7]],4],[[[6,3],8],[[1,6],[5,1]]]]
39+
[[[4,4],[[0,9],[7,8]]],[[2,[2,5]],5]]
40+
[1,[5,[[3,7],[8,2]]]]
41+
[[[[9,5],[8,6]],[5,5]],[[[9,2],8],[[9,3],[3,8]]]]
42+
[0,[[9,5],[[3,7],7]]]
43+
[[[8,[0,4]],[[2,9],6]],[[6,[8,0]],4]]
44+
[[0,[3,5]],[[5,[0,1]],[[3,6],7]]]
45+
[[2,[7,1]],[[[5,0],[7,7]],[[2,3],9]]]
46+
[[5,[9,[3,9]]],[[8,[3,7]],[[7,6],[3,0]]]]
47+
[[[4,[2,5]],5],[3,1]]
48+
[[[[4,3],1],[[5,7],6]],[0,[3,1]]]
49+
[[8,9],[[[0,7],5],[6,[5,7]]]]
50+
[[6,8],[[5,8],[[8,2],[6,0]]]]
51+
[[1,[5,6]],5]
52+
[[[6,1],[9,[1,2]]],1]
53+
[[5,[7,[4,8]]],[[4,[2,9]],5]]
54+
[[[2,2],[[7,1],3]],[[[9,7],[4,6]],[1,[0,1]]]]
55+
[[3,[6,[4,5]]],2]
56+
[[[0,2],[[8,1],[0,6]]],[[7,[9,6]],0]]
57+
[[[[1,0],[5,1]],[[0,6],5]],[[[1,8],8],[[0,2],5]]]
58+
[[6,[[3,6],6]],[[[9,7],[6,4]],[[9,5],1]]]
59+
[[[0,[5,6]],[9,0]],[[2,9],9]]
60+
[1,[[4,[9,3]],0]]
61+
[[1,0],[[1,9],[4,8]]]
62+
[[[9,3],[7,0]],[[[5,1],[3,8]],9]]
63+
[[[3,9],[[5,9],2]],[[7,2],1]]
64+
[[1,[[3,0],[7,6]]],[7,[8,1]]]
65+
[0,[6,[[7,1],[1,1]]]]
66+
[[4,[[5,0],[2,1]]],[[[8,8],[8,1]],7]]
67+
[[[[9,3],[4,3]],4],[7,5]]
68+
[[9,[[7,4],[8,3]]],[[[1,9],7],[[1,6],[3,1]]]]
69+
[[6,9],[5,[0,[5,1]]]]
70+
[[[8,7],3],[[4,8],[0,7]]]
71+
[[[[3,1],2],[[1,6],[4,3]]],[0,6]]
72+
[[5,[[5,4],3]],[[8,8],9]]
73+
[[5,[3,[4,5]]],[[2,[6,0]],[6,1]]]
74+
[[[[9,5],3],6],[[8,[1,9]],[[5,2],5]]]
75+
[[[7,5],[[3,6],4]],[6,[[5,1],[0,1]]]]
76+
[[1,[[4,8],[1,3]]],7]
77+
[[4,[[4,0],5]],[[[6,2],7],[[4,8],[4,9]]]]
78+
[[[[2,3],[0,9]],[7,2]],[4,5]]
79+
[[[[7,7],[8,0]],[7,7]],[[[6,6],[3,2]],[4,[4,3]]]]
80+
[[[[8,7],6],[[5,5],0]],[[6,[7,3]],[[4,1],[1,7]]]]
81+
[[[2,[2,2]],[[5,2],1]],[[9,[9,2]],6]]
82+
[[[[1,7],6],[[8,8],5]],[6,[1,[1,7]]]]
83+
[[[[8,6],[3,2]],[[5,2],[2,0]]],[[[8,7],2],[[5,5],2]]]
84+
[[[8,[9,0]],[[9,5],[7,5]]],[[5,1],[[1,1],[4,6]]]]
85+
[5,[9,[[0,2],7]]]
86+
[8,[[0,[4,9]],[[7,4],9]]]
87+
[[[[2,9],5],[[0,6],[6,6]]],[[0,6],[[4,2],[9,9]]]]
88+
[7,[[[4,3],3],[[5,4],[6,0]]]]
89+
[[0,[8,[1,1]]],5]
90+
[[[1,8],[[4,6],[9,7]]],[[[6,6],[2,6]],[4,3]]]
91+
[[0,[[7,5],[9,9]]],[[9,7],[6,2]]]
92+
[[[9,[3,0]],[[1,4],0]],[[1,1],1]]
93+
[[[0,7],[[3,0],8]],[[6,[8,0]],[[4,5],[4,0]]]]
94+
[[[[2,9],[4,2]],[5,[9,3]]],[4,[2,[3,4]]]]
95+
[[[1,[7,3]],[[5,7],0]],[6,[[6,5],2]]]
96+
[4,5]
97+
[[7,9],[6,[[6,5],[1,0]]]]
98+
[[4,[[7,5],8]],[[4,0],[[6,6],[0,4]]]]
99+
[[[9,[7,7]],[[4,2],7]],4]
100+
[[0,[0,3]],5]

18-r/input/more

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]]
2+
[7,[[[3,7],[4,3]],[[6,3],[8,8]]]]
3+
[[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]]
4+
[[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]]
5+
[7,[5,[[3,8],[1,4]]]]
6+
[[2,[2,2]],[8,[8,1]]]
7+
[2,9]
8+
[1,[[[9,3],9],[[9,0],[0,7]]]]
9+
[[[5,[7,4]],7],1]
10+
[[[[4,2],2],6],[8,7]]

18-r/input/simple

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[[[[4,3],4],4],[7,[[8,4],9]]]
2+
[1,1]

18-r/main.R

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
library(purrr)
2+
if (!exists("snailfish.parse", mode = "function")) source("snailfish.R")
3+
4+
args <- commandArgs(trailingOnly = TRUE)
5+
if (length(args) != 2) {
6+
print("incorrect number of arguments")
7+
stop()
8+
}
9+
10+
sns <- map(readLines(args[2]), ~ snailfish.parse(.))
11+
res <- reduce(sns[-1], ~ snailfish.add(.x, .y), .init = sns[[1]])
12+
13+
print(snailfish.toString(res))
14+
print(snailfish.magnitude(res))

18-r/snailfish.R

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
if (!exists("string.findNext", mode = "function")) source("util.R")
2+
3+
snailfish.parse.helper <- function (input) {
4+
char <- substr(input, 1, 1)
5+
if (char == "[") {
6+
first_res <- snailfish.parse.helper(substr(input, 2, nchar(input)))
7+
first <- first_res[[1]]
8+
input <- substr(first_res[[2]], 2, nchar(first_res[[2]]))
9+
second_res <- snailfish.parse.helper(input)
10+
second <- second_res[[1]]
11+
input <- substr(second_res[[2]], 2, nchar(second_res[[2]]))
12+
list(list(first, second), input)
13+
} else {
14+
end <- string.findNext(input, ",]", nchar(input) + 1)
15+
list(as.numeric(substr(input, 1, end - 1)), substr(input, end, nchar(input)))
16+
}
17+
}
18+
19+
snailfish.parse <- function (input) {
20+
snailfish.parse.helper(input)[[1]]
21+
}
22+
23+
snailfish.reduce.helper.explode.add.left <- function (sn, value) {
24+
if (class(sn) == "numeric") {
25+
sn + value
26+
} else {
27+
list(snailfish.reduce.helper.explode.add.left(sn[[1]], value), sn[[2]])
28+
}
29+
}
30+
31+
snailfish.reduce.helper.explode.add.right <- function (sn, value) {
32+
if (class(sn) == "numeric") {
33+
sn + value
34+
} else {
35+
list(sn[[1]], snailfish.reduce.helper.explode.add.right(sn[[2]], value))
36+
}
37+
}
38+
39+
snailfish.reduce.helper.explode <- function (sn, depth) {
40+
if (class(sn) == "numeric") {
41+
list(sn, list())
42+
} else {
43+
depth <- depth + 1
44+
if (depth == 5) {
45+
list(0, sn)
46+
} else {
47+
left.res <- snailfish.reduce.helper.explode(sn[[1]], depth)
48+
left <- left.res[[1]]
49+
if (length(left.res[[2]]) == 2) {
50+
if (left.res[[2]][[2]] != 0) {
51+
right <- snailfish.reduce.helper.explode.add.left(sn[[2]], left.res[[2]][[2]])
52+
} else {
53+
right <- sn[[2]]
54+
}
55+
list(list(left, right), list(left.res[[2]][[1]], 0))
56+
} else {
57+
right.res <- snailfish.reduce.helper.explode(sn[[2]], depth)
58+
right <- right.res[[1]]
59+
if (length(right.res[[2]]) == 2) {
60+
if (right.res[[2]][[1]] != 0) {
61+
left <- snailfish.reduce.helper.explode.add.right(left, right.res[[2]][[1]])
62+
}
63+
list(list(left, right), list(0, right.res[[2]][[2]]))
64+
} else {
65+
list(sn, list())
66+
}
67+
}
68+
}
69+
}
70+
}
71+
72+
snailfish.reduce.helper.split <- function (sn) {
73+
if (class(sn) == "numeric") {
74+
if (sn >= 10) {
75+
list(list(floor(sn/2), ceiling(sn/2)), TRUE)
76+
} else {
77+
list(sn, FALSE)
78+
}
79+
} else {
80+
left.res <- snailfish.reduce.helper.split(sn[[1]])
81+
if (left.res[[2]]) {
82+
list(list(left.res[[1]], sn[[2]]), TRUE)
83+
} else {
84+
right.res <- snailfish.reduce.helper.split(sn[[2]])
85+
if (right.res[[2]]) {
86+
list(list(sn[[1]], right.res[[1]]), TRUE)
87+
} else {
88+
list(sn, FALSE)
89+
}
90+
}
91+
}
92+
}
93+
94+
snailfish.reduce <- function (sn) {
95+
while (TRUE) {
96+
explode.res <- snailfish.reduce.helper.explode(sn, 0)
97+
sn <- explode.res[[1]]
98+
if (length(explode.res[[2]]) == 2) {
99+
next
100+
}
101+
split.res <- snailfish.reduce.helper.split(sn)
102+
sn <- split.res[[1]]
103+
if (split.res[[2]]) {
104+
next
105+
}
106+
break
107+
}
108+
sn
109+
}
110+
111+
snailfish.add <- function (sn1, sn2) {
112+
snailfish.reduce(list(sn1, sn2))
113+
}
114+
115+
snailfish.toString <- function (sn) {
116+
if (class(sn) == "numeric") {
117+
format(sn)
118+
} else {
119+
paste0("[", snailfish.toString(sn[[1]]), ",", snailfish.toString(sn[[2]]), "]")
120+
}
121+
}
122+
123+
snailfish.magnitude <- function (sn) {
124+
if (class(sn) == "numeric") {
125+
sn
126+
} else {
127+
3 * snailfish.magnitude(sn[[1]]) + 2 * snailfish.magnitude(sn[[2]])
128+
}
129+
}

18-r/util.R

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
string.findNext <- function (input, needle, default) {
2+
needle <- strsplit(needle, "")[[1]]
3+
chars <- strsplit(input, "")[[1]]
4+
result <- default
5+
for (i in seq_along(chars)) {
6+
if (chars[i] %in% needle) {
7+
result <- i
8+
break
9+
}
10+
}
11+
result
12+
}
13+
14+
list.push <- function (l, ele) {
15+
append(l, ele)
16+
}
17+
18+
list.pop <- function (l) {
19+
l[-length(l)]
20+
}

0 commit comments

Comments
 (0)