day(11): refactor parsing
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
585e27eed9
commit
d56154f5c2
1 changed files with 28 additions and 34 deletions
|
@ -4,6 +4,8 @@ use aoc_2022::*;
|
|||
|
||||
use color_eyre::{eyre::eyre, Report};
|
||||
use itertools::Itertools;
|
||||
use lazy_static::lazy_static;
|
||||
use regex::Regex;
|
||||
use tracing::debug;
|
||||
|
||||
type Input = Vec<Monkey>;
|
||||
|
@ -32,14 +34,13 @@ impl FromStr for Operation {
|
|||
type Err = Report;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let (_, rhs) = s
|
||||
.split(" = ")
|
||||
.collect_tuple()
|
||||
.ok_or(eyre!("couldn't split operation"))?;
|
||||
let (l, op, r) = rhs
|
||||
.split_ascii_whitespace()
|
||||
.collect_tuple()
|
||||
.ok_or(eyre!("couldn't split the expression"))?;
|
||||
lazy_static! {
|
||||
static ref OPERATION_REGEX: Regex =
|
||||
Regex::new(r"(?P<l>.*) (?P<op>\+|\*) (?P<r>.*)").unwrap();
|
||||
}
|
||||
|
||||
let caps = OPERATION_REGEX.captures(s).unwrap();
|
||||
let (l, op, r) = (&caps["l"], &caps["op"], &caps["r"]);
|
||||
|
||||
if l == r {
|
||||
match op {
|
||||
|
@ -59,7 +60,6 @@ impl FromStr for Operation {
|
|||
|
||||
#[derive(Clone)]
|
||||
struct Test(usize, usize, usize);
|
||||
|
||||
impl Test {
|
||||
fn get(&self, worry: usize) -> usize {
|
||||
let &Test(div, t, f) = self;
|
||||
|
@ -72,21 +72,6 @@ impl Test {
|
|||
}
|
||||
}
|
||||
|
||||
impl FromStr for Test {
|
||||
type Err = Report;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let (divisor, t_branch, f_branch) = s
|
||||
.split('\n')
|
||||
.map(|l| l.split_ascii_whitespace().last().unwrap())
|
||||
.map(|n| n.parse().unwrap())
|
||||
.collect_tuple()
|
||||
.ok_or(eyre!("couldn't parse the test"))?;
|
||||
|
||||
Ok(Test(divisor, t_branch, f_branch))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Monkey {
|
||||
items: Vec<usize>,
|
||||
|
@ -100,20 +85,29 @@ impl FromStr for Monkey {
|
|||
type Err = Report;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let (_, items_s, op_s, test_s, t_s, f_s) = s
|
||||
.split('\n')
|
||||
.collect_tuple()
|
||||
.ok_or(eyre!("Couldn't split string correctly"))?;
|
||||
lazy_static! {
|
||||
static ref MONKEY_REGEX: Regex = Regex::new(concat!(
|
||||
r"(?s)",
|
||||
r"Starting items: (?P<items>[^\n]+).*",
|
||||
r"Operation: new = (?P<operation>[^\n]*).*",
|
||||
r"Test: divisible by (?P<div>\d+).*",
|
||||
r"If true: throw to monkey (?P<if_1>\d+).*",
|
||||
r"If false: throw to monkey (?P<if_0>\d+)",
|
||||
))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let items = items_s
|
||||
.split(": ")
|
||||
.nth(1)
|
||||
.ok_or(eyre!("No items present"))?
|
||||
let caps = MONKEY_REGEX.captures(s).unwrap();
|
||||
let items = caps["items"]
|
||||
.split(", ")
|
||||
.map(|x| x.parse().unwrap())
|
||||
.collect_vec();
|
||||
let operation = op_s.parse()?;
|
||||
let test = vec![test_s, t_s, f_s].join("\n").parse()?;
|
||||
let operation = caps["operation"].parse()?;
|
||||
let test = Test(
|
||||
caps["div"].parse()?,
|
||||
caps["if_1"].parse()?,
|
||||
caps["if_0"].parse()?,
|
||||
);
|
||||
|
||||
Ok(Monkey {
|
||||
items,
|
||||
|
|
Loading…
Reference in a new issue