|
| 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)) |
0 commit comments