diff --git a/README.md b/README.md index 595de8f..0413aff 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,11 @@ This repo contains my attempt at this year's Advent of Code (2023) I will try and do some problems (probably not all of them) in Scala ## Progress: -#### Stars: 10 / 50 -| Mon | Tue | Wed | Thu | Fri | Sat | Sun | -|:-----------------:|:-----------------:|:---:|:---:|:-----------------:|:-----------------:|:-----------------:| -| | | | | 1
:star::star: | 2
:star::star: | 3
:star::star: | -| 4
:star::star: | 5
:star::star: | 6 | 7 | 8 | 9 | 10 | -| 11 | 12 | 13 | 14 | 15 | 16 | 17 | -| 18 | 19 | 20 | 21 | 22 | 23 | 24 | -| 25 | | | | | | | \ No newline at end of file +#### Stars: 11 / 50 +| Mon | Tue | Wed | Thu | Fri | Sat | Sun | +|:-----------------:|:-----------------:|:-----------:|:---:|:-----------------:|:-----------------:|:-----------------:| +| | | | | 1
:star::star: | 2
:star::star: | 3
:star::star: | +| 4
:star::star: | 5
:star::star: | 6
:star: | 7 | 8 | 9 | 10 | +| 11 | 12 | 13 | 14 | 15 | 16 | 17 | +| 18 | 19 | 20 | 21 | 22 | 23 | 24 | +| 25 | | | | | | | \ No newline at end of file diff --git a/src/day6/Puzzle1.scala b/src/day6/Puzzle1.scala new file mode 100644 index 0000000..81be4f0 --- /dev/null +++ b/src/day6/Puzzle1.scala @@ -0,0 +1,54 @@ +package day6 + +import scala.io.{BufferedSource, Source} + +object Puzzle1 { + case class Race(duration: Int, record: Int) + + var races: Array[Race] = Array.empty + def loadInput(path: String): Unit = { + val source: BufferedSource = Source.fromFile(path) + val lines: Array[String] = source.getLines().toArray + + val durations: Array[Int] = lines(0).split(": +")(1).split(" +").map(d => d.toInt) + val records: Array[Int] = lines(1).split(": +")(1).split(" +").map(r => r.toInt) + + races = new Array(durations.length) + + for (i: Int <- races.indices) { + races(i) = Race(durations(i), records(i)) + } + + source.close() + } + + def getMargin(race: Race): Int = { + // dist = (dur - press) * press + // dist-record > 0 <=> f(press) = dur*press - press^2 - record > 0 + // x^2 - d*x + r = 0 + // x = ( d +- sqrt(d^2 - 4r) ) / 2 + + val delta: Double = race.duration*race.duration - 4*race.record + val x0: Double = (race.duration - Math.sqrt(delta))/2 + val x1: Double = (race.duration + Math.sqrt(delta))/2 + + val margin: Int = (Math.ceil(x1) - Math.floor(x0) - 1).toInt + return margin + } + + def solve(path: String): Int = { + loadInput(path) + + var solution: Int = 1 + for (race: Race <- races) { + solution *= getMargin(race) + } + + return solution + } + + def main(args: Array[String]): Unit = { + val solution: Int = solve("./res/day6/input1.txt") + println(solution) + } +} diff --git a/tests/day6/Puzzle1Test.scala b/tests/day6/Puzzle1Test.scala new file mode 100644 index 0000000..43897e4 --- /dev/null +++ b/tests/day6/Puzzle1Test.scala @@ -0,0 +1,9 @@ +package day6 + +import org.scalatest.funsuite.AnyFunSuite + +class Puzzle1Test extends AnyFunSuite { + test("Puzzle1.solve") { + assert(Puzzle1.solve("tests_res/day6/input1.txt") == 288) + } +} diff --git a/tests_res/day6/input1.txt b/tests_res/day6/input1.txt new file mode 100644 index 0000000..b39f49d --- /dev/null +++ b/tests_res/day6/input1.txt @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200 \ No newline at end of file