File tree Expand file tree Collapse file tree 2 files changed +21
-0
lines changed
main/scala/eu/sim642/adventofcode2018
test/scala/eu/sim642/adventofcode2018 Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,18 @@ object Day8 {
6
6
7
7
def metadataSum (tree : Tree ): Int = tree.childen.map(metadataSum).sum + tree.metadata.sum
8
8
9
+ def value (tree : Tree ): Int = {
10
+ if (tree.childen.isEmpty)
11
+ tree.metadata.sum
12
+ else {
13
+ tree.metadata
14
+ .filter(i => 1 <= i && i <= tree.childen.length)
15
+ .map(i => tree.childen(i - 1 ))
16
+ .map(value)
17
+ .sum
18
+ }
19
+ }
20
+
9
21
def parseTree (seq : List [Int ]): Tree = {
10
22
def helper (seq : List [Int ]): (Tree , List [Int ]) = seq match {
11
23
case childrenCount :: metadataCount :: tl =>
@@ -29,5 +41,6 @@ object Day8 {
29
41
30
42
def main (args : Array [String ]): Unit = {
31
43
println(metadataSum(parseTree(input)))
44
+ println(value(parseTree(input)))
32
45
}
33
46
}
Original file line number Diff line number Diff line change @@ -14,4 +14,12 @@ class Day8Test extends FunSuite {
14
14
test(" Part 1 input answer" ) {
15
15
assert(metadataSum(parseTree(input)) == 46578 )
16
16
}
17
+
18
+ test(" Part 2 examples" ) {
19
+ assert(value(parseTree(exampleInput)) == 66 )
20
+ }
21
+
22
+ test(" Part 2 input answer" ) {
23
+ assert(value(parseTree(input)) == 31251 )
24
+ }
17
25
}
You can’t perform that action at this time.
0 commit comments