2015: day04: part 1
This commit is contained in:
parent
a81ad7c114
commit
ce83c56841
6 changed files with 129 additions and 1 deletions
|
|
@ -9,3 +9,4 @@ edition = "2018"
|
|||
[dependencies]
|
||||
|
||||
aoc = { path = "../aoc" }
|
||||
md-5 = "0.8"
|
||||
|
|
|
|||
1
aoc2015/input/day04.txt
Normal file
1
aoc2015/input/day04.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
yzbqklnj
|
||||
48
aoc2015/src/day04.rs
Normal file
48
aoc2015/src/day04.rs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
use md5::{Digest, Md5};
|
||||
|
||||
use aoc::err;
|
||||
use aoc::Result;
|
||||
|
||||
const INPUT: &str = include_str!("../input/day04.txt");
|
||||
|
||||
pub fn run() -> Result<()> {
|
||||
println!("part 1: {}", part1(INPUT)?);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn part1(input: &str) -> Result<u64> {
|
||||
let input = input.trim_end();
|
||||
|
||||
let mut hasher = Md5::new();
|
||||
|
||||
for i in 0.. {
|
||||
let content = format!("{}{}", input, i);
|
||||
|
||||
hasher.input(content);
|
||||
let res = format!("{:x}", hasher.result_reset());
|
||||
if &res[..5] == "00000" {
|
||||
return Ok(i);
|
||||
}
|
||||
}
|
||||
|
||||
Err(err!("couldn't find a suitable number"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
#[ignore] // takes too long!
|
||||
fn part1_provided() {
|
||||
assert_eq!(part1("abcdef").unwrap(), 609043);
|
||||
assert_eq!(part1("pqrstuv").unwrap(), 1048970);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore] // takes too long!
|
||||
fn part1_real() {
|
||||
assert_eq!(part1(INPUT).unwrap(), 282749);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
pub mod day01;
|
||||
pub mod day02;
|
||||
pub mod day03;
|
||||
pub mod day04;
|
||||
|
|
|
|||
|
|
@ -3,9 +3,10 @@ use aoc::Result;
|
|||
use aoc2015::day01;
|
||||
use aoc2015::day02;
|
||||
use aoc2015::day03;
|
||||
use aoc2015::day04;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let days: &[fn() -> Result<()>] = &[day01::run, day02::run, day03::run];
|
||||
let days: &[fn() -> Result<()>] = &[day01::run, day02::run, day03::run, day04::run];
|
||||
|
||||
aoc::run(days)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue