This commit is contained in:
2025-12-12 03:15:41 +01:00
parent 76ffeb8ef0
commit 9108278f64
2 changed files with 41 additions and 84 deletions

View File

@@ -1,9 +1,8 @@
use std::collections::HashSet;
#[aoc(day2, part1)]
pub fn part1(input: &str) -> u64 {
let ranges = parse(input);
for line in ranges.iter() {
println!("{:?}", line);
}
let invalid_ids = invalid_ids(ranges);
invalid_ids.iter().sum()
}
@@ -11,11 +10,8 @@ pub fn part1(input: &str) -> u64 {
#[aoc(day2, part2)]
pub fn part2(input: &str) -> u64 {
let ranges = parse(input);
for line in ranges.iter() {
println!("{:?}", line);
}
let invalid_ids = invalid_ids_2(ranges);
invalid_ids.iter().sum()
invalid_ids
}
fn parse(input: &str) -> Vec<(&str, &str)> {
@@ -25,41 +21,50 @@ fn parse(input: &str) -> Vec<(&str, &str)> {
.collect()
}
fn invalid_ids_2(ranges: Vec<(&str, &str)>) -> Vec<u64> {
let mut result = Vec::new();
fn invalid_ids_2(ranges: Vec<(&str, &str)>) -> u64 {
let mut result: HashSet<u64> = HashSet::new();
for (start, end) in ranges {
for i in start.parse::<u64>().unwrap()..=end.parse::<u64>().unwrap() {
let i_str = i.to_string();
println!("{}", i_str);
let i_len = i_str.len();
for rl in 1..=i_len {
if i_len % rl != 0 {
continue;
}
let mut condition = true;
for a in (0..i_len - rl).step_by(rl) {
println!(
"{:?} {:?} {:?} {} {}",
i_str,
&i_str[a..(a + rl)],
&i_str[(a + rl)..(a + 2 * rl)],
a,
rl
);
if i_str[a..(a + rl)] != i_str[(a + rl)..(a + 2 * rl)] {
condition = false;
break;
}
}
if condition {
result.push(i);
for (from, to) in ranges {
let i_from = from.parse::<u64>().unwrap();
let i_to = to.parse::<u64>().unwrap();
for len in from.len()..=to.len() {
for n in (2..=len).filter(|n| len % n == 0) {
let l = len / n;
let min = match from.len() {
x if x < len => 10u64.pow((len / n) as u32 - 1),
_ => from[..l].parse::<u64>().unwrap(),
};
let max = match to.len() {
x if x > len => 10u64.pow((len / n) as u32 + 1) - 1,
_ => to[..l].parse::<u64>().unwrap(),
};
for num in min..=max {
let candidate = num.to_string().repeat(n).parse::<u64>().unwrap();
if candidate <= i_to && candidate >= i_from {
result.insert(candidate);
}
}
}
}
result
// if from.len() == to.len() {
// println!("{} to {}", from, to,);
// for n in (2..=(from.len() / 2).max(2)).filter(|n| from.len() % n == 0) {
// //println!("{}", n);
// let l = from.len() / n;
// for num in from[..l].parse::<u64>().unwrap()..=to[..l].parse::<u64>().unwrap() {
// let candidate = num.to_string().repeat(n).parse::<u64>().unwrap();
// println!("{}", candidate);
// if candidate <= i_to && candidate >= i_from {
// println!("selected!");
// result.insert(candidate);
// }
// }
// }
// }
}
result.iter().sum()
}
fn invalid_ids(ranges: Vec<(&str, &str)>) -> Vec<u64> {
let mut result = Vec::new();
@@ -75,54 +80,6 @@ fn invalid_ids(ranges: Vec<(&str, &str)>) -> Vec<u64> {
}
}
}
// for (start, end) in ranges {
// let start_len = start.chars().count().to_owned();
// let end_len = end.chars().count().to_owned();
// let half_len = [start_len, end_len]
// .into_iter()
// .filter(|x| x % 2 == 0)
// .max()
// .unwrap_or(0)
// / 2;
// if half_len == 0 {
// continue;
// }
// let start_num = String::from(
// start
// .chars()
// .take(1.max(start_len - half_len))
// .collect::<String>(),
// )
// .parse::<u64>()
// .unwrap();
// let end_num = String::from(end.chars().take(end_len - half_len).collect::<String>())
// .parse::<u64>()
// .unwrap();
// let inner_start = String::from(start)[1.max(end_len / 2)..]
// .parse::<u64>()
// .unwrap_or(0);
// let inner_end = String::from(end)[(end_len / 2)..].parse::<u64>().unwrap();
// println!("{}-{}", start, end);
// println!("{} {}", start_num, end_num);
// println!("{} {}", inner_start, inner_end);
// for num in start_num..=end_num {
// if (inner_start..=inner_end).contains(&num) {
// let id = num * 10_u64.pow(half_len as u32) + num;
// if id.to_string().len() % 2 == 0 || id.to_string().len() == 1 {
// println!("{}", id);
// result.push(id)
// }
// }
// }
// // println!("{:?}", result);
// }
// // println!("{:?}", result);
result
}

View File

@@ -4,7 +4,7 @@ extern crate aoc_runner;
extern crate aoc_runner_derive;
pub mod day1;
// pub mod day2;
pub mod day2;
// pub mod day10;
pub mod day11;
pub mod day3;