day1
parent
623acd6e11
commit
2c472d3b75
|
@ -15,6 +15,7 @@ dependencies = [
|
||||||
name = "aoc_2023"
|
name = "aoc_2023"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"ctor",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
"log",
|
"log",
|
||||||
]
|
]
|
||||||
|
@ -25,6 +26,16 @@ version = "2.4.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"
|
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]]
|
[[package]]
|
||||||
name = "env_logger"
|
name = "env_logger"
|
||||||
version = "0.10.1"
|
version = "0.10.1"
|
||||||
|
@ -95,6 +106,24 @@ version = "2.6.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
|
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]]
|
[[package]]
|
||||||
name = "regex"
|
name = "regex"
|
||||||
version = "1.10.2"
|
version = "1.10.2"
|
||||||
|
@ -137,6 +166,17 @@ dependencies = [
|
||||||
"windows-sys 0.52.0",
|
"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]]
|
[[package]]
|
||||||
name = "termcolor"
|
name = "termcolor"
|
||||||
version = "1.4.0"
|
version = "1.4.0"
|
||||||
|
@ -146,6 +186,12 @@ dependencies = [
|
||||||
"winapi-util",
|
"winapi-util",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-ident"
|
||||||
|
version = "1.0.12"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi"
|
name = "winapi"
|
||||||
version = "0.3.9"
|
version = "0.3.9"
|
||||||
|
|
|
@ -6,5 +6,6 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
ctor = "0.2.5"
|
||||||
env_logger = "0.10.1"
|
env_logger = "0.10.1"
|
||||||
log = "0.4.20"
|
log = "0.4.20"
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
use log::debug;
|
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 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 last = line.chars().rev().find_map(|c| c.to_digit(10)).unwrap();
|
||||||
|
|
||||||
let number: String = format!("{}{}", first, last);
|
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 {
|
fn search_and_replace(line: &str) -> String {
|
||||||
|
@ -28,24 +28,42 @@ fn search_and_replace(line: &str) -> String {
|
||||||
|
|
||||||
for (search, replace) in number_mapping {
|
for (search, replace) in number_mapping {
|
||||||
let mut start = 0;
|
let mut start = 0;
|
||||||
|
let mut first_pos = None;
|
||||||
let mut last_pos = None;
|
let mut last_pos = None;
|
||||||
while let Some(pos) = line[start..].find(search) {
|
while let Some(pos) = line[start..].find(search) {
|
||||||
let abs_pos = start + pos;
|
let abs_pos = start + pos;
|
||||||
|
if first_pos.is_none() {
|
||||||
|
first_pos = Some(abs_pos);
|
||||||
|
}
|
||||||
last_pos = Some(abs_pos + search.len());
|
last_pos = Some(abs_pos + search.len());
|
||||||
start = abs_pos + search.len();
|
start = abs_pos + search.len();
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(last) = last_pos {
|
debug!("Line: {:?} {:?} {:?}", line, first_pos, last_pos);
|
||||||
replacements.push((last - search.len(), last, search, replace));
|
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));
|
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();
|
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);
|
modified_line = modified_line.replace(search, replace);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,15 +71,17 @@ fn search_and_replace(line: &str) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn day1(input: String) {
|
pub fn day1(input: String) {
|
||||||
let result1: i32 = input
|
let result1: u32 = input
|
||||||
.lines()
|
.lines()
|
||||||
.map(|line| -> i32 { first_and_last(line) })
|
.map(|line| -> u32 { first_and_last(line) })
|
||||||
.sum();
|
.sum();
|
||||||
|
|
||||||
let result2: i32 = input
|
let result2: u32 = input
|
||||||
.lines()
|
.lines()
|
||||||
.map(|line: &str| -> i32 {
|
.map(|line: &str| -> u32 {
|
||||||
|
debug!("INPUT: {}", line);
|
||||||
let modified_line = search_and_replace(line);
|
let modified_line = search_and_replace(line);
|
||||||
|
debug!("MODIFIED: {}", &modified_line);
|
||||||
first_and_last(&modified_line)
|
first_and_last(&modified_line)
|
||||||
})
|
})
|
||||||
.sum();
|
.sum();
|
||||||
|
@ -71,8 +91,18 @@ pub fn day1(input: String) {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test_day1 {
|
mod test_day1 {
|
||||||
|
use log::LevelFilter;
|
||||||
|
|
||||||
use crate::day1::{first_and_last, search_and_replace};
|
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]
|
#[test]
|
||||||
fn test() {
|
fn test() {
|
||||||
let input = "two1nine
|
let input = "two1nine
|
||||||
|
@ -81,7 +111,8 @@ abcone2threexyz
|
||||||
xtwone3four
|
xtwone3four
|
||||||
4nineeightseven2
|
4nineeightseven2
|
||||||
zoneight234
|
zoneight234
|
||||||
7pqrstsixteen";
|
7pqrstsixteen
|
||||||
|
fivesadfh33fttwoneg";
|
||||||
|
|
||||||
let mut lines: std::str::Lines<'_> = input.lines();
|
let mut lines: std::str::Lines<'_> = input.lines();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -112,5 +143,9 @@ zoneight234
|
||||||
76,
|
76,
|
||||||
first_and_last(&search_and_replace(lines.next().unwrap()))
|
first_and_last(&search_and_replace(lines.next().unwrap()))
|
||||||
);
|
);
|
||||||
|
assert_eq!(
|
||||||
|
51,
|
||||||
|
first_and_last(&search_and_replace(lines.next().unwrap()))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue