chore: fmt + clippy + time on day 3

This commit is contained in:
Xavier Morel
2024-12-03 10:09:57 +01:00
parent 2b36a49a01
commit baaae62cb9
2 changed files with 20 additions and 19 deletions

View File

@@ -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 1](./src/bin/01.rs) | `72.0µs` | `76.8µs` |
| [Day 2](./src/bin/02.rs) | `215.6µs` | `371.6µ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**
<!--- benchmarking table ---> <!--- benchmarking table --->
--- ---

View File

@@ -5,8 +5,9 @@ use regex::Regex;
pub fn part_one(input: &str) -> Option<u32> { pub fn part_one(input: &str) -> Option<u32> {
let re = Regex::new(r"mul\((\d+),(\d+)\)").unwrap(); let re = Regex::new(r"mul\((\d+),(\d+)\)").unwrap();
re.captures_iter(input) re.captures_iter(input)
.map(|c| match c.extract() { .map(|c| {
(_, [a, b]) => a.parse::<u32>().unwrap() * b.parse::<u32>().unwrap() let (_, [a, b]) = c.extract();
a.parse::<u32>().unwrap() * b.parse::<u32>().unwrap()
}) })
.sum::<u32>() .sum::<u32>()
.into() .into()
@@ -15,21 +16,21 @@ pub fn part_one(input: &str) -> Option<u32> {
pub fn part_two(input: &str) -> Option<u32> { pub fn part_two(input: &str) -> Option<u32> {
let re = Regex::new(r"(?:(mul)\((\d+),(\d+)\))|(?:(do)(\()(\)))|(?:(don't)(\()(\)))").unwrap(); let re = Regex::new(r"(?:(mul)\((\d+),(\d+)\))|(?:(do)(\()(\)))|(?:(don't)(\()(\)))").unwrap();
re.captures_iter(input) re.captures_iter(input)
.map(|c| match c.extract() { .map(|c| match c.extract() {
(_, ["mul", a, b]) => (None, a.parse::<u32>().unwrap() * b.parse::<u32>().unwrap()), (_, ["mul", a, b]) => (None, a.parse::<u32>().unwrap() * b.parse::<u32>().unwrap()),
(_, ["do", _, _]) => (Some(true), 0), (_, ["do", _, _]) => (Some(true), 0),
(_, ["don't", _, _]) => (Some(false), 0), (_, ["don't", _, _]) => (Some(false), 0),
_ => panic!("invalid parsing"), _ => panic!("invalid parsing"),
}) })
.fold((true, 0), |(flag, acc), (op_flag, op_res)| .fold((true, 0), |(flag, acc), (op_flag, op_res)| {
match (flag, acc, op_flag, op_res) { match (flag, acc, op_flag, op_res) {
(_, acc, Some(new_flag), _) => (new_flag, acc), (_, acc, Some(new_flag), _) => (new_flag, acc),
(false, acc, None, _) => (false, acc), (false, acc, None, _) => (false, acc),
(true, acc, None, new) => (true, acc + new), (true, acc, None, new) => (true, acc + new),
} }
) })
.1 .1
.into() .into()
} }
#[cfg(test)] #[cfg(test)]
@@ -48,4 +49,3 @@ mod tests {
assert_eq!(result, Some(48)); assert_eq!(result, Some(48));
} }
} }