summaryrefslogtreecommitdiff
path: root/d05
diff options
context:
space:
mode:
Diffstat (limited to 'd05')
-rw-r--r--d05/Cargo.toml7
-rw-r--r--d05/src/main.rs86
2 files changed, 93 insertions, 0 deletions
diff --git a/d05/Cargo.toml b/d05/Cargo.toml
new file mode 100644
index 0000000..38415e6
--- /dev/null
+++ b/d05/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "d05"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
+md5 = "0.7.0"
diff --git a/d05/src/main.rs b/d05/src/main.rs
new file mode 100644
index 0000000..25986ef
--- /dev/null
+++ b/d05/src/main.rs
@@ -0,0 +1,86 @@
+use md5;
+
+fn solve_part1(seed: &str) -> String {
+ let mut passw = String::new();
+
+ let mut i = 0;
+ loop {
+ let data = format!("{}{}", seed, i.to_string());
+ let hash = format!("{:x}", md5::compute(data));
+
+ // We've mined a block, so-- wait wrong software
+ if hash.starts_with("00000") {
+ passw.push(hash.chars().nth(5).unwrap());
+ if passw.len() >= 8 {
+ break;
+ }
+ }
+
+ i += 1;
+ }
+
+ passw
+}
+
+fn solve_part2(seed: &str) -> String {
+ // We'll replace each `x' with the true character
+ let mut passw = String::from("xxxxxxxx");
+
+ let mut i = 0;
+ loop {
+ let data = format!("{}{}", seed, i.to_string());
+ let hash = format!("{:x}", md5::compute(data));
+
+ // I wonder how this loop affects global power consumption...
+ if hash.starts_with("00000") {
+ let c = hash.chars().nth(5).unwrap();
+ if c.is_ascii_digit() {
+ // Get index of character to replace
+ let k = String::from(c).parse().unwrap();
+
+ // Is `k' in bounds, and haven't we replaced `passw[k]' already?
+ if k < 8 && passw.chars().nth(k).unwrap() == 'x' {
+ let s = String::from(hash.chars().nth(6).unwrap());
+ passw.replace_range(k..k + 1, &s);
+
+ // Have we replaced all characters in `passw'?
+ if !passw.contains('x') {
+ break;
+ }
+ }
+ }
+ }
+
+ i += 1;
+ }
+
+ passw
+}
+
+fn main() {
+ // My personal input ID
+ let id = "ugkcyxxp";
+
+ // Part 1 gives "d4cd2ee1" for me
+ println!("Part 1 solution: {}", solve_part1(id));
+
+ // Part 2 gives "f2c730e5" for me
+ println!("Part 2 solution: {}", solve_part2(id));
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn part1_example1() {
+ let id = "abc";
+ assert_eq!(solve_part1(id), "18f47a30");
+ }
+
+ #[test]
+ fn part2_example1() {
+ let id = "abc";
+ assert_eq!(solve_part2(id), "05ace8e3");
+ }
+}