From 2c45829d638d21f21e21a51097ddcd36c880c949 Mon Sep 17 00:00:00 2001 From: Avril Date: Tue, 1 Dec 2020 19:52:49 +0000 Subject: [PATCH] day1 reimpl 1.20x speedup --- .gitignore | 3 +++ day1/day1/Cargo.toml | 12 ++++++++++++ day1/day1/build.rs | 37 +++++++++++++++++++++++++++++++++++++ day1/day1/src/main.rs | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 day1/day1/Cargo.toml create mode 100644 day1/day1/build.rs create mode 100644 day1/day1/src/main.rs diff --git a/.gitignore b/.gitignore index 3100c04..7186bb3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ input* part1* part2* + +*.lock +target/ diff --git a/day1/day1/Cargo.toml b/day1/day1/Cargo.toml new file mode 100644 index 0000000..4b25ec1 --- /dev/null +++ b/day1/day1/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "day1" +version = "0.1.0" +authors = ["Avril "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[features] +big = [] + +[dependencies] diff --git a/day1/day1/build.rs b/day1/day1/build.rs new file mode 100644 index 0000000..57881a2 --- /dev/null +++ b/day1/day1/build.rs @@ -0,0 +1,37 @@ +use std::{ + io::{ + self, BufRead, BufReader, Write, + }, + fs, + error, +}; + +fn gen_input(output: &mut W, lines: R, const_name: &str) -> io::Result<()> { + + eprint!("Generating {}... ", const_name); + writeln!(output, "pub const {}: &[u64] = &[", const_name)?; + for line in lines.lines() + { + writeln!(output, " {},", line?)?; + } + output.write_all(b"];\n")?; + eprintln!("OK"); + Ok(()) +} + +fn main() -> Result<(), Box>{ + let mut output = fs::OpenOptions::new() + .write(true) + .truncate(true) + .create(true) + .open("./src/input.rs").expect("Couldn't open output"); + + gen_input(&mut output, BufReader::new(fs::OpenOptions::new() + .read(true) + .open("../input").expect("Couldn't open input")), "INPUT")?; + gen_input(&mut output, BufReader::new(fs::OpenOptions::new() + .read(true) + .open("../input-big").expect("Couldn't open big input")), "INPUT_BIG")?; + + Ok(()) +} diff --git a/day1/day1/src/main.rs b/day1/day1/src/main.rs new file mode 100644 index 0000000..5b54227 --- /dev/null +++ b/day1/day1/src/main.rs @@ -0,0 +1,37 @@ +use std::collections::HashMap; + +#[allow(dead_code)] +mod input; + +#[cfg(feature="big")] +use input::INPUT_BIG as INPUT; +#[cfg(not(feature="big"))] +use input::INPUT; + +#[cfg(not(feature="big"))] const TARGET: u64 = 2020; +#[cfg(feature="big")] const TARGET: u64 = 99920044; + +// 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))) + .collect(); + + //eprintln!("calculated deficits: {}", deficits.len()); + for (i, ix) in (0..).zip(INPUT.iter().copied()) + { + 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; + } + } + } + //panic!("no solution"); +}