2024 day 3 clippy of original solution

This commit is contained in:
Dylan Thies
2024-12-03 13:33:40 -05:00
parent 2e7cda7aa3
commit 17e5f32800
2 changed files with 62 additions and 35 deletions

View File

@@ -1,8 +1,8 @@
#![warn(clippy::all, clippy::pedantic)] #![warn(clippy::all, clippy::pedantic)]
use error_stack::Result; use error_stack::{Report, Result, ResultExt};
use thiserror::Error;
use regex::Regex; use regex::Regex;
use thiserror::Error;
// day-3 // day-3
#[derive(Debug, Error)] #[derive(Debug, Error)]
@@ -11,9 +11,20 @@ pub enum Day3Part1Error{
ParseError, ParseError,
} }
/// Day-2 Part 1 for 2024 advent of code
/// Problem can be found here: <https://adventofcode.com/2024/day/2>
///
/// # Errors
/// - `ParseError` there was an issue with the parser
pub fn part1(input: &str) -> Result<String, Day3Part1Error> { pub fn part1(input: &str) -> Result<String, Day3Part1Error> {
let re = Regex::new(r"mul\((\d{1,3}),(\d{1,3})\)").unwrap(); let re = Regex::new(r"mul\((\d{1,3}),(\d{1,3})\)")
Ok(re.captures_iter(input).map(|x| &x[1].parse::<i64>().unwrap() * &x[2].parse::<i64>().unwrap()).sum::<i64>().to_string()) .map_err( Report::from)
.change_context(Day3Part1Error::ParseError)?;
Ok(re
.captures_iter(input)
.map(|x| x[1].parse::<i64>().unwrap_or(0) * x[2].parse::<i64>().unwrap_or(0))
.sum::<i64>()
.to_string())
} }
#[cfg(test)] #[cfg(test)]

View File

@@ -1,6 +1,6 @@
#![warn(clippy::all, clippy::pedantic)] #![warn(clippy::all, clippy::pedantic)]
use error_stack::Result; use error_stack::{Report, Result, ResultExt};
use regex::Regex; use regex::Regex;
use thiserror::Error; use thiserror::Error;
@@ -11,11 +11,26 @@ pub enum Day3Part2Error{
ParseError, ParseError,
} }
/// Day-3 Part 2 for 2024 advent of code
/// Problem can be found here: <https://adventofcode.com/2024/day/3#part2>
///
/// # Errors
/// - `ParseError` there was an issue with the parser
pub fn part2(input: &str) -> Result<String, Day3Part2Error> { pub fn part2(input: &str) -> Result<String, Day3Part2Error> {
let do_re = Regex::new(r"do\(\)").unwrap(); let do_re = Regex::new(r"do\(\)")
let dos = do_re.find_iter(input).map(|x| (x.start(),x.end())).collect::<Vec<_>>(); .map_err( Report::from)
let dont_re = Regex::new(r"don't\(\)").unwrap(); .change_context(Day3Part2Error::ParseError)?;
let donts = dont_re.find_iter(input).map(|x| (x.start(),x.end())).collect::<Vec<_>>(); let dos = do_re
.find_iter(input)
.map(|x| (x.start(), x.end()))
.collect::<Vec<_>>();
let dont_re = Regex::new(r"don't\(\)")
.map_err( Report::from)
.change_context(Day3Part2Error::ParseError)?;
let donts = dont_re
.find_iter(input)
.map(|x| (x.start(), x.end()))
.collect::<Vec<_>>();
let mut dos_index = 0; let mut dos_index = 0;
let mut donts_index = 0; let mut donts_index = 0;
@@ -31,8 +46,7 @@ pub fn part2 (input: &str) -> Result<String, Day3Part2Error> {
blacklist_start = donts[donts_index].0; blacklist_start = donts[donts_index].0;
white_list = false; white_list = false;
} }
} else { } else if donts[donts_index].1 < dos[dos_index].0 {
if donts[donts_index].1 < dos[dos_index].0 {
//in a black list so donts are no-ops //in a black list so donts are no-ops
donts_index += 1; donts_index += 1;
} else { } else {
@@ -41,23 +55,25 @@ pub fn part2 (input: &str) -> Result<String, Day3Part2Error> {
white_list = true; white_list = true;
} }
} }
}
if donts_index < donts.len() { if donts_index < donts.len() {
blackout_ranges.push(donts[donts_index].0..input.len()); blackout_ranges.push(donts[donts_index].0..input.len());
} else if dos_index < dos.len() && blacklist_start != 0 {
} else if dos_index < dos.len() {
if blacklist_start != 0 {
blackout_ranges.push(blacklist_start..dos[dos_index].1); blackout_ranges.push(blacklist_start..dos[dos_index].1);
} }
} let re = Regex::new(r"mul\((\d{1,3}),(\d{1,3})\)")
let re = Regex::new(r"mul\((\d{1,3}),(\d{1,3})\)").unwrap(); .map_err( Report::from)
.change_context(Day3Part2Error::ParseError)?;
let mut sum = 0; let mut sum = 0;
for mult_match in re.find_iter(input) { for mult_match in re.find_iter(input) {
if blackout_ranges.iter().any(|x| x.contains(&mult_match.start())) { if blackout_ranges
.iter()
.any(|x| x.contains(&mult_match.start()))
{
continue; continue;
} }
let values = re.captures(mult_match.as_str()).unwrap(); let values = re.captures(mult_match.as_str())
sum += &values[1].parse::<i64>().unwrap() * &values[2].parse::<i64>().unwrap(); .ok_or(Report::new( Day3Part2Error::ParseError))?;
sum += values[1].parse::<i64>().unwrap_or(0) * values[2].parse::<i64>().unwrap_or(0);
} }
Ok(sum.to_string()) Ok(sum.to_string())
} }