mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 07:23:18 -09:00
add day 14
This commit is contained in:
parent
ed97d0d863
commit
2a6124b405
102
inputs/14.txt
Normal file
102
inputs/14.txt
Normal file
@ -0,0 +1,102 @@
|
||||
SCSCSKKVVBKVFKSCCSOV
|
||||
|
||||
CP -> C
|
||||
SF -> S
|
||||
BH -> F
|
||||
SS -> N
|
||||
KB -> N
|
||||
NO -> N
|
||||
BP -> F
|
||||
NK -> P
|
||||
VP -> H
|
||||
OF -> O
|
||||
VH -> O
|
||||
FV -> F
|
||||
OP -> V
|
||||
FP -> B
|
||||
VB -> B
|
||||
OK -> S
|
||||
BS -> B
|
||||
SK -> P
|
||||
VV -> H
|
||||
PC -> S
|
||||
HV -> K
|
||||
PS -> N
|
||||
VS -> O
|
||||
HF -> B
|
||||
SV -> C
|
||||
HP -> O
|
||||
NF -> V
|
||||
HB -> F
|
||||
VO -> B
|
||||
VN -> N
|
||||
ON -> H
|
||||
KV -> K
|
||||
OV -> F
|
||||
HO -> H
|
||||
NB -> K
|
||||
CB -> F
|
||||
FF -> H
|
||||
NH -> F
|
||||
SN -> N
|
||||
PO -> O
|
||||
PH -> C
|
||||
HH -> P
|
||||
KF -> N
|
||||
OH -> N
|
||||
KS -> O
|
||||
FH -> H
|
||||
CC -> F
|
||||
CK -> N
|
||||
FC -> F
|
||||
CF -> H
|
||||
HN -> B
|
||||
OC -> F
|
||||
OB -> K
|
||||
FO -> P
|
||||
KP -> N
|
||||
NC -> P
|
||||
PN -> O
|
||||
PV -> B
|
||||
CO -> C
|
||||
CS -> P
|
||||
PP -> V
|
||||
FN -> B
|
||||
PK -> C
|
||||
VK -> S
|
||||
HS -> P
|
||||
OS -> N
|
||||
NP -> K
|
||||
SB -> F
|
||||
OO -> F
|
||||
CV -> V
|
||||
BB -> O
|
||||
SH -> O
|
||||
NV -> N
|
||||
BN -> C
|
||||
KN -> H
|
||||
KC -> C
|
||||
BK -> O
|
||||
KO -> S
|
||||
VC -> N
|
||||
KK -> P
|
||||
BO -> V
|
||||
BC -> V
|
||||
BV -> H
|
||||
SC -> N
|
||||
NN -> C
|
||||
CH -> H
|
||||
SO -> P
|
||||
HC -> F
|
||||
FS -> P
|
||||
VF -> S
|
||||
BF -> S
|
||||
PF -> O
|
||||
SP -> H
|
||||
FK -> N
|
||||
NS -> C
|
||||
PB -> S
|
||||
HK -> C
|
||||
CN -> B
|
||||
FB -> O
|
||||
KH -> O
|
19
inputs/14_test.txt
Normal file
19
inputs/14_test.txt
Normal file
@ -0,0 +1,19 @@
|
||||
|
||||
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
|
81
src/day14.rs
Normal file
81
src/day14.rs
Normal file
@ -0,0 +1,81 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
type Transforms = HashMap<(char, char), char>;
|
||||
|
||||
fn part_one(input: String, transforms: Transforms) -> i32 {
|
||||
let mut input: Vec<char> = input.chars().collect();
|
||||
for _ in 1..=10 {
|
||||
let pairs = input.windows(2);
|
||||
let mut next = vec![input[0]];
|
||||
for pair in pairs {
|
||||
let key = (pair[0], pair[1]);
|
||||
if let Some(val) = transforms.get(&key) {
|
||||
next.push(*val);
|
||||
}
|
||||
next.push(pair[1]);
|
||||
}
|
||||
input = next;
|
||||
}
|
||||
let mut counts = HashMap::<char, i32>::new();
|
||||
for char in input {
|
||||
if char != '\n' {
|
||||
counts.insert(
|
||||
char,
|
||||
match counts.get(&char) {
|
||||
Some(val) => *val + 1,
|
||||
_ => 1,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
counts.iter().fold(0, |max, (_, count)| {
|
||||
let count = *count;
|
||||
if count > max {
|
||||
count
|
||||
} else {
|
||||
max
|
||||
}
|
||||
}) - counts.iter().fold(i32::max_value(), |min, (_, count)| {
|
||||
let count = *count;
|
||||
if count < min {
|
||||
count
|
||||
} else {
|
||||
min
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn part_two(input: String, transforms: Transforms) -> u128 {
|
||||
todo!()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn parse_input(input: &str) -> (String, Transforms) {
|
||||
let (input, transform_list) = input.split_once("\n\n").expect("two sections");
|
||||
let mut transforms = Transforms::new();
|
||||
for line in transform_list.split("\n") {
|
||||
let (input, output) = line.split_once(" -> ").expect("transform");
|
||||
let mut chars = input.chars();
|
||||
transforms.insert(
|
||||
(chars.next().unwrap(), chars.next().unwrap()),
|
||||
output.chars().next().unwrap(),
|
||||
);
|
||||
}
|
||||
(input.to_owned(), transforms)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part_one_test_sample() {
|
||||
let (input, transforms) = parse_input(include_str!("../inputs/14_test.txt"));
|
||||
assert_eq!(part_one(input, transforms), 1588);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part_one_test() {
|
||||
let (input, transforms) = parse_input(include_str!("../inputs/14.txt"));
|
||||
assert_eq!(part_one(input, transforms), 2112);
|
||||
}
|
||||
}
|
@ -9,3 +9,5 @@ pub(crate) mod day1;
|
||||
pub(crate) mod day9;
|
||||
pub(crate) mod day10;
|
||||
//pub(crate) mod day11; //todo, finish day11
|
||||
pub(crate) mod day13;
|
||||
pub(crate) mod day14;
|
||||
|
Loading…
x
Reference in New Issue
Block a user