Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 91a92ab

Browse files
Year 2022 Day 19
1 parent a72d7cd commit 91a92ab

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ The minimal SBT project provides:
4949
| 16 | [Proboscidea Volcanium](https://adventofcode.com/2022/day/16) | [Source](src/main/scala/AdventOfCode2022/Day16.scala) |
5050
| 17 | [Pyroclastic Flow](https://adventofcode.com/2022/day/17) | [Source](src/main/scala/AdventOfCode2022/Day17.scala) |
5151
| 18 | [Boiling Boulders](https://adventofcode.com/2022/day/18) | [Source](src/main/scala/AdventOfCode2022/Day18.scala) |
52+
| 19 | [Not Enough Minerals](https://adventofcode.com/2022/day/19) | [Source](src/main/scala/AdventOfCode2022/Day19.scala) |
5253

5354
## 2021
5455

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Blueprint 1: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 18 clay. Each geode robot costs 3 ore and 8 obsidian.
2+
Blueprint 2: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 18 clay. Each geode robot costs 4 ore and 20 obsidian.
3+
Blueprint 3: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 18 clay. Each geode robot costs 4 ore and 12 obsidian.
4+
Blueprint 4: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 20 clay. Each geode robot costs 2 ore and 8 obsidian.
5+
Blueprint 5: Each ore robot costs 2 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 14 clay. Each geode robot costs 3 ore and 8 obsidian.
6+
Blueprint 6: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 17 clay. Each geode robot costs 4 ore and 8 obsidian.
7+
Blueprint 7: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 4 ore and 6 clay. Each geode robot costs 3 ore and 11 obsidian.
8+
Blueprint 8: Each ore robot costs 2 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 20 clay. Each geode robot costs 4 ore and 18 obsidian.
9+
Blueprint 9: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 16 clay. Each geode robot costs 3 ore and 15 obsidian.
10+
Blueprint 10: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 7 clay. Each geode robot costs 4 ore and 11 obsidian.
11+
Blueprint 11: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 9 clay. Each geode robot costs 3 ore and 19 obsidian.
12+
Blueprint 12: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 17 clay. Each geode robot costs 3 ore and 16 obsidian.
13+
Blueprint 13: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 5 clay. Each geode robot costs 3 ore and 12 obsidian.
14+
Blueprint 14: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 8 clay. Each geode robot costs 2 ore and 12 obsidian.
15+
Blueprint 15: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 7 clay. Each geode robot costs 3 ore and 20 obsidian.
16+
Blueprint 16: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 11 clay. Each geode robot costs 2 ore and 19 obsidian.
17+
Blueprint 17: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 10 clay. Each geode robot costs 3 ore and 14 obsidian.
18+
Blueprint 18: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 12 clay. Each geode robot costs 3 ore and 17 obsidian.
19+
Blueprint 19: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 7 clay. Each geode robot costs 2 ore and 19 obsidian.
20+
Blueprint 20: Each ore robot costs 2 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 17 clay. Each geode robot costs 3 ore and 19 obsidian.
21+
Blueprint 21: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 14 clay. Each geode robot costs 4 ore and 11 obsidian.
22+
Blueprint 22: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 4 ore and 15 clay. Each geode robot costs 4 ore and 9 obsidian.
23+
Blueprint 23: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 9 clay. Each geode robot costs 3 ore and 7 obsidian.
24+
Blueprint 24: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 15 clay. Each geode robot costs 4 ore and 17 obsidian.
25+
Blueprint 25: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 9 clay. Each geode robot costs 2 ore and 20 obsidian.
26+
Blueprint 26: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 20 clay. Each geode robot costs 3 ore and 14 obsidian.
27+
Blueprint 27: Each ore robot costs 2 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 15 clay. Each geode robot costs 3 ore and 16 obsidian.
28+
Blueprint 28: Each ore robot costs 2 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 8 clay. Each geode robot costs 3 ore and 20 obsidian.
29+
Blueprint 29: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 14 clay. Each geode robot costs 3 ore and 14 obsidian.
30+
Blueprint 30: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 4 ore and 18 clay. Each geode robot costs 3 ore and 13 obsidian.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package AdventOfCode2022
2+
3+
object Day19:
4+
case class Resources(ore: Int, clay: Int, obsidian: Int, geode: Int):
5+
def +(r: Resources): Resources = Resources(ore + r.ore, clay + r.clay, obsidian + r.obsidian, geode + r.geode)
6+
def -(r: Resources): Resources = Resources(ore - r.ore, clay - r.clay, obsidian - r.obsidian, geode - r.geode)
7+
def <=(r: Resources): Boolean = ore <= r.ore && clay <= r.clay && obsidian <= r.obsidian && geode <= r.geode
8+
9+
def maximize(input: Seq[String], minutes: Int): Seq[Int] = input.map { line =>
10+
val Seq(id, ore1, clay1, obsidian1, obsidian2, geode1, geode2) = line.split("\\D+").tail.map(_.toInt).toSeq
11+
12+
val oreBotCost = Resources(ore1, 0, 0, 0)
13+
val clayBotCost = Resources(clay1, 0, 0, 0)
14+
val obsidianBotCost = Resources(obsidian1, obsidian2, 0, 0)
15+
val geodeBotCost = Resources(geode1, 0, geode2, 0)
16+
17+
val zero = Resources(0, 0, 0, 0)
18+
val oreBot = Resources(1, 0, 0, 0)
19+
val clayBot = Resources(0, 1, 0, 0)
20+
val obsidianBot = Resources(0, 0, 1, 0)
21+
val geodeBot = Resources(0, 0, 0, 1)
22+
23+
val maxOre = ore1.max(clay1).max(obsidian1).max(geode1)
24+
val maxClay = obsidian2
25+
val maxObsidian = geode2
26+
27+
def helper(time: Int, bots: Resources, resources: Resources, prevCanOre: Boolean, prevCanClay: Boolean, prevCanObsidian: Boolean): Int =
28+
if time == 0 then
29+
resources.geode
30+
else if geodeBotCost <= resources then
31+
helper(time - 1, bots + geodeBot, resources + bots - geodeBotCost, false, false, false)
32+
else
33+
val canOre = oreBotCost <= resources && bots.ore < maxOre
34+
val canClay = clayBotCost <= resources && bots.clay < maxClay
35+
val canObsidian = obsidianBotCost <= resources && bots.obsidian < maxObsidian
36+
37+
val first = helper(time - 1, bots, resources + bots, canOre, canClay, canObsidian)
38+
val second = if canOre && !prevCanOre then helper(time - 1, bots + oreBot, resources + bots - oreBotCost, false, false, false) else 0
39+
val third = if canClay && !prevCanClay then helper(time - 1, bots + clayBot, resources + bots - clayBotCost, false, false, false) else 0
40+
val fourth = if canObsidian && !prevCanObsidian then helper(time - 1, bots + obsidianBot, resources + bots - obsidianBotCost, false, false, false) else 0
41+
42+
first.max(second).max(third).max(fourth)
43+
end if
44+
end helper
45+
46+
id * helper(minutes, oreBot, zero, false, false, false)
47+
}
48+
49+
def part1(input: Seq[String]): Int = maximize(input, 24).sum
50+
51+
def part2(input: Seq[String]): Int = maximize(input.take(3), 32).product / 6
52+
53+
def main(args: Array[String]): Unit =
54+
val data = io.Source.fromResource("AdventOfCode2022/Day19.txt").getLines().toSeq
55+
println(part1(data))
56+
println(part2(data))
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package AdventOfCode2022
2+
3+
import org.scalatest.funsuite.AnyFunSuite
4+
5+
class Day19Suite extends AnyFunSuite:
6+
val sample = Seq(
7+
"Blueprint 1: Each ore robot costs 4 ore. Each clay robot costs 2 ore. Each obsidian robot costs 3 ore and 14 clay. Each geode robot costs 2 ore and 7 obsidian.",
8+
"Blueprint 2: Each ore robot costs 2 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 8 clay. Each geode robot costs 3 ore and 12 obsidian.")
9+
10+
test("Part 1 should handle sample input correctly") {
11+
assert(Day19.part1(sample) == 33)
12+
}
13+
14+
test("Part 2 should handle sample input correctly") {
15+
assert(Day19.part2(sample) == 1157)
16+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /