From 19615ebdb5bdeff8aba26c5d228afc2902688e7c Mon Sep 17 00:00:00 2001 From: Pcornat Date: Mon, 15 Sep 2025 21:08:25 +0200 Subject: [PATCH] Update nom --- Cargo.toml | 4 ++-- src/prob5.rs | 35 ++++++++++++++++++++++------------- src/prob7.rs | 21 ++++++++++++--------- 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 76f5ab8..34bb019 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,8 +11,8 @@ incremental = true rpath = true [dependencies] -nom = "7.1.1" -itertools = "0.11.0" +nom = "8.0.0" +itertools = "0.14.0" camino = "1.1.6" id_tree = "1.8.0" color-eyre = "0.6.2" diff --git a/src/prob5.rs b/src/prob5.rs index 3080d39..7dfb3d1 100644 --- a/src/prob5.rs +++ b/src/prob5.rs @@ -5,8 +5,8 @@ use nom::{ branch::alt, bytes::complete::{tag, take, take_while1}, combinator::{all_consuming, map, map_res, opt}, - Finish, - IResult, sequence::{delimited, preceded, tuple}, + sequence::{delimited, preceded}, + Finish, IResult, Parser, }; #[derive(Debug)] @@ -27,17 +27,17 @@ struct Instruction { fn parse_crate(i: &str) -> IResult<&str, char> { let first_char = |s: &str| s.chars().next().unwrap(); let f = delimited(tag("["), take(1_usize), tag("]")); - map(f, first_char)(i) + map(f, first_char).parse(i) } fn parse_hole(i: &str) -> IResult<&str, ()> { // `drop` takes a value and returns nothing, which is // perfect for our case - map(tag(" "), drop)(i) + map(tag(" "), drop).parse(i) } fn parse_crate_or_hole(i: &str) -> IResult<&str, Option> { - alt((map(parse_crate, Some), map(parse_hole, |_| None)))(i) + alt((map(parse_crate, Some), map(parse_hole, |_| None))).parse(i) } fn parse_crate_line(i: &str) -> IResult<&str, Vec>> { @@ -45,7 +45,7 @@ fn parse_crate_line(i: &str) -> IResult<&str, Vec>> { let mut v = vec![c]; loop { - let (next_i, maybe_c) = opt(preceded(tag(" "), parse_crate_or_hole))(i)?; + let (next_i, maybe_c) = opt(preceded(tag(" "), parse_crate_or_hole)).parse(i)?; match maybe_c { Some(c) => v.push(c), None => break, @@ -79,29 +79,32 @@ fn transpose_rev(v: Vec>>) -> Vec> { fn parse_number(i: &str) -> IResult<&str, usize> { map_res(take_while1(|c: char| c.is_ascii_digit()), |s: &str| { s.parse::() - })(i) + }) + .parse(i) } fn parse_pile_number(i: &str) -> IResult<&str, usize> { - map(parse_number, |i| i - 1)(i) + map(parse_number, |i| i - 1).parse(i) } fn parse_instruction(i: &str) -> IResult<&str, Instruction> { map( - tuple(( + ( preceded(tag("move "), parse_number), preceded(tag(" from "), parse_pile_number), preceded(tag(" to "), parse_pile_number), - )), + ), |(quantity, src, dst)| Instruction { quantity, src, dst }, - )(i) + ) + .parse(i) } fn parser(content: &str) -> (Vec>, Vec) { let mut lines = content.lines(); let crate_lines = (&mut lines) .map_while(|line| { - all_consuming(parse_crate_line)(line) + all_consuming(parse_crate_line) + .parse(line) .finish() .ok() .map(|(_, line)| line) @@ -119,7 +122,13 @@ fn parser(content: &str) -> (Vec>, Vec) { assert!(lines.next().unwrap().is_empty()); let instructions = lines - .map(|line| all_consuming(parse_instruction)(line).finish().unwrap().1) + .map(|line| { + all_consuming(parse_instruction) + .parse(line) + .finish() + .unwrap() + .1 + }) .collect::>(); (crate_columns, instructions) } diff --git a/src/prob7.rs b/src/prob7.rs index cbd100d..2aa10a5 100644 --- a/src/prob7.rs +++ b/src/prob7.rs @@ -5,7 +5,7 @@ use nom::{ bytes::complete::{tag, take_while1}, combinator::{all_consuming, map}, sequence::{preceded, separated_pair}, - Finish, IResult, + Finish, IResult, Parser, }; #[derive(Debug)] @@ -58,33 +58,35 @@ fn parse_entry(input: &str) -> IResult<&str, Entry> { ); let parse_dir = map(preceded(tag("dir "), parse_path), Entry::Dir); - alt((parse_file, parse_dir))(input) + alt((parse_file, parse_dir)).parse(input) } fn parse_path(input: &str) -> IResult<&str, Utf8PathBuf> { map( take_while1(|c: char| "abcdefghijklmnopqrstuvwxyz./".contains(c)), Into::into, - )(input) + ) + .parse(input) } fn parse_ls(input: &str) -> IResult<&str, Ls> { - map(tag("ls"), |_| Ls)(input) + map(tag("ls"), |_| Ls).parse(input) } fn parse_cd(input: &str) -> IResult<&str, Cd> { - map(preceded(tag("cd "), parse_path), Cd)(input) + map(preceded(tag("cd "), parse_path), Cd).parse(input) } fn parse_command(line: &str) -> IResult<&str, Command> { let (input, _) = tag("$ ")(line)?; - alt((map(parse_ls, Into::into), map(parse_cd, Into::into)))(input) + alt((map(parse_ls, Into::into), map(parse_cd, Into::into))).parse(input) } fn parse_line(input: &str) -> IResult<&str, Line> { alt(( map(parse_command, Line::Command), map(parse_entry, Line::Entry), - ))(input) + )) + .parse(input) } fn total_size(tree: &Tree, node: &Node) -> color_eyre::Result { @@ -97,7 +99,7 @@ fn total_size(tree: &Tree, node: &Node) -> color_eyre::Result< fn generate_tree(content: &str) -> color_eyre::Result> { let lines = content.lines().map(|line| { - all_consuming(parse_line)(line) + all_consuming(parse_line).parse(line) .finish() .unwrap_or_else(|err| panic!("{err}")) .1 @@ -171,6 +173,7 @@ pub fn solve_part2(content: &str) -> color_eyre::Result { .filter(|n| !n.children().is_empty()) .map(|n| total_size(&tree, n).unwrap()) .filter(|&s| s >= minimum_space_to_free) - .min().unwrap(); + .min() + .unwrap(); Ok(size_to_remove) }