Day 2 deadvey's solution
This commit is contained in:
6
2025/2/deadvey/Cargo.toml
Normal file
6
2025/2/deadvey/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "deadvey"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
66
2025/2/deadvey/src/main.rs
Normal file
66
2025/2/deadvey/src/main.rs
Normal file
@@ -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<Vec<&str>> = file_contents
|
||||
.split(",")
|
||||
.collect::<Vec<&str>>()
|
||||
.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<String> = num_string.chars()
|
||||
.collect::<Vec<char>>()
|
||||
.chunks(chunk_size)
|
||||
.map(|chunk| chunk.iter().collect::<String>())
|
||||
.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);
|
||||
}
|
||||
Reference in New Issue
Block a user