diff --git a/2025/2/deadvey/Cargo.toml b/2025/2/deadvey/Cargo.toml new file mode 100644 index 0000000..63d909b --- /dev/null +++ b/2025/2/deadvey/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "deadvey" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/2025/2/deadvey/src/main.rs b/2025/2/deadvey/src/main.rs new file mode 100644 index 0000000..924a723 --- /dev/null +++ b/2025/2/deadvey/src/main.rs @@ -0,0 +1,66 @@ +use std::fs; +use std::time::Instant; + +// you can find the invalid IDs by looking for any ID which is made only of some sequence of digits repeated twice. +// So, 55 (5 twice), 6464 (64 twice), and 123123 (123 twice) would all be invalid IDs. +fn main() { + let mut total1: u64 = 0; + let mut total2: u64 = 0; + let mut string_temp_1: String = "".to_string(); + let mut string_temp_2: String = "".to_string(); + + let start = Instant::now(); + let mut file_contents: String = fs::read_to_string("../large.txt").unwrap(); + file_contents.pop();// Remove \n + // Split it up + let pairs: Vec> = file_contents + .split(",") + .collect::>() + .iter() + .map(|s| s.split('-').collect()) + .collect(); + let file_read = start.elapsed(); + + // There's a lot of converting between int and string + for pair in pairs + { + let num_1: u64 = pair[0].parse().unwrap(); + let num_2: u64 = pair[1].parse().unwrap(); + for num in num_1..=num_2 { // Loop over each number in the range + let num_string = num.to_string(); + for divider in 2..=num_string.len() { + if num_string.len() % divider == 0 { + let chunk_size = num_string.len() / divider; + let substring_vector: Vec = num_string.chars() + .collect::>() + .chunks(chunk_size) + .map(|chunk| chunk.iter().collect::()) + .collect(); + if substring_vector.iter().all(|x| x == &substring_vector[0]) { + total2 += num; + if divider == 2 { + total1 += num; + } + break + } + } + } + } + //clear both strings + string_temp_1.clear(); + string_temp_2.clear(); + } + let end = start.elapsed(); + println!("part1: {total1}"); + println!("part2: {total2}"); + println!("Times: + File read: {:.2?} + Loop: {:.2?} + Avg time per pair: {:.2?} + Total: {:.4?} + ", + file_read, + end-file_read, + (end-file_read)/(100).try_into().unwrap(), + end); +}