day1: rust part1 reimpl

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

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

@ -11,14 +11,47 @@ use input::INPUT;
#[cfg(not(feature="big"))] const TARGET: u64 = 2020;
#[cfg(feature="big")] const TARGET: u64 = 99920044;
// O(n^2) ?
fn main() {
// doesn't work on big input?
#[cfg(feature="part1")]
// O(2n) ?
fn part1()
{
// u64 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;
#[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();
for ix in input.iter().copied()
{
if let Some(&idx) = deficits.get(&ix)
{
if let Some(x) = input[idx].checked_mul(ix) {
println!("{}", x);
return;
}
}
}
panic!("no solution");
}
// O(n + n^2) ?
#[cfg(feature="part2")]
fn part2()
{
// u64 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[..];
@ -48,3 +81,8 @@ fn main() {
}
panic!("no solution");
}
fn main() {
#[cfg(feature="part1")] part1();
#[cfg(feature="part2")] part2();
}

Loading…
Cancel
Save