summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Worthe <justin.worthe@entelect.co.za>2016-12-14 12:19:50 +0200
committerJustin Worthe <justin.worthe@entelect.co.za>2016-12-14 12:19:50 +0200
commit30898c25fcd0d12a4f361e8b237c8dfb4b8172ea (patch)
tree47ee2ebaf8f914b72948d9160f546c992356a932
parent26300d0d547e74cdd431c84f383616ce9f6e5499 (diff)
AOC14 added memoization of hashing to run in reasonable time
-rw-r--r--aoc14/src/main.rs25
1 files changed, 15 insertions, 10 deletions
diff --git a/aoc14/src/main.rs b/aoc14/src/main.rs
index e68e6a7..d9410ce 100644
--- a/aoc14/src/main.rs
+++ b/aoc14/src/main.rs
@@ -1,19 +1,23 @@
extern crate md5;
+use std::collections::HashMap;
+
fn main() {
// let input = "abc";
let input = "yjdafjpo";
let mut index = 0;
let mut results_found = 0;
+
+ let mut hash_memo = HashMap::new();
while results_found < 64 {
- let hash = stretched_hash(format!("{}{}", input, index));
+ let hash = stretched_hash(format!("{}{}", input, index), &mut hash_memo);
let threes = find_concurrent_symbols(&hash, 3, true);
if threes.len() > 0 {
// println!("Found three at {} -> {}", index, hash);
for i in 1..1001 {
- let hash = stretched_hash(format!("{}{}", input, index+i));
- let fives = find_concurrent_symbols(&hash, 5, true);
+ let hash = stretched_hash(format!("{}{}", input, index+i), &mut hash_memo);
+ let fives = find_concurrent_symbols(&hash, 5, false);
if fives.iter().any(|c| threes.contains(c)) {
results_found += 1;
// println!("Five found at {} -> {}", index+i, hash);
@@ -54,7 +58,6 @@ fn find_concurrent_symbols(hash: &String, count: u8, exit_early: bool) -> Vec<ch
matches
}
-
fn hash_to_string(hash: &[u8; 16]) -> String {
let mut result = String::with_capacity(32);
@@ -64,12 +67,14 @@ fn hash_to_string(hash: &[u8; 16]) -> String {
result
}
-fn stretched_hash(input: String) -> String {
- let mut result = input;
- for _ in 0..2017 {
- result = string_hash(result);
- }
- result
+fn stretched_hash(input: String, memo: &mut HashMap<String, String>) -> String {
+ memo.entry(input.clone()).or_insert_with(|| {
+ let mut result = input;
+ for _ in 0..2017 {
+ result = string_hash(result);
+ }
+ result
+ }).clone()
}
fn string_hash(input: String) -> String {