-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
333 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
467..114.. | ||
...*...... | ||
..35..633. | ||
......#... | ||
617*...... | ||
.....+.58. | ||
..592..... | ||
......755. | ||
...$.*.... | ||
.664.598.. |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
scala2/src/main/scala/jurisk/adventofcode/y2023/Advent03.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package jurisk.adventofcode.y2023 | ||
|
||
import jurisk.algorithms.pathfinding.ConnectedComponents | ||
import jurisk.geometry.{Coords2D, Field2D} | ||
import jurisk.utils.FileInput._ | ||
|
||
object Advent03 { | ||
import Square._ | ||
|
||
sealed trait Square { | ||
def toChar: Char | ||
} | ||
|
||
object Square { | ||
final case class Digit(value: Int) extends Square { | ||
override def toChar: Char = ('0'.toInt + value).toChar | ||
} | ||
|
||
final case class Symbol(value: Char) extends Square { | ||
override def toChar: Char = value | ||
} | ||
|
||
final case object Empty extends Square { | ||
override def toChar: Char = '.' | ||
} | ||
|
||
val Gear: Symbol = Symbol('*') | ||
|
||
def parse(ch: Char): Square = | ||
ch match { | ||
case '.' => Empty | ||
case d if d.isDigit => Digit(d - '0') | ||
case _ => Symbol(ch) | ||
} | ||
} | ||
|
||
type Parsed = Field2D[Square] | ||
|
||
def parse(input: String): Parsed = | ||
Field2D.parseFromString(input, Square.parse) | ||
|
||
private def extractNumbers( | ||
field: Field2D[Square] | ||
): List[(Int, Set[Coords2D])] = { | ||
def isDigit(square: Square) = square match { | ||
case Digit(_) => true | ||
case _ => false | ||
} | ||
|
||
val digitCoords = field.filterCoordsByValue(isDigit) | ||
|
||
ConnectedComponents | ||
.connectedComponents[Coords2D]( | ||
digitCoords, | ||
field.adjacent8Where(_, isDigit), | ||
) | ||
.map { island => | ||
val number = island.toList | ||
.sortBy(_.x) | ||
.map { c => | ||
field(c).toChar | ||
} | ||
.mkString | ||
.toInt | ||
(number, island) | ||
} | ||
.toList | ||
} | ||
|
||
def part1(data: Parsed): Int = { | ||
val numbers = extractNumbers(data) | ||
val specialSymbolCoords = data.filterCoordsByValue { | ||
case Symbol(_) => true | ||
case _ => false | ||
}.toSet | ||
|
||
numbers.collect { | ||
case (number, coords) | ||
if (coords.flatMap( | ||
data.adjacent8 | ||
) intersect specialSymbolCoords).nonEmpty => | ||
number | ||
}.sum | ||
} | ||
|
||
def part2(data: Parsed): Int = { | ||
val numbers = extractNumbers(data) | ||
val gearSymbolCoords = data.filterCoordsByValue(_ == Gear) | ||
|
||
gearSymbolCoords | ||
.map { gearCoordinates => | ||
val catchment = data.adjacent8(gearCoordinates).toSet | ||
val gearNumbers = numbers | ||
.collect { | ||
case (number, numberSquareCoordinates) | ||
if (catchment intersect numberSquareCoordinates).nonEmpty => | ||
number | ||
} | ||
|
||
gearNumbers | ||
} | ||
.filter(_.length == 2) | ||
.map(_.product) | ||
.sum | ||
} | ||
|
||
def parseFile(fileName: String): Parsed = | ||
parse(readFileText(fileName)) | ||
|
||
def main(args: Array[String]): Unit = { | ||
val realData: Parsed = parseFile("2023/03.txt") | ||
|
||
println(s"Part 1: ${part1(realData)}") | ||
println(s"Part 2: ${part2(realData)}") | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
scala2/src/main/scala/jurisk/algorithms/pathfinding/ConnectedComponents.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package jurisk.algorithms.pathfinding | ||
|
||
object ConnectedComponents { | ||
def connectedComponents[N]( | ||
starts: List[N], | ||
successors: N => List[N], | ||
): Set[Set[N]] = { | ||
var results: Set[Set[N]] = Set.empty | ||
var visited: Set[N] = Set.empty | ||
|
||
starts foreach { n => | ||
if (!visited.contains(n)) { | ||
val island = Bfs.bfsReachable(n, successors).toSet | ||
results += island | ||
visited = visited ++ island | ||
} | ||
} | ||
|
||
results | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
scala2/src/test/scala/jurisk/adventofcode/y2023/Advent03Spec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package jurisk.adventofcode.y2023 | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers._ | ||
import Advent03._ | ||
|
||
class Advent03Spec extends AnyFlatSpec { | ||
"Advent00" should "test part 1" in { | ||
part1(parseFile("2023/03-test.txt")) shouldEqual 4361 | ||
} | ||
|
||
it should "real part 1" in { | ||
part1(parseFile("2023/03.txt")) shouldEqual 533784 | ||
} | ||
|
||
it should "test part 2" in { | ||
part2(parseFile("2023/03-test.txt")) shouldEqual 467835 | ||
} | ||
|
||
it should "real part 2" in { | ||
part2(parseFile("2023/03.txt")) shouldEqual 78826761 | ||
} | ||
} |