-
Notifications
You must be signed in to change notification settings - Fork 4
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
3 changed files
with
47 additions
and
0 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,2 @@ | ||
Time: 48 93 85 95 | ||
Distance: 296 1928 1236 1391 |
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,26 @@ | ||
package eu.sim642.adventofcode2023 | ||
|
||
object Day6 { | ||
|
||
case class Race(time: Int, recordDistance: Int) { | ||
def wins: Int = { | ||
(0 to time).count(t => t * (time - t) > recordDistance) | ||
} | ||
} | ||
|
||
def multiplyRaceWins(races: Seq[Race]): Int = races.map(_.wins).product | ||
|
||
|
||
def parseRaces(input: String): Seq[Race] = input.linesIterator.toSeq match { | ||
case Seq(s"Time:$timesStr", s"Distance:$distancesStr") => | ||
val times = timesStr.trim.split(" +").map(_.toInt).toSeq | ||
val distances = distancesStr.trim.split(" +").map(_.toInt).toSeq | ||
(times lazyZip distances).map(Race.apply) | ||
} | ||
|
||
lazy val input: String = io.Source.fromInputStream(getClass.getResourceAsStream("day6.txt")).mkString.trim | ||
|
||
def main(args: Array[String]): Unit = { | ||
println(multiplyRaceWins(parseRaces(input))) | ||
} | ||
} |
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,19 @@ | ||
package eu.sim642.adventofcode2023 | ||
|
||
import Day6._ | ||
import org.scalatest.funsuite.AnyFunSuite | ||
|
||
class Day6Test extends AnyFunSuite { | ||
|
||
private val exampleInput = | ||
"""Time: 7 15 30 | ||
|Distance: 9 40 200""".stripMargin | ||
|
||
test("Part 1 examples") { | ||
assert(multiplyRaceWins(parseRaces(exampleInput)) == 288) | ||
} | ||
|
||
test("Part 1 input answer") { | ||
assert(multiplyRaceWins(parseRaces(input)) == 2756160) | ||
} | ||
} |