day1
This commit is contained in:
+47
-12
@@ -1,14 +1,14 @@
|
||||
use log::debug;
|
||||
|
||||
fn first_and_last(line: &str) -> i32 {
|
||||
fn first_and_last(line: &str) -> u32 {
|
||||
let first = line.chars().find_map(|c| c.to_digit(10)).unwrap();
|
||||
let last = line.chars().rev().find_map(|c| c.to_digit(10)).unwrap();
|
||||
|
||||
let number: String = format!("{}{}", first, last);
|
||||
|
||||
debug!("Number: {}", number);
|
||||
debug!("Line: {} Number: {}", line, number);
|
||||
|
||||
number.parse::<i32>().unwrap()
|
||||
number.parse::<u32>().unwrap()
|
||||
}
|
||||
|
||||
fn search_and_replace(line: &str) -> String {
|
||||
@@ -28,24 +28,42 @@ fn search_and_replace(line: &str) -> String {
|
||||
|
||||
for (search, replace) in number_mapping {
|
||||
let mut start = 0;
|
||||
let mut first_pos = None;
|
||||
let mut last_pos = None;
|
||||
while let Some(pos) = line[start..].find(search) {
|
||||
let abs_pos = start + pos;
|
||||
if first_pos.is_none() {
|
||||
first_pos = Some(abs_pos);
|
||||
}
|
||||
last_pos = Some(abs_pos + search.len());
|
||||
start = abs_pos + search.len();
|
||||
}
|
||||
|
||||
if let Some(last) = last_pos {
|
||||
replacements.push((last - search.len(), last, search, replace));
|
||||
debug!("Line: {:?} {:?} {:?}", line, first_pos, last_pos);
|
||||
if let (Some(first), Some(last)) = (first_pos, last_pos) {
|
||||
replacements.push((first, last, search, replace));
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by position in ascending order, excluding the last occurrences
|
||||
// Sort by position in ascending order
|
||||
replacements.sort_by(|a, b| a.0.cmp(&b.0));
|
||||
debug!("{:?}", replacements);
|
||||
let first = replacements.first();
|
||||
let last = replacements.last();
|
||||
debug!("first: {:?}, last: {:?}", first, last);
|
||||
|
||||
let mut first_and_last_r: Vec<(usize, usize, &str, &str)> = Vec::new();
|
||||
if let Some(f) = first {
|
||||
first_and_last_r.push(*f);
|
||||
}
|
||||
|
||||
if let Some(l) = last {
|
||||
first_and_last_r.push(*l);
|
||||
}
|
||||
|
||||
let mut modified_line = line.to_string();
|
||||
|
||||
for (_, _, search, replace) in replacements {
|
||||
for (_, _, search, replace) in first_and_last_r {
|
||||
modified_line = modified_line.replace(search, replace);
|
||||
}
|
||||
|
||||
@@ -53,15 +71,17 @@ fn search_and_replace(line: &str) -> String {
|
||||
}
|
||||
|
||||
pub fn day1(input: String) {
|
||||
let result1: i32 = input
|
||||
let result1: u32 = input
|
||||
.lines()
|
||||
.map(|line| -> i32 { first_and_last(line) })
|
||||
.map(|line| -> u32 { first_and_last(line) })
|
||||
.sum();
|
||||
|
||||
let result2: i32 = input
|
||||
let result2: u32 = input
|
||||
.lines()
|
||||
.map(|line: &str| -> i32 {
|
||||
.map(|line: &str| -> u32 {
|
||||
debug!("INPUT: {}", line);
|
||||
let modified_line = search_and_replace(line);
|
||||
debug!("MODIFIED: {}", &modified_line);
|
||||
first_and_last(&modified_line)
|
||||
})
|
||||
.sum();
|
||||
@@ -71,8 +91,18 @@ pub fn day1(input: String) {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test_day1 {
|
||||
use log::LevelFilter;
|
||||
|
||||
use crate::day1::{first_and_last, search_and_replace};
|
||||
|
||||
#[ctor::ctor]
|
||||
fn init_logger() {
|
||||
// Initialize the logger with debug level
|
||||
env_logger::Builder::from_env("RUST_LOG")
|
||||
.filter_level(LevelFilter::Debug)
|
||||
.init();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
let input = "two1nine
|
||||
@@ -81,7 +111,8 @@ abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen";
|
||||
7pqrstsixteen
|
||||
fivesadfh33fttwoneg";
|
||||
|
||||
let mut lines: std::str::Lines<'_> = input.lines();
|
||||
assert_eq!(
|
||||
@@ -112,5 +143,9 @@ zoneight234
|
||||
76,
|
||||
first_and_last(&search_and_replace(lines.next().unwrap()))
|
||||
);
|
||||
assert_eq!(
|
||||
51,
|
||||
first_and_last(&search_and_replace(lines.next().unwrap()))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user