File tree Expand file tree Collapse file tree 4 files changed +54
-0
lines changed
resources/AdventOfCode2015
test/scala/AdventOfCode2015 Expand file tree Collapse file tree 4 files changed +54
-0
lines changed Original file line number Diff line number Diff line change @@ -223,3 +223,4 @@ The minimal SBT project provides:
223
223
| 11 | [ Corporate Policy] ( https://adventofcode.com/2015/day/11 ) | [ Source] ( src/main/scala/AdventOfCode2015/Day11.scala ) |
224
224
| 12 | [ JSAbacusFramework.io] ( https://adventofcode.com/2015/day/12 ) | [ Source] ( src/main/scala/AdventOfCode2015/Day12.scala ) |
225
225
| 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 ) |
Original file line number Diff line number Diff line change
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.
Original file line number Diff line number Diff line change
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 ))
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments