Day 14 part 1
This commit is contained in:
parent
9cdb935254
commit
dbd597ad41
102
2021/input/2021/day14.txt
Normal file
102
2021/input/2021/day14.txt
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
VHCKBFOVCHHKOHBPNCKO
|
||||||
|
|
||||||
|
SO -> F
|
||||||
|
OP -> V
|
||||||
|
NF -> F
|
||||||
|
BO -> V
|
||||||
|
BH -> S
|
||||||
|
VB -> B
|
||||||
|
SV -> B
|
||||||
|
BK -> S
|
||||||
|
KC -> N
|
||||||
|
SP -> O
|
||||||
|
CP -> O
|
||||||
|
VN -> O
|
||||||
|
HO -> S
|
||||||
|
PC -> B
|
||||||
|
CS -> O
|
||||||
|
PO -> K
|
||||||
|
KF -> B
|
||||||
|
BP -> K
|
||||||
|
VO -> O
|
||||||
|
HB -> N
|
||||||
|
PH -> O
|
||||||
|
FF -> O
|
||||||
|
FB -> K
|
||||||
|
CC -> H
|
||||||
|
FK -> F
|
||||||
|
HV -> P
|
||||||
|
CO -> S
|
||||||
|
OC -> N
|
||||||
|
KV -> V
|
||||||
|
SS -> O
|
||||||
|
FC -> O
|
||||||
|
NP -> B
|
||||||
|
OH -> B
|
||||||
|
OF -> K
|
||||||
|
KB -> K
|
||||||
|
BN -> C
|
||||||
|
OK -> C
|
||||||
|
NC -> O
|
||||||
|
NO -> O
|
||||||
|
FS -> C
|
||||||
|
VP -> K
|
||||||
|
KP -> S
|
||||||
|
VS -> B
|
||||||
|
VV -> N
|
||||||
|
NN -> P
|
||||||
|
KH -> P
|
||||||
|
OB -> H
|
||||||
|
HP -> H
|
||||||
|
KK -> H
|
||||||
|
FH -> F
|
||||||
|
KS -> V
|
||||||
|
BS -> V
|
||||||
|
SN -> H
|
||||||
|
CB -> B
|
||||||
|
HN -> K
|
||||||
|
SB -> O
|
||||||
|
OS -> K
|
||||||
|
BC -> H
|
||||||
|
OV -> N
|
||||||
|
PN -> B
|
||||||
|
VH -> N
|
||||||
|
SK -> C
|
||||||
|
PV -> K
|
||||||
|
VC -> N
|
||||||
|
PF -> S
|
||||||
|
NB -> B
|
||||||
|
PP -> S
|
||||||
|
NS -> F
|
||||||
|
PB -> B
|
||||||
|
CV -> C
|
||||||
|
HK -> P
|
||||||
|
PK -> S
|
||||||
|
NH -> B
|
||||||
|
SH -> V
|
||||||
|
KO -> H
|
||||||
|
NV -> B
|
||||||
|
HH -> V
|
||||||
|
FO -> O
|
||||||
|
CK -> O
|
||||||
|
VK -> F
|
||||||
|
HF -> O
|
||||||
|
BF -> C
|
||||||
|
BV -> P
|
||||||
|
KN -> K
|
||||||
|
VF -> C
|
||||||
|
FN -> V
|
||||||
|
ON -> C
|
||||||
|
SF -> F
|
||||||
|
SC -> C
|
||||||
|
OO -> S
|
||||||
|
FP -> K
|
||||||
|
PS -> C
|
||||||
|
NK -> O
|
||||||
|
BB -> V
|
||||||
|
HC -> H
|
||||||
|
FV -> V
|
||||||
|
CH -> N
|
||||||
|
HS -> V
|
||||||
|
CF -> F
|
||||||
|
CN -> S
|
||||||
104
2021/src/day14.rs
Normal file
104
2021/src/day14.rs
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
|
fmt::{Debug, Error, Formatter},
|
||||||
|
num::ParseIntError,
|
||||||
|
ops::{Index, IndexMut},
|
||||||
|
str::FromStr,
|
||||||
|
};
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
|
use aoc_runner_derive::{aoc, aoc_generator};
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
fn expand(template: &[u8], rules: &HashMap<&[u8], u8>) -> Vec<u8> {
|
||||||
|
let mut res = vec![0u8; template.len() * 2 - 1];
|
||||||
|
template.windows(2).enumerate().for_each(|(i, xy)| {
|
||||||
|
let z = rules[xy];
|
||||||
|
res[i * 2] = xy[0];
|
||||||
|
res[i * 2 + 1] = z;
|
||||||
|
res[i * 2 + 2] = xy[1];
|
||||||
|
});
|
||||||
|
//dbg!(String::from_utf8_lossy(&res));
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
|
fn count(template: &[u8]) -> (usize, usize) {
|
||||||
|
let m = template
|
||||||
|
.iter()
|
||||||
|
.fold(HashMap::<u8, usize>::new(), |mut m, v| {
|
||||||
|
*m.entry(*v).or_insert(0) += 1;
|
||||||
|
m
|
||||||
|
});
|
||||||
|
m.values()
|
||||||
|
.fold((usize::MAX, 0), |(min, max), v| (min.min(*v), max.max(*v)))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[aoc(day14, part1)]
|
||||||
|
fn part1(input: &str) -> Result<usize> {
|
||||||
|
let (template, rules) = input.split_once("\n\n").unwrap();
|
||||||
|
let rules: HashMap<&[u8], u8> = rules
|
||||||
|
.lines()
|
||||||
|
.map(|l| {
|
||||||
|
let (pair, insert) = l.split_once(" -> ").unwrap();
|
||||||
|
(pair.as_bytes(), insert.as_bytes()[0])
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
let mut template = template.as_bytes().to_vec();
|
||||||
|
for i in 1..11 {
|
||||||
|
template = expand(&template, &rules);
|
||||||
|
//println!("After step {}: {}", i, String::from_utf8_lossy(&template));
|
||||||
|
}
|
||||||
|
let (min, max) = count(&template);
|
||||||
|
Ok(max - min)
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
#[aoc(day14, part2)]
|
||||||
|
fn part2(input: &[u64]) -> Result<u64> {
|
||||||
|
todo!("part2");
|
||||||
|
Ok(0)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_part1() -> Result<()> {
|
||||||
|
let input = r#"
|
||||||
|
NNCB
|
||||||
|
|
||||||
|
CH -> B
|
||||||
|
HH -> N
|
||||||
|
CB -> H
|
||||||
|
NH -> C
|
||||||
|
HB -> C
|
||||||
|
HC -> B
|
||||||
|
HN -> C
|
||||||
|
NN -> C
|
||||||
|
BH -> H
|
||||||
|
NC -> B
|
||||||
|
NB -> B
|
||||||
|
BN -> B
|
||||||
|
BB -> N
|
||||||
|
BC -> B
|
||||||
|
CC -> N
|
||||||
|
CN -> C
|
||||||
|
"#
|
||||||
|
.trim();
|
||||||
|
assert_eq!(part1(input)?, 1588);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
#[test]
|
||||||
|
fn test_part2()->Result<()> {
|
||||||
|
let input = r#"
|
||||||
|
"#
|
||||||
|
.trim();
|
||||||
|
assert_eq!(part2(&parse(input)?)?, u64::MAX);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
@ -2,6 +2,7 @@ pub mod day1;
|
|||||||
pub mod day10;
|
pub mod day10;
|
||||||
pub mod day11;
|
pub mod day11;
|
||||||
pub mod day12;
|
pub mod day12;
|
||||||
|
pub mod day14;
|
||||||
pub mod day2;
|
pub mod day2;
|
||||||
pub mod day3;
|
pub mod day3;
|
||||||
pub mod day4;
|
pub mod day4;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user