From 14a80c2cca5118e2f35c37159268783b0a84cc46 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sun, 11 Dec 2022 14:15:08 -0800 Subject: [PATCH] parses input for day11 --- 2022-aoc/src/d11.rs | 114 ++++++++++++++++++++++++++++++++++++++++++++ 2022-aoc/src/lib.rs | 1 + 2 files changed, 115 insertions(+) create mode 100644 2022-aoc/src/d11.rs diff --git a/2022-aoc/src/d11.rs b/2022-aoc/src/d11.rs new file mode 100644 index 0000000..08c6f70 --- /dev/null +++ b/2022-aoc/src/d11.rs @@ -0,0 +1,114 @@ +use std::collections::VecDeque; + +use aoc_runner_derive::{aoc as aoc_run, aoc_generator}; + +#[derive(Debug, Clone, Default)] +struct Monkey { + items: VecDeque, + op: Operation, + tmod: u32, + target: (usize, usize), +} + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] +struct Item(u32); + +#[derive(Clone, Debug, PartialEq, Eq)] +enum Operation { + Add(Option), + Mul(Option), +} + +impl Default for Operation { + fn default() -> Self { + Operation::Add(Some(0)) + } +} + +#[aoc_generator(day11)] +fn parse_input(input: &str) -> Vec { + let mut out = Vec::new(); + let mut monkey = Monkey::default(); + + for line in input.lines() { + let monkey = &mut monkey; + let line = line.trim(); + + if line.starts_with("Monkey") { + continue; + } + if line.starts_with("Starting") { + monkey.items = parse_items(line); + continue; + } + if line.starts_with("Operation") { + monkey.op = parse_operation(line); + continue; + } + if line.starts_with("Test") { + monkey.tmod = line.split_whitespace().last().unwrap().parse().unwrap(); + continue; + } + if line.starts_with("If true") { + monkey.target.0 = line.split_whitespace().last().unwrap().parse().unwrap(); + continue; + } + if line.starts_with("If false") { + monkey.target.1 = line.split_whitespace().last().unwrap().parse().unwrap(); + let m = monkey.clone(); + out.push(m); + *monkey = Monkey::default(); + continue; + } + } + + out +} + +fn parse_items(items: &str) -> VecDeque { + let mut out = VecDeque::new(); + let (_, items) = items.split_once(" items: ").unwrap(); + for item in items.split(", ") { + out.push_back(Item(item.parse().unwrap())); + } + out +} + +fn parse_operation(op: &str) -> Operation { + let (_, op) = op.split_once(" new = old ").unwrap(); + let (op, val) = op.split_once(' ').unwrap(); + match op { + "*" => Operation::Mul(val.parse().ok()), + "+" => Operation::Add(val.parse().ok()), + _ => unreachable!(), + } +} + +#[aoc_run(day11, part1)] +fn part1(troop: &[Monkey]) -> u32 { + 0 +} + +#[aoc_run(day11, part2)] +fn part2(troop: &[Monkey]) -> u32 { + 0 +} + +#[cfg(test)] +mod test { + use super::*; + const INPUT: &str = ""; + + #[test] + fn part1_test() { + let v = parse_input(INPUT); + + assert_eq!(part1(&v), 1); + } + + #[test] + fn part2_test() { + let v = parse_input(INPUT); + assert_eq!(part2(&v), 1); + } +} diff --git a/2022-aoc/src/lib.rs b/2022-aoc/src/lib.rs index d33246d..aa6aaf3 100644 --- a/2022-aoc/src/lib.rs +++ b/2022-aoc/src/lib.rs @@ -10,5 +10,6 @@ mod d7; mod d8; mod d9; mod d10; +mod d11; aoc_lib! { year = 2022 }