Skip to content

Commit 0a75778

Browse files
committed
Add day 19
1 parent 1adf042 commit 0a75778

File tree

5 files changed

+225
-0
lines changed

5 files changed

+225
-0
lines changed

day19_input.txt

+30
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 2 ore and 16 clay. Each geode robot costs 4 ore and 16 obsidian.
2+
Blueprint 2: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 14 clay. Each geode robot costs 4 ore and 8 obsidian.
3+
Blueprint 3: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 18 clay. Each geode robot costs 4 ore and 16 obsidian.
4+
Blueprint 4: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 7 clay. Each geode robot costs 2 ore and 7 obsidian.
5+
Blueprint 5: 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 16 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 3 ore and 20 clay. Each geode robot costs 2 ore and 19 obsidian.
8+
Blueprint 8: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 10 clay. Each geode robot costs 4 ore and 10 obsidian.
9+
Blueprint 9: 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.
10+
Blueprint 10: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 19 clay. Each geode robot costs 3 ore and 13 obsidian.
11+
Blueprint 11: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 12 clay. Each geode robot costs 3 ore and 15 obsidian.
12+
Blueprint 12: 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.
13+
Blueprint 13: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 10 clay. Each geode robot costs 2 ore and 10 obsidian.
14+
Blueprint 14: 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.
15+
Blueprint 15: 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.
16+
Blueprint 16: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 10 clay. Each geode robot costs 2 ore and 14 obsidian.
17+
Blueprint 17: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 6 clay. Each geode robot costs 2 ore and 10 obsidian.
18+
Blueprint 18: 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.
19+
Blueprint 19: 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 3 ore and 14 obsidian.
20+
Blueprint 20: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 8 clay. Each geode robot costs 2 ore and 10 obsidian.
21+
Blueprint 21: 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.
22+
Blueprint 22: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 13 clay. Each geode robot costs 3 ore and 12 obsidian.
23+
Blueprint 23: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 20 clay. Each geode robot costs 4 ore and 7 obsidian.
24+
Blueprint 24: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 7 clay. Each geode robot costs 3 ore and 9 obsidian.
25+
Blueprint 25: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 13 clay. Each geode robot costs 2 ore and 9 obsidian.
26+
Blueprint 26: 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 4 ore and 17 obsidian.
27+
Blueprint 27: 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.
28+
Blueprint 28: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 6 clay. Each geode robot costs 2 ore and 16 obsidian.
29+
Blueprint 29: 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.
30+
Blueprint 30: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 20 clay. Each geode robot costs 3 ore and 9 obsidian.

day19a/Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "day19a"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

day19a/src/main.rs

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
use std::io::Read;
2+
3+
fn find_max(
4+
costs: &[[u16; 3]; 4],
5+
inventory: [u16; 4],
6+
bots: [u16; 4],
7+
time: u16,
8+
level: u8,
9+
best: &mut u16,
10+
) {
11+
const MAX_TIME: u16 = 24;
12+
let mut time_to_geode_bot = u16::MAX;
13+
let mut built_bot = false;
14+
15+
'outer: for (n, (&cost, &bot)) in costs.iter().zip(&bots).enumerate().rev() {
16+
if n < costs[0].len() && costs.iter().all(|cost| bot >= cost[n]) {
17+
continue;
18+
}
19+
20+
let mut needed_time = 0;
21+
for ((&cost, &inventory), &bots) in cost.iter().zip(&inventory).zip(&bots) {
22+
if inventory < cost {
23+
if bots == 0 {
24+
continue 'outer;
25+
}
26+
let needed_resources = cost - inventory;
27+
needed_time = std::cmp::max(
28+
needed_time,
29+
needed_resources / bots + if needed_resources % bots != 0 { 1 } else { 0 },
30+
)
31+
}
32+
}
33+
needed_time += 1;
34+
35+
if n == bots.len() - 1 {
36+
time_to_geode_bot = needed_time;
37+
} else if needed_time >= time_to_geode_bot {
38+
continue;
39+
}
40+
41+
if time + needed_time >= MAX_TIME {
42+
continue;
43+
}
44+
45+
let mut new_inventory = inventory;
46+
for (inv, &bots) in new_inventory.iter_mut().zip(&bots) {
47+
*inv += bots * needed_time;
48+
}
49+
for (inv, &cost) in new_inventory.iter_mut().zip(&cost) {
50+
*inv -= cost;
51+
}
52+
53+
let mut new_bots = bots;
54+
new_bots[n] += 1;
55+
56+
let remaining_time = MAX_TIME - (time + needed_time);
57+
if new_inventory.last().unwrap() +
58+
new_bots.last().unwrap() * remaining_time +
59+
remaining_time * (remaining_time - 1) / 2 <= *best {
60+
continue;
61+
}
62+
63+
find_max(costs, new_inventory, new_bots, time + needed_time, level + 1, best);
64+
built_bot = true;
65+
}
66+
67+
if !built_bot {
68+
*best = std::cmp::max(*best, inventory.last().unwrap() + bots.last().unwrap() * (MAX_TIME - time));
69+
}
70+
}
71+
72+
fn main() {
73+
let mut input = String::new();
74+
std::io::stdin().read_to_string(&mut input).unwrap();
75+
76+
println!("{}", input.lines().map(|line| {
77+
let mut words = line.split_whitespace();
78+
let id: u16 = words.nth(1).unwrap().trim_end_matches(':').parse().unwrap();
79+
let costs: [[u16; 3]; 4] = [
80+
[words.nth(4).unwrap().parse().unwrap(), 0, 0],
81+
[words.nth(5).unwrap().parse().unwrap(), 0, 0],
82+
[words.nth(5).unwrap().parse().unwrap(), words.nth(2).unwrap().parse().unwrap(), 0],
83+
[words.nth(5).unwrap().parse().unwrap(), 0, words.nth(2).unwrap().parse().unwrap()],
84+
];
85+
86+
let mut result = 0;
87+
find_max(&costs, [0; 4], [1, 0, 0, 0], 0, 0, &mut result);
88+
id * result
89+
}).sum::<u16>());
90+
}

day19b/Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "day19b"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

day19b/src/main.rs

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use std::io::Read;
2+
3+
fn find_max(
4+
costs: &[[u16; 3]; 4],
5+
inventory: [u16; 4],
6+
bots: [u16; 4],
7+
time: u16,
8+
level: u8,
9+
best: &mut u16,
10+
) {
11+
const MAX_TIME: u16 = 32;
12+
let mut time_to_geode_bot = u16::MAX;
13+
let mut built_bot = false;
14+
15+
'outer: for (n, (&cost, &bot)) in costs.iter().zip(&bots).enumerate().rev() {
16+
if n < costs[0].len() && costs.iter().all(|cost| bot >= cost[n]) {
17+
continue;
18+
}
19+
20+
let mut needed_time = 0;
21+
for ((&cost, &inventory), &bots) in cost.iter().zip(&inventory).zip(&bots) {
22+
if inventory < cost {
23+
if bots == 0 {
24+
continue 'outer;
25+
}
26+
let needed_resources = cost - inventory;
27+
needed_time = std::cmp::max(
28+
needed_time,
29+
needed_resources / bots + if needed_resources % bots != 0 { 1 } else { 0 },
30+
)
31+
}
32+
}
33+
needed_time += 1;
34+
35+
if n == bots.len() - 1 {
36+
time_to_geode_bot = needed_time;
37+
} else if needed_time >= time_to_geode_bot {
38+
continue;
39+
}
40+
41+
if time + needed_time >= MAX_TIME {
42+
continue;
43+
}
44+
45+
let mut new_inventory = inventory;
46+
for (inv, &bots) in new_inventory.iter_mut().zip(&bots) {
47+
*inv += bots * needed_time;
48+
}
49+
for (inv, &cost) in new_inventory.iter_mut().zip(&cost) {
50+
*inv -= cost;
51+
}
52+
53+
let mut new_bots = bots;
54+
new_bots[n] += 1;
55+
56+
let remaining_time = MAX_TIME - (time + needed_time);
57+
if new_inventory.last().unwrap() +
58+
new_bots.last().unwrap() * remaining_time +
59+
remaining_time * (remaining_time - 1) / 2 <= *best {
60+
continue;
61+
}
62+
63+
find_max(costs, new_inventory, new_bots, time + needed_time, level + 1, best);
64+
built_bot = true;
65+
}
66+
67+
if !built_bot {
68+
*best = std::cmp::max(*best, inventory.last().unwrap() + bots.last().unwrap() * (MAX_TIME - time));
69+
}
70+
}
71+
72+
fn main() {
73+
let mut input = String::new();
74+
std::io::stdin().read_to_string(&mut input).unwrap();
75+
76+
println!("{}", input.lines().take(3).map(|line| {
77+
let mut words = line.split_whitespace();
78+
let costs: [[u16; 3]; 4] = [
79+
[words.nth(6).unwrap().parse().unwrap(), 0, 0],
80+
[words.nth(5).unwrap().parse().unwrap(), 0, 0],
81+
[words.nth(5).unwrap().parse().unwrap(), words.nth(2).unwrap().parse().unwrap(), 0],
82+
[words.nth(5).unwrap().parse().unwrap(), 0, words.nth(2).unwrap().parse().unwrap()],
83+
];
84+
85+
let mut result = 0;
86+
find_max(&costs, [0; 4], [1, 0, 0, 0], 0, 0, &mut result);
87+
result
88+
}).product::<u16>());
89+
}

0 commit comments

Comments
 (0)