parses input for day11

This commit is contained in:
Joe Ardent 2022-12-11 14:15:08 -08:00
parent b2aa96255e
commit 14a80c2cca
2 changed files with 115 additions and 0 deletions

114
2022-aoc/src/d11.rs Normal file
View file

@ -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<Item>,
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<u32>),
Mul(Option<u32>),
}
impl Default for Operation {
fn default() -> Self {
Operation::Add(Some(0))
}
}
#[aoc_generator(day11)]
fn parse_input(input: &str) -> Vec<Monkey> {
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<Item> {
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);
}
}

View file

@ -10,5 +10,6 @@ mod d7;
mod d8; mod d8;
mod d9; mod d9;
mod d10; mod d10;
mod d11;
aoc_lib! { year = 2022 } aoc_lib! { year = 2022 }