mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Implement 2022 day 20
This commit is contained in:
30
2022/inputs/19.txt
Normal file
30
2022/inputs/19.txt
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
Blueprint 1: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 16 clay. Each geode robot costs 3 ore and 20 obsidian.
|
||||||
|
Blueprint 2: 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.
|
||||||
|
Blueprint 3: 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 3 ore and 8 obsidian.
|
||||||
|
Blueprint 4: 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 2 ore and 13 obsidian.
|
||||||
|
Blueprint 5: 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.
|
||||||
|
Blueprint 6: 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.
|
||||||
|
Blueprint 7: 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 2 ore and 15 obsidian.
|
||||||
|
Blueprint 8: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 11 clay. Each geode robot costs 2 ore and 10 obsidian.
|
||||||
|
Blueprint 9: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 16 clay. Each geode robot costs 3 ore and 9 obsidian.
|
||||||
|
Blueprint 10: Each ore robot costs 4 ore. Each clay robot costs 2 ore. Each obsidian robot costs 2 ore and 16 clay. Each geode robot costs 2 ore and 8 obsidian.
|
||||||
|
Blueprint 11: 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.
|
||||||
|
Blueprint 12: 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 20 obsidian.
|
||||||
|
Blueprint 13: 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.
|
||||||
|
Blueprint 14: 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.
|
||||||
|
Blueprint 15: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 10 clay. Each geode robot costs 2 ore and 7 obsidian.
|
||||||
|
Blueprint 16: 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.
|
||||||
|
Blueprint 17: 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 2 ore and 13 obsidian.
|
||||||
|
Blueprint 18: 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.
|
||||||
|
Blueprint 19: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 7 clay. Each geode robot costs 4 ore and 13 obsidian.
|
||||||
|
Blueprint 20: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 14 clay. Each geode robot costs 3 ore and 17 obsidian.
|
||||||
|
Blueprint 21: 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 19 obsidian.
|
||||||
|
Blueprint 22: 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.
|
||||||
|
Blueprint 23: 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.
|
||||||
|
Blueprint 24: 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 2 ore and 20 obsidian.
|
||||||
|
Blueprint 25: 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.
|
||||||
|
Blueprint 26: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 5 clay. Each geode robot costs 3 ore and 18 obsidian.
|
||||||
|
Blueprint 27: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 19 clay. Each geode robot costs 2 ore and 12 obsidian.
|
||||||
|
Blueprint 28: Each ore robot costs 2 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 15 clay. Each geode robot costs 2 ore and 20 obsidian.
|
||||||
|
Blueprint 29: 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.
|
||||||
|
Blueprint 30: 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.
|
||||||
5000
2022/inputs/20.txt
Normal file
5000
2022/inputs/20.txt
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,9 +1,135 @@
|
|||||||
|
use std::cmp::Ordering;
|
||||||
|
use std::fmt::Write;
|
||||||
|
use std::iter::once;
|
||||||
|
|
||||||
|
use anyhow::Context;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use nom::character::complete::newline;
|
||||||
|
use nom::multi::many0;
|
||||||
|
use nom::sequence::terminated;
|
||||||
|
use nom::IResult;
|
||||||
|
|
||||||
pub fn part1(_input: &[u8]) -> Result<String> {
|
use crate::common::parse_input;
|
||||||
anyhow::bail!("not implemented")
|
|
||||||
|
fn parse_encrypted(input: &[u8]) -> IResult<&[u8], Vec<i64>> {
|
||||||
|
many0(terminated(nom::character::complete::i64, newline))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part2(_input: &[u8]) -> Result<String> {
|
fn step(steps: &[usize], mut start: usize, count: usize) -> usize {
|
||||||
anyhow::bail!("not implemented")
|
for _ in 0..(count % (steps.len() - 1)) {
|
||||||
|
start = steps[start];
|
||||||
|
}
|
||||||
|
|
||||||
|
start
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
|
fn print(encrypted: &[i64], next: &[usize]) {
|
||||||
|
let mut base = String::new();
|
||||||
|
|
||||||
|
let mut start = 0;
|
||||||
|
|
||||||
|
for _ in 0..encrypted.len() {
|
||||||
|
if base != "" {
|
||||||
|
base += ", ";
|
||||||
|
}
|
||||||
|
|
||||||
|
write!(base, "{}", encrypted[start]).unwrap();
|
||||||
|
|
||||||
|
start = next[start];
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{base}");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn move_between(prev: &mut [usize], next: &mut [usize], i: usize, before: usize, after: usize) {
|
||||||
|
if before == i || after == i {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let before_i = prev[i];
|
||||||
|
let after_i = next[i];
|
||||||
|
|
||||||
|
// Remove i from its original place
|
||||||
|
prev[after_i] = before_i;
|
||||||
|
next[before_i] = after_i;
|
||||||
|
|
||||||
|
// Connect i properly to before
|
||||||
|
prev[before] = i;
|
||||||
|
next[i] = before;
|
||||||
|
|
||||||
|
// Connect i properly to after
|
||||||
|
prev[i] = after;
|
||||||
|
next[after] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part1(input: &[u8]) -> Result<String> {
|
||||||
|
let encrypted = parse_input(input, parse_encrypted)?;
|
||||||
|
|
||||||
|
shuffle(&encrypted, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn shuffle(encrypted: &[i64], times: usize) -> Result<String> {
|
||||||
|
let mut next: Vec<_> = (1..encrypted.len()).chain(once(0)).collect();
|
||||||
|
let mut prev: Vec<_> = once(encrypted.len() - 1)
|
||||||
|
.chain(0..(encrypted.len() - 1))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
for _ in 0..times {
|
||||||
|
for (i, &value) in encrypted.iter().enumerate() {
|
||||||
|
match value.cmp(&0) {
|
||||||
|
Ordering::Less => {
|
||||||
|
let before = step(&prev, i, (-value) as usize);
|
||||||
|
let after = prev[before];
|
||||||
|
|
||||||
|
move_between(&mut prev, &mut next, i, before, after);
|
||||||
|
}
|
||||||
|
Ordering::Equal => continue,
|
||||||
|
Ordering::Greater => {
|
||||||
|
let after = step(&next, i, value as usize);
|
||||||
|
let before = next[after];
|
||||||
|
|
||||||
|
move_between(&mut prev, &mut next, i, before, after);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// print(&encrypted, &next);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut start = encrypted
|
||||||
|
.iter()
|
||||||
|
.position(|&v| v == 0)
|
||||||
|
.context("Could not find zero")?;
|
||||||
|
|
||||||
|
let mut sum = 0;
|
||||||
|
|
||||||
|
for _ in 0..3 {
|
||||||
|
start = step(&next, start, 1000);
|
||||||
|
sum += encrypted[start];
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(sum.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part2(input: &[u8]) -> Result<String> {
|
||||||
|
const ENCRYPTION_KEY: i64 = 811589153;
|
||||||
|
|
||||||
|
let mut encrypted = parse_input(input, parse_encrypted)?;
|
||||||
|
|
||||||
|
encrypted.iter_mut().for_each(|v| *v *= ENCRYPTION_KEY);
|
||||||
|
|
||||||
|
shuffle(&encrypted, 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
const SAMPLE: &[u8] = include_bytes!("samples/20.txt");
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sample_part1() {
|
||||||
|
assert_eq!(part1(SAMPLE).unwrap(), "3");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
11
2022/src/samples/19.txt
Normal file
11
2022/src/samples/19.txt
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
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.
|
||||||
|
|
||||||
|
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.
|
||||||
7
2022/src/samples/20.txt
Normal file
7
2022/src/samples/20.txt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
1
|
||||||
|
2
|
||||||
|
-3
|
||||||
|
3
|
||||||
|
-2
|
||||||
|
0
|
||||||
|
4
|
||||||
Reference in New Issue
Block a user