Skip to content

Commit 894bc43

Browse files
committed
Year 2015 Day 14
1 parent f24d2aa commit 894bc43

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,4 @@ The minimal SBT project provides:
223223
| 11 | [Corporate Policy](https://adventofcode.com/2015/day/11) | [Source](src/main/scala/AdventOfCode2015/Day11.scala) |
224224
| 12 | [JSAbacusFramework.io](https://adventofcode.com/2015/day/12) | [Source](src/main/scala/AdventOfCode2015/Day12.scala) |
225225
| 13 | [Knights of the Dinner Table](https://adventofcode.com/2015/day/13) | [Source](src/main/scala/AdventOfCode2015/Day13.scala) |
226+
| 14 | [Reindeer Olympics](https://adventofcode.com/2015/day/14) | [Source](src/main/scala/AdventOfCode2015/Day14.scala) |
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Vixen can fly 8 km/s for 8 seconds, but then must rest for 53 seconds.
2+
Blitzen can fly 13 km/s for 4 seconds, but then must rest for 49 seconds.
3+
Rudolph can fly 20 km/s for 7 seconds, but then must rest for 132 seconds.
4+
Cupid can fly 12 km/s for 4 seconds, but then must rest for 43 seconds.
5+
Donner can fly 9 km/s for 5 seconds, but then must rest for 38 seconds.
6+
Dasher can fly 10 km/s for 4 seconds, but then must rest for 37 seconds.
7+
Comet can fly 3 km/s for 37 seconds, but then must rest for 76 seconds.
8+
Prancer can fly 9 km/s for 12 seconds, but then must rest for 97 seconds.
9+
Dancer can fly 37 km/s for 1 seconds, but then must rest for 36 seconds.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package AdventOfCode2015
2+
3+
object Day14:
4+
case class Reindeer(speed: Int, fly: Int, rest: Int):
5+
def distance(time: Int): Int =
6+
val total = fly + rest
7+
val complete = time / total
8+
val partial = (time % total).min(fly)
9+
speed * (complete * fly + partial)
10+
11+
def parse(input: Seq[String]): Seq[Reindeer] = input.map { line =>
12+
val Array(speed, fly, rest) = line.split("\\D+").tail.map(_.toInt)
13+
Reindeer(speed, fly, rest)
14+
}
15+
16+
def part1(input: Seq[String], time: Int): Int = parse(input).map(_.distance(time)).max
17+
18+
def part2(input: Seq[String], time: Int): Int =
19+
val reindeer = parse(input)
20+
(1 to time)
21+
.map(time => reindeer.map(_.distance(time)))
22+
.map(state => state.map(d => if d == state.max then 1 else 0))
23+
.transpose.map(_.sum).max
24+
25+
def main(args: Array[String]): Unit =
26+
val data = io.Source.fromResource("AdventOfCode2015/Day14.txt").getLines().toSeq
27+
println(part1(data, 2503))
28+
println(part2(data, 2503))
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package AdventOfCode2015
2+
3+
import org.scalatest.funsuite.AnyFunSuite
4+
5+
class Day14Suite extends AnyFunSuite:
6+
val sample = Seq(
7+
"Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.",
8+
"Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds.")
9+
10+
test("Part 1 should handle sample input correctly") {
11+
assert(Day14.part1(sample, 1000) == 1120)
12+
}
13+
14+
test("Part 2 should handle sample input correctly") {
15+
assert(Day14.part2(sample, 1000) == 689)
16+
}

0 commit comments

Comments
 (0)