Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d5b072

Browse files
committed
2022 day 19 part 1
1 parent ebc3bf7 commit 8d5b072

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//: [Previous](@previous)
2+
3+
import Foundation
4+
5+
part1()
6+
7+
//: [Next](@next)
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 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 17 clay. Each geode robot costs 4 ore and 20 obsidian.
2+
Blueprint 2: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 12 clay. Each geode robot costs 2 ore and 10 obsidian.
3+
Blueprint 3: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 20 clay. Each geode robot costs 3 ore and 18 obsidian.
4+
Blueprint 4: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 19 clay. Each geode robot costs 3 ore and 17 obsidian.
5+
Blueprint 5: 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 13 obsidian.
6+
Blueprint 6: Each ore robot costs 2 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 16 clay. Each geode robot costs 2 ore and 9 obsidian.
7+
Blueprint 7: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 14 clay. Each geode robot costs 4 ore and 17 obsidian.
8+
Blueprint 8: 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.
9+
Blueprint 9: 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 15 obsidian.
10+
Blueprint 10: Each ore robot costs 4 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.
11+
Blueprint 11: Each ore robot costs 2 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 19 clay. Each geode robot costs 4 ore and 13 obsidian.
12+
Blueprint 12: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 4 ore and 19 clay. Each geode robot costs 4 ore and 7 obsidian.
13+
Blueprint 13: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 15 clay. Each geode robot costs 3 ore and 7 obsidian.
14+
Blueprint 14: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 10 clay. Each geode robot costs 2 ore and 7 obsidian.
15+
Blueprint 15: 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.
16+
Blueprint 16: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 5 clay. Each geode robot costs 3 ore and 7 obsidian.
17+
Blueprint 17: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 20 clay. Each geode robot costs 2 ore and 10 obsidian.
18+
Blueprint 18: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 15 clay. Each geode robot costs 2 ore and 13 obsidian.
19+
Blueprint 19: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 14 clay. Each geode robot costs 4 ore and 19 obsidian.
20+
Blueprint 20: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 18 clay. Each geode robot costs 4 ore and 9 obsidian.
21+
Blueprint 21: 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.
22+
Blueprint 22: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 4 ore and 8 clay. Each geode robot costs 3 ore and 7 obsidian.
23+
Blueprint 23: Each ore robot costs 2 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 19 clay. Each geode robot costs 4 ore and 8 obsidian.
24+
Blueprint 24: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 5 clay. Each geode robot costs 2 ore and 10 obsidian.
25+
Blueprint 25: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 4 ore and 8 clay. Each geode robot costs 2 ore and 8 obsidian.
26+
Blueprint 26: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 14 clay. Each geode robot costs 4 ore and 10 obsidian.
27+
Blueprint 27: Each ore robot costs 2 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 18 clay. Each geode robot costs 2 ore and 11 obsidian.
28+
Blueprint 28: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 16 clay. Each geode robot costs 3 ore and 14 obsidian.
29+
Blueprint 29: Each ore robot costs 2 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 18 clay. Each geode robot costs 2 ore and 19 obsidian.
30+
Blueprint 30: Each ore robot costs 2 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 11 clay. Each geode robot costs 3 ore and 8 obsidian.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import Foundation
2+
3+
struct Blueprint {
4+
let id: Int
5+
let oreOre: Int
6+
let clayOre: Int
7+
let obsidianOre: Int
8+
let obsidianClay: Int
9+
let geodeOre: Int
10+
let geodeObsidian: Int
11+
}
12+
13+
func inputData() -> [Blueprint] {
14+
var result = [Blueprint]()
15+
for line in stringsFromFile() where line != "" {
16+
let parts = line.replacingOccurrences(of: ":", with: "").split(separator: " ")
17+
result.append(Blueprint(
18+
id: Int(parts[1])!,
19+
oreOre: Int(parts[6])!,
20+
clayOre: Int(parts[12])!,
21+
obsidianOre: Int(parts[18])!,
22+
obsidianClay: Int(parts[21])!,
23+
geodeOre: Int(parts[27])!,
24+
geodeObsidian: Int(parts[30])!
25+
))
26+
}
27+
return result
28+
}
29+
30+
extension Blueprint {
31+
func geodes(_ oreBot: Int, _ clayBot: Int, _ obsidianBot: Int, _ geodeBot: Int, _ ore: Int, _ clay: Int, _ obsidian: Int, _ geode: Int, _ timeLeft: Int) -> Int {
32+
33+
let oreNext = ore + oreBot
34+
let clayNext = clay + clayBot
35+
let obsidianNext = obsidian + obsidianBot
36+
let geodeNext = geode + geodeBot
37+
38+
if timeLeft <= 0 {
39+
return geodeNext
40+
}
41+
if ore >= self.geodeOre && obsidian >= self.geodeObsidian {
42+
return geodes(oreBot, clayBot, obsidianBot, geodeBot + 1, oreNext - self.geodeOre, clayNext, obsidianNext - self.geodeObsidian, geodeNext, timeLeft - 1)
43+
}
44+
if ore >= self.obsidianOre && clay >= self.obsidianClay {
45+
return geodes(oreBot, clayBot, obsidianBot + 1, geodeBot, oreNext - self.obsidianOre, clayNext - self.obsidianClay, obsidianNext, geodeNext, timeLeft - 1)
46+
}
47+
var results = [0]
48+
if ore < 5 {
49+
results.append(geodes(oreBot, clayBot, obsidianBot, geodeBot, oreNext, clayNext, obsidianNext, geodeNext, timeLeft - 1))
50+
}
51+
if ore >= self.oreOre {
52+
results.append(geodes(oreBot + 1, clayBot, obsidianBot, geodeBot, oreNext - self.oreOre, clayNext, obsidianNext, geodeNext, timeLeft - 1))
53+
}
54+
if ore >= self.clayOre {
55+
results.append(geodes(oreBot, clayBot + 1, obsidianBot, geodeBot, oreNext - self.clayOre, clayNext, obsidianNext, geodeNext, timeLeft - 1))
56+
}
57+
return results.reduce(0, max)
58+
}
59+
}
60+
public func part1() -> Int {
61+
inputData().map({ $0.id * $0.geodes(1, 0, 0, 0, 0, 0, 0, 0, 23) })
62+
.reduce(0, +)
63+
}
64+

aoc22.playground/contents.xcplayground

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
<page name='Day16'/>
2020
<page name='Day17'/>
2121
<page name='Day18'/>
22+
<page name='Day19'/>
2223
</pages>
2324
</playground>

0 commit comments

Comments
 (0)