From baaae62cb93971a399a09381670f1c975f581ba9 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Tue, 3 Dec 2024 10:09:57 +0100 Subject: [PATCH] chore: fmt + clippy + time on day 3 --- README.md | 3 ++- src/bin/03.rs | 36 ++++++++++++++++++------------------ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 7afb1d4..12b2d6a 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www. | :---: | :---: | :---: | | [Day 1](./src/bin/01.rs) | `72.0µs` | `76.8µs` | | [Day 2](./src/bin/02.rs) | `215.6µs` | `371.6µs` | +| [Day 3](./src/bin/03.rs) | `652.5µs` | `736.7µs` | -**Total: 0.74ms** +**Total: 2.13ms** --- diff --git a/src/bin/03.rs b/src/bin/03.rs index 8cb9f83..1fa08fa 100644 --- a/src/bin/03.rs +++ b/src/bin/03.rs @@ -5,8 +5,9 @@ use regex::Regex; pub fn part_one(input: &str) -> Option { let re = Regex::new(r"mul\((\d+),(\d+)\)").unwrap(); re.captures_iter(input) - .map(|c| match c.extract() { - (_, [a, b]) => a.parse::().unwrap() * b.parse::().unwrap() + .map(|c| { + let (_, [a, b]) = c.extract(); + a.parse::().unwrap() * b.parse::().unwrap() }) .sum::() .into() @@ -15,21 +16,21 @@ pub fn part_one(input: &str) -> Option { pub fn part_two(input: &str) -> Option { let re = Regex::new(r"(?:(mul)\((\d+),(\d+)\))|(?:(do)(\()(\)))|(?:(don't)(\()(\)))").unwrap(); re.captures_iter(input) - .map(|c| match c.extract() { - (_, ["mul", a, b]) => (None, a.parse::().unwrap() * b.parse::().unwrap()), - (_, ["do", _, _]) => (Some(true), 0), - (_, ["don't", _, _]) => (Some(false), 0), - _ => panic!("invalid parsing"), - }) - .fold((true, 0), |(flag, acc), (op_flag, op_res)| - match (flag, acc, op_flag, op_res) { - (_, acc, Some(new_flag), _) => (new_flag, acc), - (false, acc, None, _) => (false, acc), - (true, acc, None, new) => (true, acc + new), - } - ) - .1 - .into() + .map(|c| match c.extract() { + (_, ["mul", a, b]) => (None, a.parse::().unwrap() * b.parse::().unwrap()), + (_, ["do", _, _]) => (Some(true), 0), + (_, ["don't", _, _]) => (Some(false), 0), + _ => panic!("invalid parsing"), + }) + .fold((true, 0), |(flag, acc), (op_flag, op_res)| { + match (flag, acc, op_flag, op_res) { + (_, acc, Some(new_flag), _) => (new_flag, acc), + (false, acc, None, _) => (false, acc), + (true, acc, None, new) => (true, acc + new), + } + }) + .1 + .into() } #[cfg(test)] @@ -48,4 +49,3 @@ mod tests { assert_eq!(result, Some(48)); } } -