Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jurisk committed Dec 13, 2023
1 parent 8cd4119 commit cafa571
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object Advent24 extends IOApp.Simple {
setOfTwo: SetOfTwo[LocationCode]
): Option[Int] = {
def isPassable(c: Coords2D): Boolean =
field.at(c).exists(_ != Square.Wall)
field.atOrElse(c, Square.Wall) != Square.Wall

def locationOf(locationCode: LocationCode): Coords2D =
field
Expand Down
16 changes: 9 additions & 7 deletions scala2/src/main/scala/jurisk/adventofcode/y2018/Advent17.scala
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ object Advent17 {
val (result, _) = Simulation.runUntilStableState(State(field)) {
case (state, iteration) =>
val newState = state.next
if (iteration % 100 == 0) {
if (iteration % 1_000 == 0) {
printField("Next", newState.field)
}
newState
Expand Down Expand Up @@ -218,17 +218,19 @@ object Advent17 {
def part2(field: Field2D[Square]): Int =
squareCount(field, Set(StandingWater))

def parseFile(fileName: String): Field2D[Square] = parse(
readFileText(fileName)
)

def main(args: Array[String]): Unit = {
val realData = readFileText("2018/17.txt")
val testData = readFileText("2018/17-test.txt")
val realData = parseFile("2018/17.txt")
val testData = parseFile("2018/17-test.txt")

val testInput = parse(testData)
val testResult = simulate(testInput)
val testResult = simulate(testData)
part1(testResult) shouldEqual 57
part2(testResult) shouldEqual 29

val realInput = parse(realData)
val realResult = simulate(realInput)
val realResult = simulate(realData)
part1(realResult) shouldEqual 33004
part2(realResult) shouldEqual 23294
}
Expand Down
23 changes: 11 additions & 12 deletions scala2/src/main/scala/jurisk/adventofcode/y2023/Advent03.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ object Advent03 {

sealed trait Square {
def toChar: Char
def isDigit: Boolean
}

object Square {
final case class Digit(value: Int) extends Square {
override def toChar: Char = ('0'.toInt + value).toChar
def toChar: Char = ('0'.toInt + value).toChar
def isDigit: Boolean = true
}

final case class Symbol(value: Char) extends Square {
override def toChar: Char = value
def toChar: Char = value
def isDigit: Boolean = false
}

case object Empty extends Square {
override def toChar: Char = '.'
def toChar: Char = '.'
def isDigit: Boolean = false
}

val Gear: Symbol = Symbol('*')
Expand All @@ -44,20 +48,15 @@ object Advent03 {
private def extractNumbers(
field: Field2D[Square]
): Set[(Int, Set[Coords2D])] = {
def isDigit(square: Square) = square match {
case Digit(_) => true
case _ => false
}

val digitCoords = field.filterCoordsByValue(isDigit)
val digitCoords = field.filterCoordsByValue(_.isDigit)

val islands = ConnectedComponents
.connectedComponents[Coords2D](
digitCoords,
x => {
def helper(d: Direction2D): List[Coords2D] = {
val n = x + d
if (field.at(n).exists(isDigit)) n :: Nil else Nil
if (field.atOrElse(n, Empty).isDigit) n :: Nil else Nil
}

helper(Direction2D.W) ::: helper(Direction2D.E) ::: Nil
Expand All @@ -67,8 +66,8 @@ object Advent03 {
islands.map { island =>
val number = island.toList
.sortBy(_.x)
.flatMap { c =>
field.at(c)
.map { c =>
field.atOrElse(c, Empty)
}
.map(_.toChar)
.mkString
Expand Down
27 changes: 27 additions & 0 deletions scala2/src/test/scala/jurisk/adventofcode/y2018/Advent17Spec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package jurisk.adventofcode.y2018

import org.scalatest.freespec.AnyFreeSpec
import Advent17._
import org.scalatest.matchers.should.Matchers._

class Advent17Spec extends AnyFreeSpec {
"part 1" - {
"test" in {
part1(simulate(parseFile("2018/17-test.txt"))) shouldEqual 57
}

"real" ignore {
part1(simulate(parseFile("2018/17.txt"))) shouldEqual 33004
}
}

"part 2" - {
"test" in {
part2(simulate(parseFile("2018/17-test.txt"))) shouldEqual 29
}

"real" ignore {
part2(simulate(parseFile("2018/17.txt"))) shouldEqual 23294
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ class AllSolutionSpec extends AnyFlatSpec {
Advent16.main(Array.empty)
}

"Advent17" should "work" ignore {
Advent17.main(Array.empty)
}

"Advent18" should "work" in {
Advent18.main(Array.empty)
}
Expand Down

0 comments on commit cafa571

Please sign in to comment.