day 6 puzzle 1
This commit is contained in:
parent
4e1cd00e53
commit
c8ea1636e1
16
README.md
16
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<br>:star::star: | 2<br>:star::star: | 3<br>:star::star: |
|
||||
| 4<br>:star::star: | 5<br>:star::star: | 6 | 7 | 8 | 9 | 10 |
|
||||
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
|
||||
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
|
||||
| 25 | | | | | | |
|
||||
#### Stars: 11 / 50
|
||||
| Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|
||||
|:-----------------:|:-----------------:|:-----------:|:---:|:-----------------:|:-----------------:|:-----------------:|
|
||||
| | | | | 1<br>:star::star: | 2<br>:star::star: | 3<br>:star::star: |
|
||||
| 4<br>:star::star: | 5<br>:star::star: | 6<br>:star: | 7 | 8 | 9 | 10 |
|
||||
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
|
||||
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
|
||||
| 25 | | | | | | |
|
54
src/day6/Puzzle1.scala
Normal file
54
src/day6/Puzzle1.scala
Normal file
@ -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)
|
||||
}
|
||||
}
|
9
tests/day6/Puzzle1Test.scala
Normal file
9
tests/day6/Puzzle1Test.scala
Normal file
@ -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)
|
||||
}
|
||||
}
|
2
tests_res/day6/input1.txt
Normal file
2
tests_res/day6/input1.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Time: 7 15 30
|
||||
Distance: 9 40 200
|
Loading…
Reference in New Issue
Block a user