summaryrefslogtreecommitdiff
path: root/aoc14/src/main.rs
diff options
context:
space:
mode:
authorJustin Worthe <justin.worthe@entelect.co.za>2016-12-14 11:05:29 +0200
committerJustin Worthe <justin.worthe@entelect.co.za>2016-12-14 11:05:29 +0200
commit26300d0d547e74cdd431c84f383616ce9f6e5499 (patch)
tree6607448adf6bf9f3dd81ff908706130ddf50ac74 /aoc14/src/main.rs
parenta2d46254c1045eef2fbe3e4d42c1c04e8827cda7 (diff)
AOC14
Should work for part 2, but the performance isn't there yet
Diffstat (limited to 'aoc14/src/main.rs')
-rw-r--r--aoc14/src/main.rs79
1 files changed, 79 insertions, 0 deletions
diff --git a/aoc14/src/main.rs b/aoc14/src/main.rs
new file mode 100644
index 0000000..e68e6a7
--- /dev/null
+++ b/aoc14/src/main.rs
@@ -0,0 +1,79 @@
+extern crate md5;
+
+fn main() {
+// let input = "abc";
+ let input = "yjdafjpo";
+ let mut index = 0;
+ let mut results_found = 0;
+ while results_found < 64 {
+ let hash = stretched_hash(format!("{}{}", input, index));
+
+ 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);
+ 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) -> String {
+ let mut result = input;
+ for _ in 0..2017 {
+ result = string_hash(result);
+ }
+ result
+}
+
+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)
+}