Skip to content

Commit 0423093

Browse files
committed
2016.18
1 parent 4fd61b6 commit 0423093

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/main/scala/y2016/Day18.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}

src/test/scala/y2016/Day18Spec.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy