y2025d2
This commit is contained in:
1
y2025/resources/2_input.txt
Normal file
1
y2025/resources/2_input.txt
Normal file
@@ -0,0 +1 @@
|
||||
8284583-8497825,7171599589-7171806875,726-1031,109709-251143,1039-2064,650391-673817,674522-857785,53851-79525,8874170-8908147,4197684-4326484,22095-51217,92761-107689,23127451-23279882,4145708930-4145757240,375283-509798,585093-612147,7921-11457,899998-1044449,3-19,35-64,244-657,5514-7852,9292905274-9292965269,287261640-287314275,70-129,86249864-86269107,5441357-5687039,2493-5147,93835572-94041507,277109-336732,74668271-74836119,616692-643777,521461-548256,3131219357-3131417388
|
||||
27
y2025/src/bin/d2.rs
Normal file
27
y2025/src/bin/d2.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use std::{fs, time::Instant};
|
||||
|
||||
use utils::time::get_elapsed_string;
|
||||
use y2025::days::d2;
|
||||
|
||||
fn main() {
|
||||
let now = Instant::now();
|
||||
println!("Part 1:");
|
||||
part1();
|
||||
println!("Ran in {}", get_elapsed_string(now.elapsed()));
|
||||
let now = Instant::now();
|
||||
println!("Part 2:");
|
||||
part2();
|
||||
println!("Ran in {}", get_elapsed_string(now.elapsed()));
|
||||
}
|
||||
|
||||
fn part1() {
|
||||
let root = env!("CARGO_MANIFEST_DIR");
|
||||
let content = fs::read_to_string(format!("{root}/resources/2_input.txt")).unwrap();
|
||||
println!("{}", d2::process_part1(&content));
|
||||
}
|
||||
|
||||
fn part2() {
|
||||
let root = env!("CARGO_MANIFEST_DIR");
|
||||
let content = fs::read_to_string(format!("{root}/resources/2_input.txt")).unwrap();
|
||||
println!("{}", d2::process_part2(&content));
|
||||
}
|
||||
91
y2025/src/days/d2.rs
Normal file
91
y2025/src/days/d2.rs
Normal file
@@ -0,0 +1,91 @@
|
||||
use itertools::Itertools;
|
||||
|
||||
pub fn process_part1(input: &str) -> i64 {
|
||||
input
|
||||
.split(',')
|
||||
.map(|range| range.split_once('-').unwrap())
|
||||
.map(|(start, end)| {
|
||||
(
|
||||
start
|
||||
.parse::<i64>()
|
||||
.expect(&format!("Cannot parse start from {start}")),
|
||||
end.trim_end()
|
||||
.parse::<i64>()
|
||||
.expect(&format!("Cannot parse end from {end}")),
|
||||
)
|
||||
})
|
||||
.map(|(start, end)| {
|
||||
(start..=end)
|
||||
.map(|id| {
|
||||
let id_str = format!("{id}");
|
||||
if id_str.starts_with('0') {
|
||||
return 0;
|
||||
}
|
||||
let (left, right) = id_str.split_at(id_str.len() / 2);
|
||||
if left == right {
|
||||
return id;
|
||||
}
|
||||
0
|
||||
})
|
||||
.sum::<i64>()
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
pub fn process_part2(input: &str) -> i64 {
|
||||
input
|
||||
.split(',')
|
||||
.map(|range| range.split_once('-').unwrap())
|
||||
.map(|(start, end)| {
|
||||
(
|
||||
start
|
||||
.parse::<i64>()
|
||||
.expect(&format!("Cannot parse start from {start}")),
|
||||
end.trim_end()
|
||||
.parse::<i64>()
|
||||
.expect(&format!("Cannot parse end from {end}")),
|
||||
)
|
||||
})
|
||||
.map(|(start, end)| {
|
||||
(start..=end)
|
||||
.map(|id| {
|
||||
let id_str = format!("{id}");
|
||||
if id_str.starts_with('0') {
|
||||
return 0;
|
||||
}
|
||||
for num_parts in 2..=id_str.len() {
|
||||
let parts = id_str
|
||||
.chars()
|
||||
.chunks(id_str.len() / num_parts)
|
||||
.into_iter()
|
||||
.map(|chunk| chunk.collect::<String>())
|
||||
.collect::<Vec<String>>();
|
||||
if parts.iter().all_equal() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
0
|
||||
})
|
||||
.sum::<i64>()
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const INPUT: &str = "11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124";
|
||||
|
||||
#[test]
|
||||
fn part1() {
|
||||
let result = process_part1(INPUT);
|
||||
assert_eq!(result, 1227775554);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2() {
|
||||
let result = process_part2(INPUT);
|
||||
assert_eq!(result, 4174379265);
|
||||
}
|
||||
}
|
||||
@@ -1 +1,3 @@
|
||||
pub mod d1;
|
||||
|
||||
pub mod d2;
|
||||
|
||||
Reference in New Issue
Block a user