File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ package y2016
2
+
3
+ trait Day18 {
4
+
5
+ case class TileRow (input : String ) {
6
+ def nextChar (x : Int ): Char =
7
+ s " ${if (x == 0 ) " ." else input(x - 1 )}${input(x)}${if (x == input.length - 1 ) " ." else input(x + 1 )}" match {
8
+ case " ^^." | " .^^" | " ^.." | " ..^" => '^'
9
+ case _ => '.'
10
+ }
11
+
12
+ def next : TileRow = TileRow (input.indices.map(nextChar).mkString)
13
+ }
14
+
15
+ def solve (input : String , n : Int ): Int =
16
+ Iterator .iterate(TileRow (input), n)(_.next).map(_.input.count(_ == '.' )).sum
17
+ }
Original file line number Diff line number Diff line change
1
+ package y2016
2
+
3
+ import org .scalatest .flatspec .AnyFlatSpec
4
+ import org .scalatest .matchers .should ._
5
+
6
+ class Day18Spec extends AnyFlatSpec with Matchers with Day18 {
7
+ lazy val testInput : String = util.Loader (this , " day18.txt" ).head
8
+ it should " solve part 1" in {
9
+ solve(testInput, 40 ) shouldBe 1978
10
+ }
11
+ it should " solve part 2" in {
12
+ solve(testInput, 400000 ) shouldBe 20003246
13
+ }
14
+ }
You can’t perform that action at this time.
0 commit comments