diff --git a/advent_of_code/2025/2/Cargo.toml b/advent_of_code/2025/2/Cargo.toml new file mode 100644 index 0000000..820a001 --- /dev/null +++ b/advent_of_code/2025/2/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "main" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/advent_of_code/2025/2/input.txt b/advent_of_code/2025/2/input.txt new file mode 100644 index 0000000..cc522c9 --- /dev/null +++ b/advent_of_code/2025/2/input.txt @@ -0,0 +1 @@ +385350926-385403705,48047-60838,6328350434-6328506208,638913-698668,850292-870981,656-1074,742552-796850,4457-6851,138-206,4644076-4851885,3298025-3353031,8594410816-8594543341,396-498,1558-2274,888446-916096,12101205-12154422,2323146444-2323289192,37-57,101-137,46550018-46679958,79-96,317592-341913,495310-629360,33246-46690,14711-22848,1-17,2850-4167,3723700171-3723785996,190169-242137,272559-298768,275-365,7697-11193,61-78,75373-110112,425397-451337,9796507-9899607,991845-1013464,77531934-77616074 diff --git a/advent_of_code/2025/2/src/main.rs b/advent_of_code/2025/2/src/main.rs new file mode 100644 index 0000000..baaaf57 --- /dev/null +++ b/advent_of_code/2025/2/src/main.rs @@ -0,0 +1,123 @@ +use std::cmp::{max, min}; + +fn parse_input(input: &str) -> Vec<(i64, i64)> { + let line = input.lines().next().unwrap(); + + line.split(",") + .map(|id_range| { + let mut id_range_iter = id_range.split("-"); + + ( + id_range_iter.next().unwrap().parse::().unwrap(), + id_range_iter.next().unwrap().parse::().unwrap(), + ) + }) + .collect() +} + +fn solve_1(input: &str) -> i64 { + let ranges = parse_input(input); + + let mut result = 0; + + for range in ranges { + let min_length = range.0.to_string().len(); + let max_length = range.1.to_string().len(); + + for range_length in min_length..=max_length { + if range_length % 2 == 0 { + let mut range_start = max(range.0, 10_i64.pow(range_length as u32 - 1)); + let mut range_end = min(range.1, 10_i64.pow(range_length as u32) - 1); + + let start_halves = range_start.to_string(); + let start_halves = start_halves.split_at(range_length / 2); + let (start_first_half, start_second_half) = ( + start_halves.0.parse::().unwrap(), + start_halves.1.parse::().unwrap(), + ); + + if start_first_half < start_second_half { + range_start = start_first_half + 1; + } else { + range_start = start_first_half; + } + + let end_halves = range_end.to_string(); + let end_halves = end_halves.split_at(range_length / 2); + let (end_first_half, end_second_half) = ( + end_halves.0.parse::().unwrap(), + end_halves.1.parse::().unwrap(), + ); + + if end_first_half > end_second_half { + range_end = end_first_half - 1; + } else { + range_end = end_first_half; + } + + for x in range_start..=range_end { + result += x * 10_i64.pow(range_length as u32 / 2) + x; + } + } + } + } + + result +} + +fn get_factors(n: i64) -> Vec { + // Naive implementation, not counting n itself, but counting 1 always +} + +fn solve_2(input: &str) -> i64 { + let ranges = parse_input(input); + + let mut result = 0; + + for range in ranges { + let min_length = range.0.to_string().len(); + let max_length = range.1.to_string().len(); + + for range_length in min_length..=max_length { + let range_start = max(range.0, 10_i64.pow(range_length as u32 - 1)); + let range_end = min(range.1, 10_i64.pow(range_length as u32) - 1); + + let factors = get_factors(range_length); + + for factor in factors { + + } + } + } + + result +} + +fn main() { + println!("Hello, this is Patrick!"); + + let input = include_str!("../input.txt"); + + let result_1 = solve_1(input); + println!("Sum of false ids: {}", result_1); + + let result_2 = solve_2(input); + println!("{}", result_2); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_1() { + let test_input = include_str!("../test.txt"); + assert_eq!(solve_1(test_input), 1227775554); + } + + #[test] + fn test_2() { + let test_input = include_str!("../test.txt"); + assert_eq!(solve_2(test_input), 4174379265); + } +} diff --git a/advent_of_code/2025/2/test.txt b/advent_of_code/2025/2/test.txt new file mode 100644 index 0000000..a3f22ef --- /dev/null +++ b/advent_of_code/2025/2/test.txt @@ -0,0 +1 @@ +11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124