From bcd374d3f94da352062c3bd02ab15cc4b51e4d22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20K=C5=82ak?= Date: Wed, 4 Dec 2019 23:26:31 +0100 Subject: [PATCH] Day 20 --- day20/Cargo.toml | 9 +++++ day20/src/main.rs | 85 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 day20/Cargo.toml create mode 100644 day20/src/main.rs diff --git a/day20/Cargo.toml b/day20/Cargo.toml new file mode 100644 index 0000000..5e681fb --- /dev/null +++ b/day20/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day20" +version = "0.1.0" +authors = ["Tomasz Kłak "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day20/src/main.rs b/day20/src/main.rs new file mode 100644 index 0000000..9125457 --- /dev/null +++ b/day20/src/main.rs @@ -0,0 +1,85 @@ +use std::collections::{HashMap, HashSet}; + +fn sq(n: u64) -> u64 { + (n as f64).sqrt() as u64 + 1 +} + +fn divisors_slow(n: u64) -> HashSet { + let mut ret = HashSet::new(); + ret.insert(n); + for i in 1..sq(n) { + if n % i == 0 { + ret.insert(i); + ret.insert(n/i); + } + } + ret.insert(1); + ret +} + +fn divisors_slow2(n: u64) -> HashSet { + let mut ret = HashSet::new(); + ret.insert(n); + for i in 1..n/2 { + let o = n/i; + let ok = n % i == 0; + if ok && o <= 50 { + ret.insert(i); + } + if ok && i <= 50 { + ret.insert(o); + } + } + ret +} + +fn presents(n: u64) -> u64 { + divisors_slow(n).into_iter().map(|v| v * 10).sum() +} + +fn presents2(n: u64) -> u64 { + divisors_slow2(n).into_iter().map(|v| v * 11).sum() +} + +fn foo(n: u64) -> HashMap> { + let mut h: HashMap> = HashMap::new(); + for i in 1..=n { + for j in 1..=50 { + h.entry(i*j).or_default().insert(i); + } + } + + h +} + +fn main() { + assert_eq!(10, presents(1)); + assert_eq!(30, presents(2)); + assert_eq!(40, presents(3)); + assert_eq!(70, presents(4)); + assert_eq!(60, presents(5)); + assert_eq!(120, presents(6)); + assert_eq!(80, presents(7)); + assert_eq!(150, presents(8)); + assert_eq!(130, presents(9)); + assert_eq!(foo(5000)[&1300], dbg!(divisors_slow2(1300))); + let min = 29000000; + for i in 1.. { + let p = presents(i); + if p >= min { + println!("found: {} - {}", i, p); + break + } + } + for i in 700000.. { + let p = presents2(i); + if p >= min { + println!("found: {} - {}", i, p); + break + } + } + + + // the answer is to high 1451008 + // part 2 is too high 2636363 +} -- 2.45.2