Skip to content

Instantly share code, notes, and snippets.

@retorquere
Created December 4, 2025 09:52
Show Gist options
  • Select an option

  • Save retorquere/de2c3640e38670b84e13606666562b78 to your computer and use it in GitHub Desktop.

Select an option

Save retorquere/de2c3640e38670b84e13606666562b78 to your computer and use it in GitHub Desktop.
use std::fs::File;
use std::io::Read;
fn main() {
let total: u64 = {
let mut contents = String::new();
File::open("02.txt").expect("FATAL: Failed to open a.txt.")
.read_to_string(&mut contents).expect("FATAL: Failed to read contents of a.txt.");
contents
}
.replace('\n', "")
.split(',')
.flat_map(|rng| {
let parts: Vec<&str> = rng.split('-').collect();
if parts.len() != 2 {
panic!("FATAL: Invalid range '{}'.", rng);
}
let start = parts[0].parse::<u64>().unwrap();
let end = parts[1].parse::<u64>().unwrap();
start..=end
})
.map(|n| {
let ns = n.to_string();
let ln = ns.len();
for p in 1..=(ln / 2) {
if ln % p == 0 {
if ns == ns.get(0..p).unwrap().repeat(ln / p) {
return n;
}
}
}
0
})
.sum();
println!("{}", total);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment