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 color_eyre::{eyre::eyre, Report};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
use regex::Regex;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
type Input = Vec<Monkey>;
|
type Input = Vec<Monkey>;
|
||||||
|
@ -32,14 +34,13 @@ impl FromStr for Operation {
|
||||||
type Err = Report;
|
type Err = Report;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
let (_, rhs) = s
|
lazy_static! {
|
||||||
.split(" = ")
|
static ref OPERATION_REGEX: Regex =
|
||||||
.collect_tuple()
|
Regex::new(r"(?P<l>.*) (?P<op>\+|\*) (?P<r>.*)").unwrap();
|
||||||
.ok_or(eyre!("couldn't split operation"))?;
|
}
|
||||||
let (l, op, r) = rhs
|
|
||||||
.split_ascii_whitespace()
|
let caps = OPERATION_REGEX.captures(s).unwrap();
|
||||||
.collect_tuple()
|
let (l, op, r) = (&caps["l"], &caps["op"], &caps["r"]);
|
||||||
.ok_or(eyre!("couldn't split the expression"))?;
|
|
||||||
|
|
||||||
if l == r {
|
if l == r {
|
||||||
match op {
|
match op {
|
||||||
|
@ -59,7 +60,6 @@ impl FromStr for Operation {
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Test(usize, usize, usize);
|
struct Test(usize, usize, usize);
|
||||||
|
|
||||||
impl Test {
|
impl Test {
|
||||||
fn get(&self, worry: usize) -> usize {
|
fn get(&self, worry: usize) -> usize {
|
||||||
let &Test(div, t, f) = self;
|
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)]
|
#[derive(Clone)]
|
||||||
struct Monkey {
|
struct Monkey {
|
||||||
items: Vec<usize>,
|
items: Vec<usize>,
|
||||||
|
@ -100,20 +85,29 @@ impl FromStr for Monkey {
|
||||||
type Err = Report;
|
type Err = Report;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
let (_, items_s, op_s, test_s, t_s, f_s) = s
|
lazy_static! {
|
||||||
.split('\n')
|
static ref MONKEY_REGEX: Regex = Regex::new(concat!(
|
||||||
.collect_tuple()
|
r"(?s)",
|
||||||
.ok_or(eyre!("Couldn't split string correctly"))?;
|
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
|
let caps = MONKEY_REGEX.captures(s).unwrap();
|
||||||
.split(": ")
|
let items = caps["items"]
|
||||||
.nth(1)
|
|
||||||
.ok_or(eyre!("No items present"))?
|
|
||||||
.split(", ")
|
.split(", ")
|
||||||
.map(|x| x.parse().unwrap())
|
.map(|x| x.parse().unwrap())
|
||||||
.collect_vec();
|
.collect_vec();
|
||||||
let operation = op_s.parse()?;
|
let operation = caps["operation"].parse()?;
|
||||||
let test = vec![test_s, t_s, f_s].join("\n").parse()?;
|
let test = Test(
|
||||||
|
caps["div"].parse()?,
|
||||||
|
caps["if_1"].parse()?,
|
||||||
|
caps["if_0"].parse()?,
|
||||||
|
);
|
||||||
|
|
||||||
Ok(Monkey {
|
Ok(Monkey {
|
||||||
items,
|
items,
|
||||||
|
|
Loading…
Reference in a new issue