Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jurisk committed Dec 10, 2024
1 parent 61b052c commit f7258a5
Show file tree
Hide file tree
Showing 11 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object Advent24 extends IOApp.Simple {

def locationOf(locationCode: LocationCode): Coords2D =
field
.filterCoordsByValue(_ == Square.Location(locationCode))
.filterCoordsByValue(Square.Location(locationCode))
.singleElementUnsafe

val (a, b) = setOfTwo.tupleInArbitraryOrder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ object Advent17 {
}

private def runWaterDown: State = {
val waterSquares = field.filterCoordsByValue(_ == RunningWater)
val waterSquares = field.filterCoordsByValue(RunningWater)
// Optimisation possibility: run all the way at once, not just one square
val result = waterSquares.foldLeft(field) { case (acc, c) =>
acc.conditionalUpdate(c.S, _ == Empty, RunningWater)
Expand All @@ -57,7 +57,7 @@ object Advent17 {
}

private def runWaterToSides: State = {
val waterSquares = field.filterCoordsByValue(_ == RunningWater)
val waterSquares = field.filterCoordsByValue(RunningWater)
val result = waterSquares.foldLeft(field) { case (acc, c) =>
if (isSolid(acc, c.S)) {
// Optimisation possibility: all the way at once, not just one square
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ object Advent12 {

def parse(data: String): Task = {
val charField = Field2D.parseCharField(data)
val List(start) = charField.filterCoordsByValue(_ == 'S')
val List(end) = charField.filterCoordsByValue(_ == 'E')
val List(start) = charField.filterCoordsByValue('S')
val List(end) = charField.filterCoordsByValue('E')

val field = charField map {
case 'S' => Elevation.Lowest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ object Advent17 {

private def fieldMinY(field: Field2D[Square]): Int =
field
.filterCoordsByValue {
.filterCoordsByPredicate {
case Square.Wall => true
case Square.Rock => true
case Square.Empty => false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ object Advent22 {
val (field, commands) = data

val start = field
.filterCoordsByValue(_ == Open)
.filterCoordsByValue(Open)
.filter(_.y == 0)
.minBy(_.x)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ object Advent23 {
def parse(data: String): State = {
val field = Field2D.parseBooleanField(data)

val elfPositions = field.filterCoordsByValue(_ == true)
val elfPositions = field.filterCoordsByValue(true)
val elves = elfPositions.zipWithIndex.map { case (c, idx) =>
Elf(idx, c)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ object Advent03 {
private def extractNumbers(
field: Field2D[Square]
): Set[(Int, Set[Coords2D])] = {
val digitCoords = field.filterCoordsByValue(_.isDigit)
val digitCoords = field.filterCoordsByPredicate(_.isDigit)

val islands = ConnectedComponents
.connectedComponents[Coords2D](
Expand Down Expand Up @@ -78,7 +78,7 @@ object Advent03 {

def part1(data: Parsed): Int = {
val numbers = extractNumbers(data)
val specialSymbolCoords = data.filterCoordsByValue {
val specialSymbolCoords = data.filterCoordsByPredicate {
case Symbol(_) => true
case _ => false
}.toSet
Expand All @@ -94,7 +94,7 @@ object Advent03 {

def part2(data: Parsed): Int = {
val numbers = extractNumbers(data)
val gearSymbolCoords = data.filterCoordsByValue(_ == Gear)
val gearSymbolCoords = data.filterCoordsByValue(Gear)

gearSymbolCoords
.map { gearCoordinates =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ object Advent10 {
def parse(s: String): Input = {
val chars: Field2D[Char] = Field2D.parseCharField(s)

val animalAt = chars.filterCoordsByValue(_ == 'S').singleResultUnsafe
val animalAt = chars.filterCoordsByValue('S').singleResultUnsafe

val mapping = Map(
'|' -> N_S,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ object Advent11 {
def parse(input: String): Galaxies = {
val field = Field2D.parseBooleanField(input)

val coords = field.filterCoordsByValue(_ == true)
val coords = field.filterCoordsByValue(true)

val results = coords.map { c =>
Coordinates2D[BigInt](c.x, c.y)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object Advent21 {

def parse(input: String): Input = {
val temp = Field2D.parseCharField(input)
val start = temp.filterCoordsByValue(_ == 'S').singleResultUnsafe
val start = temp.filterCoordsByValue('S').singleResultUnsafe

val field = temp.map {
case '.' => false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package jurisk.adventofcode.y2024

import jurisk.geometry.{Coords2D, Field2D}
import jurisk.utils.FileInput._
import jurisk.utils.Parsing.StringOps

object Advent10 {
type Input = Field2D[Int]
Expand Down Expand Up @@ -51,7 +50,7 @@ object Advent10 {
}

private def solve(data: Input, f: (Input, Coords2D) => Int): Int = {
val candidateTrailHeads = data.filterCoordsByValue(_ == Start)
val candidateTrailHeads = data.filterCoordsByValue(Start)
val scores = candidateTrailHeads.map(c => f(data, c))
scores.sum
}
Expand Down

0 comments on commit f7258a5

Please sign in to comment.