summaryrefslogtreecommitdiff
path: root/2016/aoc14
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2022-04-19 20:22:56 +0200
committerJustin Wernick <justin@worthe-it.co.za>2022-04-19 20:23:15 +0200
commit174772b5b8d9f5bf5e3c8e8152adfd89f0e83f6b (patch)
treea003b748ee939b30a2bcd3caf2378228baa304c1 /2016/aoc14
parentfd75b3fb95ad049b0025cb8fc0b3459b8f872d61 (diff)
Refile for merging repos
Diffstat (limited to '2016/aoc14')
-rw-r--r--2016/aoc14/Cargo.lock14
-rw-r--r--2016/aoc14/Cargo.toml7
-rw-r--r--2016/aoc14/src/main.rs84
3 files changed, 105 insertions, 0 deletions
diff --git a/2016/aoc14/Cargo.lock b/2016/aoc14/Cargo.lock
new file mode 100644
index 0000000..a889c91
--- /dev/null
+++ b/2016/aoc14/Cargo.lock
@@ -0,0 +1,14 @@
+[root]
+name = "aoc14"
+version = "0.1.0"
+dependencies = [
+ "md5 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "md5"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[metadata]
+"checksum md5 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7df230903ccdffd6b3b4ec21624498ea64c912ce50297846907f0b8e1bb249dd"
diff --git a/2016/aoc14/Cargo.toml b/2016/aoc14/Cargo.toml
new file mode 100644
index 0000000..eb3f92d
--- /dev/null
+++ b/2016/aoc14/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "aoc14"
+version = "0.1.0"
+authors = ["Justin Worthe <justin.worthe@entelect.co.za>"]
+
+[dependencies]
+md5 = "^0.2" \ No newline at end of file
diff --git a/2016/aoc14/src/main.rs b/2016/aoc14/src/main.rs
new file mode 100644
index 0000000..d9410ce
--- /dev/null
+++ b/2016/aoc14/src/main.rs
@@ -0,0 +1,84 @@
+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), &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), &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);
+ println!("Found hash {} at index {}", results_found, index);
+ break;
+ }
+ }
+ }
+ index += 1;
+ }
+
+}
+
+fn find_concurrent_symbols(hash: &String, count: u8, exit_early: bool) -> Vec<char> {
+ let mut last_symbol = None;
+ let mut last_symbol_run = 0;
+ let mut matches = Vec::new();
+ for c in hash.chars() {
+ let symbol_matches = match last_symbol {
+ Some(s) => s == c,
+ None => false
+ };
+
+ if symbol_matches {
+ last_symbol_run += 1;
+ if last_symbol_run >= count && !matches.contains(&c) {
+ matches.push(c);
+ if exit_early {
+ break;
+ }
+ }
+ } else {
+ last_symbol = Some(c);
+ last_symbol_run = 1;
+ }
+ }
+
+ matches
+}
+
+fn hash_to_string(hash: &[u8; 16]) -> String {
+ let mut result = String::with_capacity(32);
+
+ for &byte in hash.iter() {
+ result.push_str(format!("{:02x}", byte).as_ref());
+ }
+ 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 {
+ let bytes_to_hash = input.into_bytes();
+ let hash = md5::compute(bytes_to_hash.as_slice());
+ hash_to_string(&hash)
+}