style(day01): add some comments, minor cleanup

This commit is contained in:
swaits committed 2023-12-01 16:36:04 -07:00
1 parent 23df9e8263
commit 18a409bf05
1 file changed
+25 -17
+25 -17
View File
@@ -1,54 +1,60 @@
use lazy_static::lazy_static; use lazy_static::lazy_static;
use regex::Regex; use regex::Regex;
use crate::utils::digit_value; use crate::{utils::digit_value, Day};
use crate::Day;
// Register this day in our runner inventory
inventory::submit! { inventory::submit! {
Day { number: 1, run } Day { number: 1, run }
} }
fn first_and_last_digit(s: &str) -> usize { fn first_and_last_digit(s: &str) -> usize {
// Find the first character in the string that is an ASCII digit
let first = s let first = s
.chars() .chars()
.find(|c| c.is_ascii_digit()) .find(|c| c.is_ascii_digit()) // Use find to get an Option containing the first digit character
.unwrap() .unwrap() // Unwrap the Option, panicking if no digit is found
.to_digit(10) .to_digit(10) // Convert the character to a digit in base 10
.unwrap() as usize; .unwrap() as usize; // Convert the digit to a usize
// Find the last character in the string that is an ASCII digit
let last = s let last = s
.chars() .chars()
.rfind(|c| c.is_ascii_digit()) .rfind(|c| c.is_ascii_digit()) // Use rfind to get an Option containing the last digit character
.unwrap() .unwrap() // Unwrap the Option, panicking if no digit is found
.to_digit(10) .to_digit(10) // Convert the character to a digit in base 10
.unwrap() as usize; .unwrap() as usize; // Convert the digit to a usize
// Return the result
first * 10 + last first * 10 + last
} }
fn day01_part1(input: &str) -> usize {
input.lines().map(first_and_last_digit).sum()
}
lazy_static! { lazy_static! {
static ref REGEX: Regex = static ref REGEX: Regex =
Regex::new(r"(?P<digit>zero|one|two|three|four|five|six|seven|eight|nine|\d)").unwrap(); Regex::new(r"(?P<digit>zero|one|two|three|four|five|six|seven|eight|nine|\d)").unwrap();
} }
fn first_and_last_digit_regex(s: &str) -> usize { fn first_and_last_digit_regex(s: &str) -> usize {
// Initialize variables to keep track of the positions and values of the first and last digits.
let mut first_start = usize::MAX; let mut first_start = usize::MAX;
let mut last_end = 0; let mut last_end = 0;
let mut first_digit = String::new(); let mut first_digit = String::new();
let mut last_digit = String::new(); let mut last_digit = String::new();
// Iterate over each character index in the string.
for (index, _) in s.char_indices() { for (index, _) in s.char_indices() {
// Check if there is a digit at the current index or beyond.
if let Some(mat) = REGEX.find(&s[index..]) { if let Some(mat) = REGEX.find(&s[index..]) {
// Calculate the start and end positions of the matched digit.
let match_start = index + mat.start(); let match_start = index + mat.start();
let match_end = index + mat.end(); let match_end = index + mat.end();
// If the current match starts before any previously found digit, it's the new first digit.
if match_start < first_start { if match_start < first_start {
first_start = match_start; first_start = match_start;
first_digit = mat.as_str().to_string(); first_digit = mat.as_str().to_string();
} }
// If the current match ends after any previously found digit, it's the new last digit.
if match_end > last_end { if match_end > last_end {
last_end = match_end; last_end = match_end;
last_digit = mat.as_str().to_string(); last_digit = mat.as_str().to_string();
@@ -56,10 +62,12 @@ fn first_and_last_digit_regex(s: &str) -> usize {
} }
} }
let first_digit_val = digit_value(&first_digit); // Convert the first and last digit strings to their numeric values and return the result
let last_digit_val = digit_value(&last_digit); digit_value(&first_digit) * 10 + digit_value(&last_digit)
}
first_digit_val * 10 + last_digit_val fn day01_part1(input: &str) -> usize {
input.lines().map(first_and_last_digit).sum()
} }
fn day01_part2(input: &str) -> usize { fn day01_part2(input: &str) -> usize {