From 86ca2c95af88fb269f97db9928d71ae7f132f452 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Wed, 6 Dec 2023 08:31:14 +0100 Subject: [PATCH] day 6 puzzle 2 --- README.md | 16 ++++++------ src/day6/Puzzle2.scala | 47 ++++++++++++++++++++++++++++++++++++ tests/day6/Puzzle2Test.scala | 9 +++++++ 3 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 src/day6/Puzzle2.scala create mode 100644 tests/day6/Puzzle2Test.scala diff --git a/README.md b/README.md index 0413aff..02fa6c8 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: 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 +#### Stars: 12 / 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::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/Puzzle2.scala b/src/day6/Puzzle2.scala new file mode 100644 index 0000000..2a24e49 --- /dev/null +++ b/src/day6/Puzzle2.scala @@ -0,0 +1,47 @@ +package day6 + +import scala.io.{BufferedSource, Source} + +object Puzzle2 { + case class Race(duration: Long, record: Long) + + var race: Race = Race(0, 0) + def loadInput(path: String): Unit = { + val source: BufferedSource = Source.fromFile(path) + val lines: Array[String] = source.getLines().toArray + + val duration: Long = lines(0).split(": +")(1).replace(" ", "").toLong + val record: Long = lines(1).split(": +")(1).replace(" ", "").toLong + + race = Race(duration, record) + + 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) + + return getMargin(race) + } + + def main(args: Array[String]): Unit = { + val solution: Int = solve("./res/day6/input1.txt") + println(solution) + } +} + diff --git a/tests/day6/Puzzle2Test.scala b/tests/day6/Puzzle2Test.scala new file mode 100644 index 0000000..7879ecc --- /dev/null +++ b/tests/day6/Puzzle2Test.scala @@ -0,0 +1,9 @@ +package day6 + +import org.scalatest.funsuite.AnyFunSuite + +class Puzzle2Test extends AnyFunSuite { + test("Puzzle2.solve") { + assert(Puzzle2.solve("tests_res/day6/input1.txt") == 71503) + } +}