Skip to content

Commit 38cde79

Browse files
committed
feat(2022): refactor days 19 and 25
Refactored Days 19 and 25 of 2022 to use the common framework. Successfully migrated solutions, integrated them into the runner, and verified results.
1 parent 9f9f108 commit 38cde79

6 files changed

Lines changed: 295 additions & 0 deletions

File tree

2022/inputs/19/prod.txt

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

2022/inputs/19/test.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
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.
2+
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.

2022/inputs/25/prod.txt

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
1==-20221
2+
1101-20012
3+
21-12--21=2-0-0-=0
4+
2-12101=1-
5+
1==-2
6+
12-=0122=1=0-21=
7+
120=11=-1=1012-12
8+
100=22212-20=-01
9+
10-==-0-21111221-=
10+
1-=
11+
21-2-20-
12+
2-0202-=102-
13+
1--1-110-=12=
14+
2-2122=
15+
1=1===2
16+
2--===-10=-111--10
17+
10--1--1101100
18+
21-0==-1-=2211
19+
2-2202=-0-0=0
20+
2-2-1-=22=02--0
21+
1=2-=10-02120----
22+
10==1002=-
23+
2-0=21-=-0
24+
1=-=2201112-221==02
25+
10-12-22=2=-
26+
12-2-00000=-
27+
1=10=2020200==01-022
28+
1=2=
29+
100220
30+
2-1-2
31+
1-12-021--=
32+
1-022---2=1=010==
33+
2201100==21
34+
11==-002==0==20=0
35+
1=22-01-1==2-=1=20
36+
221-
37+
11=
38+
2==2-0021--
39+
1---212--
40+
202=11--=1-101-01
41+
1-----10==12-021=
42+
1-02=-00--
43+
1=0
44+
102---
45+
2-2=1==11100
46+
1=11=2
47+
11200-=102==02=-20
48+
11021100-00----0
49+
121-0=00=22=10=-
50+
2=-=21=
51+
212202=1--1002=
52+
10-=2=12--
53+
2002-2
54+
102-=-
55+
1=0022
56+
2011--0-2==012021
57+
12=-0
58+
1==--110=2-21
59+
1-12-11==22-=2001
60+
2=1112==22-0020-2
61+
2--21=
62+
1==0-0
63+
1=2=-11-=2
64+
2=120001=20=01-
65+
111-1-0=
66+
1-1-00=-2=002
67+
1==-212=02=1=20-
68+
22=
69+
1--2
70+
10--=11002=1-0-011=
71+
1=01-1=0==0
72+
1-=0-2---2=12
73+
1-2-=-
74+
22222
75+
12002=0=021=22022
76+
1=-0112021-
77+
12=-20=0-=2=1
78+
1=
79+
22
80+
1==1020--0-1=21=21
81+
1=202=0021=2
82+
1-22-022-1=01
83+
1==-==1-02=
84+
1-02-
85+
21
86+
1=2=010=
87+
2=-==02211
88+
1-22=--10=1=110-=
89+
20-1222=-=1=2-12
90+
2012=1-=
91+
111=1=-100=2
92+
11
93+
20-=-20-111202
94+
1=01
95+
1-12=1-=10-0-1
96+
10-02-2
97+
200=01=
98+
1=0002=01222-=100=
99+
11-0-=1=011002=-0
100+
10-=2-=-
101+
1====2==0=1=02=-=20
102+
11=-020012
103+
21-11-22-0=0=2
104+
1=20011
105+
11-102021=122=-
106+
2-2-12021
107+
10--1=21=
108+
100-111
109+
1-2-201=0101-1
110+
2=02-00-1=02=2=221=
111+
1-=200=10=0-20
112+
22=0-==-10=2-1
113+
1==
114+
11=002=201-2
115+
2-12
116+
211=-12=20=-2=-01-
117+
102-100
118+
10=12210=20=-2

2022/inputs/25/test.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
1=-0-2
2+
12111
3+
2=0=
4+
21
5+
2=01
6+
111
7+
20012
8+
112
9+
1=-1=
10+
1-12
11+
12
12+
1=
13+
122

2022/src/day19.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use aoc_rust_common::Solution;
2+
use std::fmt::Display;
3+
use std::collections::VecDeque;
4+
5+
pub struct Day19;
6+
7+
#[derive(Clone, Copy, Debug)]
8+
struct State {
9+
inventory: [u16; 4],
10+
bots: [u16; 4],
11+
elapsed: u16,
12+
}
13+
14+
fn max_geodes(blueprint: &[[u16; 4]; 4], max_time: u16) -> u16 {
15+
let mut max_robots = [u16::MAX; 4];
16+
for i in 0..3 {
17+
max_robots[i] = blueprint.iter().map(|cost| cost[i]).max().unwrap();
18+
}
19+
let mut max_geodes = 0;
20+
21+
let mut q = VecDeque::new();
22+
q.push_back(State {
23+
inventory: [0, 0, 0, 0],
24+
bots: [1, 0, 0, 0],
25+
elapsed: 0,
26+
});
27+
28+
while let Some(State { inventory, bots, elapsed }) = q.pop_front() {
29+
for i in 0..blueprint.len() {
30+
if bots[i] == max_robots[i] { continue; }
31+
32+
let costs = &blueprint[i];
33+
let wait_time = (0..3)
34+
.map(|idx| {
35+
match costs[idx] {
36+
cost if cost <= inventory[idx] => 0,
37+
_ if bots[idx] == 0 => max_time + 1,
38+
cost => (cost - inventory[idx] + bots[idx] - 1) / bots[idx],
39+
}
40+
})
41+
.max()
42+
.unwrap();
43+
44+
let new_elapsed = elapsed + wait_time + 1;
45+
if new_elapsed >= max_time { continue; }
46+
47+
let mut new_inventory = [0; 4];
48+
for idx in 0..bots.len() {
49+
new_inventory[idx] = inventory[idx] + bots[idx] * (wait_time + 1) - costs[idx];
50+
}
51+
52+
let mut new_bots = bots;
53+
new_bots[i] += 1;
54+
55+
let remaining_time = max_time - new_elapsed;
56+
if ((remaining_time - 1) * remaining_time) / 2 + new_inventory[3] + remaining_time * new_bots[3] < max_geodes {
57+
continue;
58+
}
59+
60+
q.push_back(State { inventory: new_inventory, bots: new_bots, elapsed: new_elapsed })
61+
}
62+
max_geodes = max_geodes.max(inventory[3] + bots[3] * (max_time - elapsed));
63+
}
64+
max_geodes
65+
}
66+
67+
fn parse(input: &str) -> Vec<[[u16; 4]; 4]> {
68+
input.lines().map(|line| {
69+
let mut iter = line.split_ascii_whitespace();
70+
let ore_bot_costs = [iter.nth(6).unwrap().parse().unwrap(), 0, 0, 0];
71+
let clay_bot_costs = [iter.nth(5).unwrap().parse().unwrap(), 0, 0, 0];
72+
let obsidian_bot_costs = [iter.nth(5).unwrap().parse().unwrap(), iter.nth(2).unwrap().parse().unwrap(), 0, 0];
73+
let geode_bot_costs = [iter.nth(5).unwrap().parse().unwrap(), 0, iter.nth(2).unwrap().parse().unwrap(), 0];
74+
[ore_bot_costs, clay_bot_costs, obsidian_bot_costs, geode_bot_costs]
75+
}).collect()
76+
}
77+
78+
impl Solution for Day19 {
79+
fn year(&self) -> u32 { 2022 }
80+
fn day(&self) -> u32 { 19 }
81+
82+
fn part1(&self, input: &str) -> Box<dyn Display> {
83+
let blueprints = parse(input);
84+
Box::new(blueprints.iter().enumerate().map(|(idx, blueprint)| (idx + 1) as u16 * max_geodes(blueprint, 24)).sum::<u16>() as i64)
85+
}
86+
87+
fn part2(&self, input: &str) -> Box<dyn Display> {
88+
let blueprints = parse(input);
89+
Box::new(blueprints.iter().take(3).map(|blueprint| max_geodes(blueprint, 32) as u64).product::<u64>() as i64)
90+
}
91+
}

2022/src/day25.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use aoc_rust_common::Solution;
2+
use std::fmt::Display;
3+
4+
pub struct Day25;
5+
6+
fn snafu_to_decimal(snafu: &str) -> i64 {
7+
snafu.chars().rev().enumerate().fold(0, |acc, (i, c)| {
8+
let val = match c {
9+
'0' => 0, '1' => 1, '2' => 2, '-' => -1, '=' => -2,
10+
_ => 0,
11+
};
12+
acc + val * 5_i64.pow(i as u32)
13+
})
14+
}
15+
16+
fn decimal_to_snafu(mut number: i64) -> String {
17+
let mut digits = Vec::new();
18+
while number != 0 {
19+
let mut rem = ((number + 2) % 5) - 2;
20+
number = (number - rem) / 5;
21+
digits.push(match rem {
22+
0 => '0', 1 => '1', 2 => '2', -1 => '-', -2 => '=',
23+
_ => unreachable!(),
24+
});
25+
}
26+
digits.iter().rev().collect()
27+
}
28+
29+
impl Solution for Day25 {
30+
fn year(&self) -> u32 { 2022 }
31+
fn day(&self) -> u32 { 25 }
32+
33+
fn part1(&self, input: &str) -> Box<dyn Display> {
34+
let total = input.lines().map(snafu_to_decimal).sum();
35+
Box::new(decimal_to_snafu(total))
36+
}
37+
38+
fn part2(&self, _input: &str) -> Box<dyn Display> {
39+
Box::new("There is no part 2 for Day 25!")
40+
}
41+
}

0 commit comments

Comments
 (0)