Skip to content

Commit

Permalink
2023-08
Browse files Browse the repository at this point in the history
  • Loading branch information
jurisk committed Dec 8, 2023
1 parent 18ebc21 commit befc661
Show file tree
Hide file tree
Showing 23 changed files with 57 additions and 48 deletions.
2 changes: 1 addition & 1 deletion scala2/src/main/scala/jurisk/adventofcode/Advent00.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package jurisk.adventofcode
import cats.implicits._
import jurisk.utils.FileInput._
import jurisk.utils.Parsing.StringOps
import jurisk.utils.Utils.IterableOps
import jurisk.utils.CollectionOps.IterableOps
import org.scalatest.matchers.should.Matchers._

object Advent00 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import jurisk.geometry.Coords2D
import jurisk.geometry.Field2D
import jurisk.geometry.Field2D.parseFromLines
import jurisk.utils.FileInputIO.readFileLines
import jurisk.utils.Utils.IterableOps
import jurisk.utils.CollectionOps.IterableOps
import org.scalatest.matchers.should.Matchers._

object Advent24 extends IOApp.Simple {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package jurisk.adventofcode.y2018

import jurisk.utils.FileInput.readFileLines
import jurisk.utils.Utils.IterableOps
import jurisk.utils.CollectionOps.IterableOps
import org.scalatest.matchers.should.Matchers._

object Advent02 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cats.implicits._
import jurisk.geometry.Area2D
import jurisk.geometry.Coords2D
import jurisk.utils.FileInput.parseFileLines
import jurisk.utils.Utils.IterableOps
import jurisk.utils.CollectionOps.IterableOps
import org.scalatest.matchers.should.Matchers._

object Advent03 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cats.implicits._
import jurisk.adventofcode.y2018.Advent04.Entry.BeginsShift
import jurisk.utils.FileInput.parseFileLines
import jurisk.utils.Parsing.splitIntoSections
import jurisk.utils.Utils.IterableOps
import jurisk.utils.CollectionOps.IterableOps
import org.scalatest.matchers.should.Matchers._

import java.time.Duration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cats.implicits._
import jurisk.geometry.Coords2D
import jurisk.utils.FileInput.parseFileLines
import jurisk.utils.Parsing.StringOps
import jurisk.utils.Utils.IterableOps
import jurisk.utils.CollectionOps.IterableOps
import org.scalatest.matchers.should.Matchers._

object Advent06 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package jurisk.adventofcode.y2018

import cats.implicits._
import jurisk.utils.FileInput.parseFileLines
import jurisk.utils.Utils.IterableOps
import jurisk.utils.CollectionOps.IterableOps
import org.scalatest.matchers.should.Matchers._

import scala.annotation.tailrec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package jurisk.adventofcode.y2018

import jurisk.utils.FileInput.readSingleFileLine
import jurisk.utils.Parsing.StringOps
import jurisk.utils.Utils.IterableOps
import jurisk.utils.CollectionOps.IterableOps
import org.scalatest.matchers.should.Matchers._

object Advent08 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cats.implicits._
import jurisk.utils.FileInput._
import jurisk.utils.Parsing.StringOps
import jurisk.utils.Simulation
import jurisk.utils.Utils.IterableOps
import jurisk.utils.CollectionOps.IterableOps
import org.scalatest.matchers.should.Matchers._

import scala.annotation.tailrec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import jurisk.geometry.Rotation._
import jurisk.geometry._
import jurisk.utils.FileInput._
import jurisk.utils.Simulation
import jurisk.utils.Utils.IterableOps
import jurisk.utils.CollectionOps.IterableOps
import org.scalatest.matchers.should.Matchers._

import scala.annotation.tailrec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package jurisk.adventofcode.y2018

import jurisk.utils.FileInput._
import jurisk.utils.Parsing.StringOps
import jurisk.utils.Utils.IterableOps
import jurisk.utils.CollectionOps.IterableOps
import org.scalatest.matchers.should.Matchers._

import scala.annotation.tailrec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import jurisk.geometry.Field2D
import jurisk.utils.FileInput._
import jurisk.utils.Parsing.StringOps
import jurisk.utils.Simulation
import jurisk.utils.Utils.IterableOps
import jurisk.utils.CollectionOps.IterableOps
import org.scalatest.matchers.should.Matchers._

object Advent18 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import jurisk.geometry.Area2D
import jurisk.geometry.Coords2D
import jurisk.utils.Memoize.memoize
import jurisk.utils.Parsing.StringOps
import jurisk.utils.Utils.IterableOps
import jurisk.utils.CollectionOps.IterableOps
import org.scalatest.matchers.should.Matchers._

object Advent22 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package jurisk.adventofcode.y2022

import jurisk.utils.FileInput.parseFileLines
import jurisk.utils.Utils.IterableOps
import jurisk.utils.CollectionOps.IterableOps
import org.scalatest.matchers.should.Matchers._

object Advent03 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package jurisk.adventofcode.y2022

import jurisk.utils.FileInput.readLineGroups
import jurisk.utils.Utils.IterableOps
import jurisk.utils.CollectionOps.IterableOps
import org.scalatest.matchers.should.Matchers._

import scala.collection.SortedMap
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package jurisk.adventofcode.y2022

import jurisk.utils.FileInput.readSingleFileLine
import jurisk.utils.Utils.IterableOps
import jurisk.utils.CollectionOps.IterableOps
import org.scalatest.matchers.should.Matchers._

object Advent06 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cats.implicits._
import jurisk.utils.FileInput._
import jurisk.utils.Parsing.StringOps
import jurisk.utils.Simulation
import jurisk.utils.Utils.IterableOps
import jurisk.utils.CollectionOps.IterableOps
import org.scalatest.matchers.should.Matchers._

import scala.annotation.tailrec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import jurisk.geometry.Rotation
import jurisk.geometry.Rotation.TurnAround
import jurisk.utils.FileInput._
import jurisk.utils.Parsing.StringOps
import jurisk.utils.Utils.IterableOps
import jurisk.utils.CollectionOps.IterableOps
import org.scalatest.matchers.should.Matchers._

import scala.annotation.tailrec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import jurisk.geometry.Direction2D._
import jurisk.geometry.Field2D
import jurisk.utils.FileInput._
import jurisk.utils.Simulation
import jurisk.utils.Utils.IterableOps
import jurisk.utils.CollectionOps.IterableOps
import org.scalatest.matchers.should.Matchers._

import scala.collection.immutable.ArraySeq
Expand Down
51 changes: 25 additions & 26 deletions scala2/src/main/scala/jurisk/adventofcode/y2023/Advent08.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import jurisk.math.lcmMany
import jurisk.utils.FileInput._
import jurisk.utils.Parsing.StringOps
import jurisk.utils.Simulation
import jurisk.utils.CollectionOps.IndexedSeqOps

object Advent08 {
sealed trait Instruction
Expand All @@ -20,7 +21,7 @@ object Advent08 {
}
}

private type NodeId = Int
private type NodeId = Int
private type NodeName = String

final case class Mapping(
Expand All @@ -36,14 +37,16 @@ object Advent08 {
}

final case class Input(
instructions: IndexedSeq[Instruction],
mappings: IndexedSeq[Mapping],
private val instructions: IndexedSeq[Instruction],
private val mappings: IndexedSeq[Mapping],
) {
def instructionAtStep(step: Long): Instruction = instructions(
(step % instructions.length).toInt
)
def instructionAtStep(step: Long): Instruction =
instructions.atIndexWithWraparound(step)

def findNodesByNameFilter(predicate: NodeName => Boolean): IndexedSeq[NodeId] = mappings.indices.filter(index => predicate(mapping(index).name))
def findNodesByNameFilter(
predicate: NodeName => Boolean
): IndexedSeq[NodeId] =
mappings.indices.filter(index => predicate(mapping(index).name))

def findNodeByName(name: NodeName): NodeId = {
val result = mappings.indexWhere(_.name === name)
Expand All @@ -61,15 +64,17 @@ object Advent08 {
val List(List(instructionLine), mappingLines) = input
val instructions = instructionLine.map(Instruction.parse)

val mappingTuples: IndexedSeq[(NodeName, NodeName, NodeName)] = mappingLines
.toIndexedSeq
.map {
case s"$from = ($left, $right)" => (from, left, right)
case line => line.failedToParse
}
val mappingTuples: IndexedSeq[(NodeName, NodeName, NodeName)] =
mappingLines.toIndexedSeq
.map {
case s"$from = ($left, $right)" => (from, left, right)
case line => line.failedToParse
}

val mappings = mappingTuples map { case (from, left, right) =>
def idx(name: NodeName): NodeId = mappingTuples.indexWhere {case (from, _, _) => from == name }
def idx(name: NodeName): NodeId = mappingTuples.indexWhere {
case (from, _, _) => from == name
}

Mapping(
name = from,
Expand All @@ -90,28 +95,22 @@ object Advent08 {
isTerminal: NodeId => Boolean,
): Long =
Simulation.runWithIterationCount(start) { case (node, counter) =>
if (isTerminal(node)) {
counter.asLeft
} else {
val next = game.instructionAtStep(counter)
val nextState = game.mapping(node).move(next)
nextState.asRight
}
if (isTerminal(node)) counter.asLeft
else game.mapping(node).move(game.instructionAtStep(counter)).asRight
}

def part1(game: Input): Long = {
val start = game.findNodeByName("AAA")
val start = game.findNodeByName("AAA")
val finish = game.findNodeByName("ZZZ")

loopAt(game, start, _ == finish)
}

def part2(game: Input): Long = {
val startNodes = game.findNodesByNameFilter(_.last === 'A')
val finishNodes = game.findNodesByNameFilter(_.last === 'Z')

val individualResults = startNodes.map(loopAt(game, _, finishNodes.contains))
val starts = game.findNodesByNameFilter(_.last === 'A')
val finishes = game.findNodesByNameFilter(_.last === 'Z')

val individualResults = starts.map(loopAt(game, _, finishes.contains))
lcmMany(individualResults)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package jurisk.utils

object Utils {
import scala.math.Integral.Implicits.infixIntegralOps

object CollectionOps {
implicit class IterableOps[T](seq: Iterable[T]) {
def counts: Map[T, Int] = seq.groupMapReduce(identity)(_ => 1)(_ + _)

Expand All @@ -20,4 +22,12 @@ object Utils {
if (seq.size == 2) (seq.head, seq.tail.head)
else sys.error(s"Expected two elements, but got $seq")
}

implicit class IndexedSeqOps[T](seq: IndexedSeq[T]) {
def atIndexWithWraparound[N: Integral](index: N): T = {
val I = implicitly[Integral[N]]
val adjusted = index % I.fromInt(seq.length)
seq(adjusted.toInt)
}
}
}
2 changes: 1 addition & 1 deletion scala2/src/main/scala/jurisk/utils/FileInput.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package jurisk.utils

import jurisk.utils.Utils.IterableOps
import jurisk.utils.CollectionOps.IterableOps

import scala.io.Source
import scala.util.Using
Expand Down
2 changes: 1 addition & 1 deletion scala2/src/main/scala/jurisk/utils/Parsing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package jurisk.utils

import cats.implicits._
import jurisk.geometry.Coords2D
import jurisk.utils.Utils.IterableOps
import jurisk.utils.CollectionOps.IterableOps

import scala.annotation.tailrec

Expand Down

0 comments on commit befc661

Please sign in to comment.