diff --git a/day1/day1/Cargo.toml b/day1/day1/Cargo.toml index 4b25ec1..781048b 100644 --- a/day1/day1/Cargo.toml +++ b/day1/day1/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -big = [] +big = ["u128"] +u128 = [] [dependencies] diff --git a/day1/day1/src/main.rs b/day1/day1/src/main.rs index 5b54227..fcf6d7f 100644 --- a/day1/day1/src/main.rs +++ b/day1/day1/src/main.rs @@ -11,27 +11,40 @@ use input::INPUT; #[cfg(not(feature="big"))] const TARGET: u64 = 2020; #[cfg(feature="big")] const TARGET: u64 = 99920044; -// O(n^2) +// O(n^2) ? fn main() { - let deficits: HashMap = (0..) - .zip(INPUT.iter().copied()) - .filter_map(|(i, x)| TARGET.checked_sub(x).map(|x| (x, i))) + // doesn't work on big input? + #[cfg(feature="u128")] + let input: Box<[_]> = INPUT[..].iter().map(|&x| x as u128).collect(); + #[cfg(feature="u128")] + let target = TARGET as u128; + // ^ doesn't help.. + + #[cfg(not(feature="u128"))] + let input = &INPUT[..]; + #[cfg(not(feature="u128"))] + let target = TARGET; + + let deficits: HashMap<_, usize> = (0..) + .zip(input.iter().copied()) + .filter_map(|(i, x)| target.checked_sub(x).map(|x| (x, i))) .collect(); - //eprintln!("calculated deficits: {}", deficits.len()); - for (i, ix) in (0..).zip(INPUT.iter().copied()) + eprintln!("calculated deficits: {}", deficits.len()); + for (i, ix) in (0..).zip(input.iter().copied()) { - for jx in INPUT[i..].iter().copied().skip(1) + for jx in input[i..].iter().copied().skip(1) { - let sum = ix+jx; - if let Some(&idx) = deficits.get(&sum) - { - //eprintln!("solution found?"); - println!("{}", if let Some(x) = INPUT[idx].checked_mul(ix).map(|x| x.checked_mul(jx)).flatten() - { x } else { continue; }); - return; + if let Some(sum) = ix.checked_add(jx) { + if let Some(&idx) = deficits.get(&sum) + { + eprintln!("solution found?"); + println!("{}", if let Some(x) = input[idx].checked_mul(ix).map(|x| x.checked_mul(jx)).flatten() + { x } else { continue; }); + return; + } } } } - //panic!("no solution"); + panic!("no solution"); }