this repo has no description

day2: first impl

+67 -3
+1
inputs/2.test
··· 1 + 11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124
+1
inputs/2.txt
··· 1 + 18623-26004,226779-293422,65855-88510,868-1423,248115026-248337139,903911-926580,97-121,67636417-67796062,24-47,6968-10197,193-242,3769-5052,5140337-5233474,2894097247-2894150301,979582-1016336,502-646,9132195-9191022,266-378,58-91,736828-868857,622792-694076,6767592127-6767717303,2920-3656,8811329-8931031,107384-147042,941220-969217,3-17,360063-562672,7979763615-7979843972,1890-2660,23170346-23308802
+5 -3
src/main.rs
··· 1 1 mod solutions; 2 2 3 - use solutions::Day1; 3 + use solutions::Day2; 4 4 5 5 fn main() { 6 - let day1 = Day1::new("inputs/1.txt"); 7 - println!("Part 1: {}, Part 2: {}", day1.part1(), day1.part2()); 6 + //let day1 = Day1::new("inputs/1.txt"); 7 + let day = Day2::new("inputs/2.txt"); 8 + println!("Part 1: {}", day.part1()); 9 + println!("Part 2: {}", day.part2()); 8 10 }
+58
src/solutions/day2.rs
··· 1 + use std::fs; 2 + 3 + pub struct Day2 { 4 + data: String, 5 + } 6 + 7 + fn divisors_desc(n: usize) -> impl Iterator<Item = usize> { 8 + (1..n).rev().filter(move |&i| n % i == 0) 9 + } 10 + 11 + fn invalid_id(id: &i64) -> bool { 12 + let str_id = id.to_string(); 13 + if str_id.len() % 2 != 0 { 14 + return false; 15 + } 16 + let (left, right) = str_id.split_at(str_id.len() / 2); 17 + left == right 18 + } 19 + 20 + fn new_invalid_id(id: &i64) -> bool { 21 + let str_id = id.to_string(); 22 + for div in divisors_desc(str_id.len()) { 23 + let chars = str_id.chars().collect::<Vec<char>>(); 24 + let seqs = chars.chunks(div).collect::<Vec<_>>(); 25 + if seqs.iter().all(|&seq| seq == seqs[0]) { 26 + return true; 27 + } 28 + } 29 + false 30 + } 31 + 32 + impl Day2 { 33 + pub fn new(path: &str) -> Self { 34 + Self { 35 + data: fs::read_to_string(path).expect("failed to read input file"), 36 + } 37 + } 38 + 39 + pub fn part1(&self) -> i64 { 40 + self.data.split(',').fold(0, |acc, range| { 41 + let (n1, n2) = range.split_once('-').unwrap(); 42 + println!("Range: {}-{}", n1, n2); 43 + let (n1, n2) = (n1.parse::<i64>().unwrap(), n2.parse::<i64>().unwrap()); 44 + println!("Range: {}-{}", n1, n2); 45 + (n1..=n2).filter(|x| invalid_id(x)).sum::<i64>() + acc 46 + }) 47 + } 48 + 49 + pub fn part2(&self) -> i64 { 50 + self.data.split(',').fold(0, |acc, range| { 51 + let (n1, n2) = range.split_once('-').unwrap(); 52 + println!("Range: {}-{}", n1, n2); 53 + let (n1, n2) = (n1.parse::<i64>().unwrap(), n2.parse::<i64>().unwrap()); 54 + println!("Range: {}-{}", n1, n2); 55 + (n1..=n2).filter(|x| new_invalid_id(x)).sum::<i64>() + acc 56 + }) 57 + } 58 + }
+2
src/solutions/mod.rs
··· 1 1 pub mod day1; 2 + pub mod day2; 2 3 pub use day1::Day1; 4 + pub use day2::Day2;