main
Mathias Rothenhaeusler 2023-12-02 13:23:08 +01:00
parent 623acd6e11
commit 2c472d3b75
3 changed files with 94 additions and 12 deletions

46
Cargo.lock generated
View File

@ -15,6 +15,7 @@ dependencies = [
name = "aoc_2023"
version = "0.1.0"
dependencies = [
"ctor",
"env_logger",
"log",
]
@ -25,6 +26,16 @@ version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"
[[package]]
name = "ctor"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37e366bff8cd32dd8754b0991fb66b279dc48f598c3a18914852a6673deef583"
dependencies = [
"quote",
"syn",
]
[[package]]
name = "env_logger"
version = "0.10.1"
@ -95,6 +106,24 @@ version = "2.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
[[package]]
name = "proc-macro2"
version = "1.0.70"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
[[package]]
name = "regex"
version = "1.10.2"
@ -137,6 +166,17 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "syn"
version = "2.0.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "termcolor"
version = "1.4.0"
@ -146,6 +186,12 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "winapi"
version = "0.3.9"

View File

@ -6,5 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
ctor = "0.2.5"
env_logger = "0.10.1"
log = "0.4.20"

View File

@ -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()))
);
}
}