Day 2 aoc in progress

This commit is contained in:
Philippe Zwietering
2026-01-09 17:00:16 +01:00
parent 4ad2ec5240
commit 121a9a02cb
4 changed files with 131 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
[package]
name = "main"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@@ -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

View File

@@ -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::<i64>().unwrap(),
id_range_iter.next().unwrap().parse::<i64>().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::<i64>().unwrap(),
start_halves.1.parse::<i64>().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::<i64>().unwrap(),
end_halves.1.parse::<i64>().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<i64> {
// 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);
}
}

View File

@@ -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