2024 day 3 clippy of original solution
This commit is contained in:
@@ -1,19 +1,30 @@
|
|||||||
#![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)]
|
||||||
pub enum Day3Part1Error{
|
pub enum Day3Part1Error {
|
||||||
#[error("Problem parsing Day 3")]
|
#[error("Problem parsing Day 3")]
|
||||||
ParseError,
|
ParseError,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part1 (input: &str) -> Result<String, Day3Part1Error> {
|
/// Day-2 Part 1 for 2024 advent of code
|
||||||
let re = Regex::new(r"mul\((\d{1,3}),(\d{1,3})\)").unwrap();
|
/// Problem can be found here: <https://adventofcode.com/2024/day/2>
|
||||||
Ok(re.captures_iter(input).map(|x| &x[1].parse::<i64>().unwrap() * &x[2].parse::<i64>().unwrap()).sum::<i64>().to_string())
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// - `ParseError` there was an issue with the parser
|
||||||
|
pub fn part1(input: &str) -> Result<String, Day3Part1Error> {
|
||||||
|
let re = Regex::new(r"mul\((\d{1,3}),(\d{1,3})\)")
|
||||||
|
.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)]
|
||||||
|
|||||||
@@ -1,63 +1,79 @@
|
|||||||
#![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;
|
||||||
|
|
||||||
// day-3
|
// day-3
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error)]
|
||||||
pub enum Day3Part2Error{
|
pub enum Day3Part2Error {
|
||||||
#[error("Problem parsing Day 3")]
|
#[error("Problem parsing Day 3")]
|
||||||
ParseError,
|
ParseError,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part2 (input: &str) -> Result<String, Day3Part2Error> {
|
/// Day-3 Part 2 for 2024 advent of code
|
||||||
let do_re = Regex::new(r"do\(\)").unwrap();
|
/// Problem can be found here: <https://adventofcode.com/2024/day/3#part2>
|
||||||
let dos = do_re.find_iter(input).map(|x| (x.start(),x.end())).collect::<Vec<_>>();
|
///
|
||||||
let dont_re = Regex::new(r"don't\(\)").unwrap();
|
/// # Errors
|
||||||
let donts = dont_re.find_iter(input).map(|x| (x.start(),x.end())).collect::<Vec<_>>();
|
/// - `ParseError` there was an issue with the parser
|
||||||
|
pub fn part2(input: &str) -> Result<String, Day3Part2Error> {
|
||||||
|
let do_re = Regex::new(r"do\(\)")
|
||||||
|
.map_err( Report::from)
|
||||||
|
.change_context(Day3Part2Error::ParseError)?;
|
||||||
|
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;
|
||||||
let mut white_list = true;
|
let mut white_list = true;
|
||||||
let mut blackout_ranges = Vec::new();
|
let mut blackout_ranges = Vec::new();
|
||||||
let mut blacklist_start = 0;
|
let mut blacklist_start = 0;
|
||||||
while dos_index < dos.len() && donts_index < donts.len(){
|
while dos_index < dos.len() && donts_index < donts.len() {
|
||||||
if white_list {
|
if white_list {
|
||||||
if dos[dos_index].1 < donts[donts_index].0 {
|
if dos[dos_index].1 < donts[donts_index].0 {
|
||||||
//currently whitelisted so dos are no-ops
|
//currently whitelisted so dos are no-ops
|
||||||
dos_index +=1;
|
dos_index += 1;
|
||||||
} else {
|
} else {
|
||||||
blacklist_start = donts[donts_index].0;
|
blacklist_start = donts[donts_index].0;
|
||||||
white_list = false;
|
white_list = false;
|
||||||
}
|
}
|
||||||
|
} else if donts[donts_index].1 < dos[dos_index].0 {
|
||||||
|
//in a black list so donts are no-ops
|
||||||
|
donts_index += 1;
|
||||||
} else {
|
} else {
|
||||||
if donts[donts_index].1 < dos[dos_index].0 {
|
|
||||||
//in a black list so donts are no-ops
|
|
||||||
donts_index +=1;
|
|
||||||
} else {
|
|
||||||
blackout_ranges.push(blacklist_start..dos[dos_index].1);
|
|
||||||
blacklist_start = 0;
|
|
||||||
white_list = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if donts_index < donts.len() {
|
|
||||||
blackout_ranges.push(donts[donts_index].0..input.len());
|
|
||||||
|
|
||||||
} 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);
|
||||||
|
blacklist_start = 0;
|
||||||
|
white_list = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let re = Regex::new(r"mul\((\d{1,3}),(\d{1,3})\)").unwrap();
|
if donts_index < donts.len() {
|
||||||
|
blackout_ranges.push(donts[donts_index].0..input.len());
|
||||||
|
} else if dos_index < dos.len() && blacklist_start != 0 {
|
||||||
|
blackout_ranges.push(blacklist_start..dos[dos_index].1);
|
||||||
|
}
|
||||||
|
let re = Regex::new(r"mul\((\d{1,3}),(\d{1,3})\)")
|
||||||
|
.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())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user