diff options
| author | Josh Kingsley <josh@joshkingsley.me> | 2025-12-04 03:35:40 +0200 |
|---|---|---|
| committer | Josh Kingsley <josh@joshkingsley.me> | 2025-12-04 03:35:40 +0200 |
| commit | 6d26f1cbda0eaa41fc735a2e340ec7612a5461e0 (patch) | |
| tree | c420b33a1bb60abd3226cbfa6a41fd56070301b4 /day2 | |
| parent | e057ccbb3d437d6ceea76740a62582e592ccbca8 (diff) | |
Day 2 solve
Diffstat (limited to 'day2')
| -rw-r--r-- | day2/src/main.rs | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/day2/src/main.rs b/day2/src/main.rs index 2acc32d..fc990de 100644 --- a/day2/src/main.rs +++ b/day2/src/main.rs @@ -1,4 +1,4 @@ -use std::{env, fs, num::ParseIntError, ops::Range, str::FromStr}; +use std::{env, fs, num::ParseIntError, str::FromStr}; #[derive(Debug, Clone, Copy)] struct IDRange(u64, u64); @@ -84,11 +84,12 @@ impl ID { if chars[i] == chars[j] { j += 1; + i += 1; + } else if j == 0 { + i += 1; } else { j = 0; } - - i += 1; } } } @@ -106,7 +107,6 @@ impl From<std::io::Error> for Error { } fn main() -> Result<(), Error> { - let x: Range<u32> = 0..99; let mut args = env::args(); let input_path = args.nth(1).ok_or_else(|| Error::BadArgument)?; let input = fs::read_to_string(input_path)?; @@ -128,12 +128,13 @@ fn main() -> Result<(), Error> { }) .sum(); + println!("Result (simple): {}", result_simple); + let result: u64 = ranges .iter() .flat_map(|range| range.into_iter().filter(ID::is_invalid).map(|id| id.0)) .sum(); - println!("Result (simple): {}", result_simple); println!("Result: {}", result); Ok(()) @@ -184,6 +185,13 @@ mod tests { assert!(ID(111).is_invalid()); assert!(ID(123123).is_invalid()); assert!(ID(121212).is_invalid()); + assert!(ID(12121212121212).is_invalid()); assert!(ID(1010).is_invalid()); + assert!(ID(12341234).is_invalid()); + assert!(ID(123412341234).is_invalid()); + assert!(ID(1213212132).is_invalid()); + assert!(ID(2222122221).is_invalid()); + + assert!(ID(121121).is_invalid()); } } |
