Solutions for 2022, as well as 2015-2018 and 2019 up to day 11
This commit is contained in:
commit
1895197c49
722 changed files with 375457 additions and 0 deletions
9
2015/day04-ideal_stocking_suffer/Cargo.toml
Normal file
9
2015/day04-ideal_stocking_suffer/Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "day04-ideal_stocking_suffer"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
md-5 = "0.10.5"
|
26
2015/day04-ideal_stocking_suffer/challenge.txt
Normal file
26
2015/day04-ideal_stocking_suffer/challenge.txt
Normal file
|
@ -0,0 +1,26 @@
|
|||
\--- Day 4: The Ideal Stocking Stuffer ---
|
||||
----------
|
||||
|
||||
Santa needs help [mining](https://en.wikipedia.org/wiki/Bitcoin#Mining) some AdventCoins (very similar to [bitcoins](https://en.wikipedia.org/wiki/Bitcoin)) to use as gifts for all the economically forward-thinking little girls and boys.
|
||||
|
||||
To do this, he needs to find [MD5](https://en.wikipedia.org/wiki/MD5) hashes which, in [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal), start with at least *five zeroes*. The input to the MD5 hash is some secret key (your puzzle input, given below) followed by a number in decimal. To mine AdventCoins, you must find Santa the lowest positive number (no leading zeroes: `1`, `2`, `3`, ...) that produces such a hash.
|
||||
|
||||
For example:
|
||||
|
||||
* If your secret key is `abcdef`, the answer is `609043`, because the MD5 hash of `abcdef609043` starts with five zeroes (`000001dbbfa...`), and it is the lowest such number to do so.
|
||||
* If your secret key is `pqrstuv`, the lowest number it combines with to make an MD5 hash starting with five zeroes is `1048970`; that is, the MD5 hash of `pqrstuv1048970` looks like `000006136ef...`.
|
||||
|
||||
Your puzzle answer was `254575`.
|
||||
|
||||
\--- Part Two ---
|
||||
----------
|
||||
|
||||
Now find one that starts with *six zeroes*.
|
||||
|
||||
Your puzzle answer was `1038736`.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: \*\*
|
||||
|
||||
At this point, all that is left is for you to [admire your Advent calendar](/2015).
|
||||
|
||||
Your puzzle input was `bgvyzdsv`.
|
51
2015/day04-ideal_stocking_suffer/src/lib.rs
Normal file
51
2015/day04-ideal_stocking_suffer/src/lib.rs
Normal file
|
@ -0,0 +1,51 @@
|
|||
use md5::{Md5, Digest};
|
||||
|
||||
fn hash_has_leading_zeroes(input: &str, counter: usize, zeroes: usize) -> bool {
|
||||
let mut hasher = Md5::new();
|
||||
hasher.update(input.to_owned() + &(counter.to_string())[..]);
|
||||
let hash = hasher.finalize();
|
||||
for i in 0..zeroes/2 {
|
||||
if hash[i] != 0 {
|
||||
return false;
|
||||
}
|
||||
if zeroes % 2 == 1 && hash[zeroes/2] >= 16 {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
pub fn run(input: &str) -> (usize, usize) {
|
||||
let first = (0..).find(|i| hash_has_leading_zeroes(input, *i, 5)).unwrap();
|
||||
|
||||
let second = (first..).find(|i| hash_has_leading_zeroes(input, *i, 6)).unwrap();
|
||||
|
||||
(first, second)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs::read_to_string;
|
||||
|
||||
fn read_file(name: &str) -> String {
|
||||
read_to_string(name).expect(&format!("Unable to read file: {}", name)[..])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sample() {
|
||||
let sample_input = read_file("tests/sample_input");
|
||||
let expected = [(609043, 6742839), (1048970, 5714438)];
|
||||
for (idx, input) in sample_input.lines().enumerate() {
|
||||
assert_eq!(run(input), expected[idx]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_challenge() {
|
||||
let challenge_input = read_file("tests/challenge_input");
|
||||
for input in challenge_input.lines() {
|
||||
assert_eq!(run(input), (254575, 1038736));
|
||||
}
|
||||
}
|
||||
}
|
1
2015/day04-ideal_stocking_suffer/tests/challenge_input
Normal file
1
2015/day04-ideal_stocking_suffer/tests/challenge_input
Normal file
|
@ -0,0 +1 @@
|
|||
bgvyzdsv
|
2
2015/day04-ideal_stocking_suffer/tests/sample_input
Normal file
2
2015/day04-ideal_stocking_suffer/tests/sample_input
Normal file
|
@ -0,0 +1,2 @@
|
|||
abcdef
|
||||
pqrstuv
|
Loading…
Add table
Add a link
Reference in a new issue