2022 day 16 parsing

This commit is contained in:
Maciej Jur 2022-12-16 11:47:01 +01:00
parent bfe96fbdf5
commit 1522be4221
3 changed files with 81 additions and 1 deletions

View file

@ -17,5 +17,6 @@ fn main() {
// solutions::day12::run(); // solutions::day12::run();
// solutions::day13::run(); // solutions::day13::run();
// solutions::day14::run(); // solutions::day14::run();
solutions::day15::run(); // solutions::day15::run();
solutions::day16::run();
} }

View file

@ -0,0 +1,78 @@
use crate::utils;
pub fn run() -> () {
let lines = utils::read_lines(utils::Source::Day(-1));
let data = parse_data(&lines);
println!("Day 16");
println!("Part 1: {}", solve1(&data));
println!("Part 2: {}", solve2(&data));
}
struct Valve<'data> {
name: &'data str,
rate: u8,
next: Vec<&'data str>
}
fn solve1(data: &[Valve]) -> i32 {
1
}
fn solve2(data: &[Valve]) -> i32 {
2
}
fn parse_data<T: AsRef<str>>(data: &[T]) -> Vec<Valve> {
data.iter()
.map(|line| {
let mut line = line.as_ref().split(" ");;
let name = line.nth(1).unwrap();
let rate = line.nth(2).unwrap()
.rsplit("=")
.next().unwrap()
.split(";")
.next().unwrap()
.parse::<u8>().unwrap();
let next = line.skip(4)
.map(|str| str.split(",").next().unwrap())
.collect::<Vec<_>>();
Valve { name, rate, next }
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
static DATA: &[&str] = &[
"Valve AA has flow rate=0; tunnels lead to valves DD, II, BB",
"Valve BB has flow rate=13; tunnels lead to valves CC, AA",
"Valve CC has flow rate=2; tunnels lead to valves DD, BB",
"Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE",
"Valve EE has flow rate=3; tunnels lead to valves FF, DD",
"Valve FF has flow rate=0; tunnels lead to valves EE, GG",
"Valve GG has flow rate=0; tunnels lead to valves FF, HH",
"Valve HH has flow rate=22; tunnel leads to valve GG",
"Valve II has flow rate=0; tunnels lead to valves AA, JJ",
"Valve JJ has flow rate=21; tunnel leads to valve II"
];
#[test]
fn part1() {
let data = parse_data(DATA);
assert_eq!(1, solve1(&data));
}
#[test]
fn part2() {
let data = parse_data(DATA);
assert_eq!(2, solve2(&data));
}
}

View file

@ -13,3 +13,4 @@ pub mod day12;
pub mod day13; pub mod day13;
pub mod day14; pub mod day14;
pub mod day15; pub mod day15;
pub mod day16;