Skip to content

Commit 76ff4b6

Browse files
committed
Year 2015 Day 21
1 parent 7a2e58f commit 76ff4b6

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,4 @@ The minimal SBT project provides:
230230
| 18 | [Like a GIF For Your Yard](https://adventofcode.com/2015/day/18) | [Source](src/main/scala/AdventOfCode2015/Day18.scala) |
231231
| 19 | [Medicine for Rudolph](https://adventofcode.com/2015/day/19) | [Source](src/main/scala/AdventOfCode2015/Day19.scala) |
232232
| 20 | [Infinite Elves and Infinite Houses](https://adventofcode.com/2015/day/20) | [Source](src/main/scala/AdventOfCode2015/Day20.scala) |
233+
| 21 | [RPG Simulator 20XX](https://adventofcode.com/2015/day/21) | [Source](src/main/scala/AdventOfCode2015/Day21.scala) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Hit Points: 100
2+
Damage: 8
3+
Armor: 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package AdventOfCode2015
2+
3+
object Day21:
4+
case class Item(cost: Int, damage: Int, armor: Int):
5+
def +(other: Item): Item = Item(cost + other.cost, damage + other.damage, armor + other.armor)
6+
7+
val none = Item(0, 0, 0)
8+
val weapons = Set(Item(8, 4, 0), Item(10, 5, 0), Item(25, 6, 0), Item(40, 7, 0), Item(74, 8, 0))
9+
val armors = Set(none, Item(13, 0, 1), Item(31, 0, 2), Item(53, 0, 3), Item(75, 0, 4), Item(102, 0, 5))
10+
val rings = Seq(none, none, Item(25, 1, 0), Item(50, 2, 0), Item(100, 3, 0), Item(20, 0, 1), Item(40, 0, 2), Item(80, 0, 3))
11+
.combinations(2).map(c => c.head + c.last).toSet
12+
val outfits = for weapon <- weapons; armor <- armors; ring <- rings yield weapon + armor + ring
13+
14+
def parse(input: Seq[String]): Item =
15+
val Seq(health, damage, armor) = input.map(_.filter(_.isDigit).toInt)
16+
Item(health, damage, armor)
17+
18+
def fight(boss: Item)(hero: Item): Boolean =
19+
val heroTurns = boss.cost / (hero.damage - boss.armor).max(1)
20+
val bossTurns = 100 / (boss.damage - hero.armor).max(1)
21+
heroTurns <= bossTurns
22+
23+
def part1(input: Seq[String]): Int =
24+
val boss = parse(input)
25+
outfits.filter(fight(boss)).map(_.cost).min
26+
27+
def part2(input: Seq[String]): Int =
28+
val boss = parse(input)
29+
outfits.filterNot(fight(boss)).map(_.cost).max
30+
31+
def main(args: Array[String]): Unit =
32+
val data = io.Source.fromResource("AdventOfCode2015/Day21.txt").getLines().toSeq
33+
println(part1(data))
34+
println(part2(data))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package AdventOfCode2015
2+
3+
import org.scalatest.funsuite.AnyFunSuite
4+
5+
class Day21Suite extends AnyFunSuite
6+
// No unit tests possible

0 commit comments

Comments
 (0)