-
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.
* 2023-15 * 2023-15 * 2023-15 * 2023-15 --------- Co-authored-by: Juris <jurisk@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
184 additions
and
15 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
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 @@ | ||
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7 |
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
104 changes: 104 additions & 0 deletions
104
scala2/src/main/scala/jurisk/adventofcode/y2023/Advent15.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,104 @@ | ||
package jurisk.adventofcode.y2023 | ||
|
||
import jurisk.adventofcode.y2023.Advent15.Step.{Remove, Replace} | ||
import jurisk.utils.CollectionOps.{SeqOps, VectorOps} | ||
import jurisk.utils.FileInput._ | ||
import jurisk.utils.Parsing.StringOps | ||
|
||
object Advent15 { | ||
private type Steps = List[Step] | ||
private type Label = String | ||
private type FocalLength = Int | ||
|
||
def calculateHash(s: String): Int = | ||
s.toList.foldLeft(0) { case (acc, ch) => | ||
((acc + ch.toInt) * 17) % 256 | ||
} | ||
|
||
def parse(input: String): Steps = | ||
input.split(",").map(Step.parse).toList | ||
|
||
sealed trait Step { | ||
def originalString: String | ||
} | ||
|
||
object Step { | ||
final case class Remove(label: String) extends Step { | ||
override def originalString: Label = s"$label-" | ||
} | ||
|
||
final case class Replace( | ||
label: Label, | ||
focalLength: FocalLength, | ||
) extends Step { | ||
override def originalString: Label = s"$label=$focalLength" | ||
} | ||
|
||
def parse(s: String): Step = | ||
s match { | ||
case s"$label-" => Remove(label) | ||
case s"$label=$focalLength" => Replace(label, focalLength.toInt) | ||
case _ => s.failedToParse | ||
} | ||
} | ||
|
||
final case class Lens(label: String, focalLength: Int) | ||
|
||
final case class LensBox(lenses: Vector[Lens]) { | ||
def subtract(label: Label): LensBox = | ||
lenses.firstIndexWhere(_.label == label) match { | ||
case Some(idx) => LensBox(lenses.removeAt(idx)) | ||
case None => this | ||
} | ||
|
||
def replace(label: Label, value: FocalLength): LensBox = | ||
LensBox( | ||
lenses.firstIndexWhere(_.label == label) match { | ||
case Some(idx) => lenses.updated(idx, Lens(label, value)) | ||
case None => lenses :+ Lens(label, value) | ||
} | ||
) | ||
|
||
def value: Int = | ||
lenses.zipWithIndex.map { case (lens, index) => | ||
(index + 1) * lens.focalLength | ||
}.sum | ||
} | ||
|
||
private object LensBox { | ||
def empty: LensBox = LensBox(Vector.empty) | ||
} | ||
|
||
def part1(data: Steps): Int = | ||
data.map(op => calculateHash(op.originalString)).sum | ||
|
||
def part2(ops: Steps): Int = { | ||
val LensCount = 256 | ||
val boxen = ops.foldLeft(Vector.fill(LensCount)(LensBox.empty)) { | ||
case (acc, op) => | ||
op match { | ||
case Remove(label) => | ||
val idx = calculateHash(label) | ||
acc.updatedWith(idx)(_.subtract(label)) | ||
|
||
case Replace(label, focalLength) => | ||
val idx = calculateHash(label) | ||
acc.updatedWith(idx)(_.replace(label, focalLength)) | ||
} | ||
} | ||
|
||
boxen.zipWithIndex.map { case (contents, idx) => | ||
(idx + 1) * contents.value | ||
}.sum | ||
} | ||
|
||
def parseFile(fileName: String): Steps = | ||
parse(readFileText(fileName)) | ||
|
||
def main(args: Array[String]): Unit = { | ||
val realData: Steps = parseFile("2023/15.txt") | ||
|
||
println(s"Part 1: ${part1(realData)}") | ||
println(s"Part 2: ${part2(realData)}") | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
scala2/src/test/scala/jurisk/adventofcode/y2023/Advent15Spec.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,37 @@ | ||
package jurisk.adventofcode.y2023 | ||
|
||
import org.scalatest.freespec.AnyFreeSpec | ||
import Advent15._ | ||
import org.scalatest.matchers.should.Matchers._ | ||
|
||
class Advent15Spec extends AnyFreeSpec { | ||
"calculateHash" - { | ||
"HASH" in { | ||
calculateHash("HASH") shouldEqual 52 | ||
} | ||
|
||
"rn=1" in { | ||
calculateHash("rn=1") shouldEqual 30 | ||
} | ||
} | ||
|
||
"part 1" - { | ||
"test" in { | ||
part1(parseFile("2023/15-test.txt")) shouldEqual 1320 | ||
} | ||
|
||
"real" in { | ||
part1(parseFile("2023/15.txt")) shouldEqual 521341 | ||
} | ||
} | ||
|
||
"part 2" - { | ||
"test" in { | ||
part2(parseFile("2023/15-test.txt")) shouldEqual 145 | ||
} | ||
|
||
"real" in { | ||
part2(parseFile("2023/15.txt")) shouldEqual 252782 | ||
} | ||
} | ||
} |