biginput overflows u64?

master
Avril 4 years ago
parent 2c45829d63
commit 792b0d8a1d
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -7,6 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features] [features]
big = [] big = ["u128"]
u128 = []
[dependencies] [dependencies]

@ -11,27 +11,40 @@ use input::INPUT;
#[cfg(not(feature="big"))] const TARGET: u64 = 2020; #[cfg(not(feature="big"))] const TARGET: u64 = 2020;
#[cfg(feature="big")] const TARGET: u64 = 99920044; #[cfg(feature="big")] const TARGET: u64 = 99920044;
// O(n^2) // O(n^2) ?
fn main() { fn main() {
let deficits: HashMap<u64, usize> = (0..) // doesn't work on big input?
.zip(INPUT.iter().copied()) #[cfg(feature="u128")]
.filter_map(|(i, x)| TARGET.checked_sub(x).map(|x| (x, i))) 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(); .collect();
//eprintln!("calculated deficits: {}", deficits.len()); eprintln!("calculated deficits: {}", deficits.len());
for (i, ix) in (0..).zip(INPUT.iter().copied()) 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(sum) = ix.checked_add(jx) {
if let Some(&idx) = deficits.get(&sum) if let Some(&idx) = deficits.get(&sum)
{ {
//eprintln!("solution found?"); eprintln!("solution found?");
println!("{}", if let Some(x) = INPUT[idx].checked_mul(ix).map(|x| x.checked_mul(jx)).flatten() println!("{}", if let Some(x) = input[idx].checked_mul(ix).map(|x| x.checked_mul(jx)).flatten()
{ x } else { continue; }); { x } else { continue; });
return; return;
} }
} }
} }
//panic!("no solution"); }
panic!("no solution");
} }

Loading…
Cancel
Save