From b5da2fa328ad63d683d66da65e656ed2ee64878c Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Mon, 27 Nov 2017 17:11:18 -0500 Subject: Move to 2016 directory --- 2016/src/bin/day05.rs | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 2016/src/bin/day05.rs (limited to '2016/src/bin/day05.rs') diff --git a/2016/src/bin/day05.rs b/2016/src/bin/day05.rs new file mode 100644 index 0000000..eefa3df --- /dev/null +++ b/2016/src/bin/day05.rs @@ -0,0 +1,71 @@ +extern crate crypto; + +use std::io::{self, Read}; + +use crypto::digest::Digest; +use crypto::md5::Md5; + +fn solve1(input: &str) -> String { + let mut password = String::new(); + let mut index = 0u64; + + while password.len() < 8 { + let mut md5 = Md5::new(); + md5.input_str(input); + md5.input_str(&index.to_string()); + let digest = md5.result_str(); + + if &digest[0..5] == "00000" { + password.push_str(&digest[5..6]); + } + + index += 1; + } + + password +} + +fn solve2(input: &str) -> String { + let mut password = [None; 8]; + let mut index = 0u64; + + while !password.iter().all(Option::is_some) { + let mut md5 = Md5::new(); + md5.input_str(input); + md5.input_str(&index.to_string()); + let digest = md5.result_str(); + + if &digest[0..5] == "00000" { + if let Some(pos @ 0 ... 7) = digest[5..6].parse().ok() { + password[pos] = password[pos].or(digest[6..7].chars().next()); + } + } + + index += 1; + } + + password.iter() + .cloned() + .map(Option::unwrap) + .collect() +} + +fn main() { + let mut input = String::new(); + io::stdin().read_to_string(&mut input).unwrap(); + + println!("Part 1: {}", solve1(input.trim())); + println!("Part 2: {}", solve2(input.trim())); +} + +#[test] +#[ignore] +fn part1() { + assert_eq!("18f47a30", solve1("abc")); +} + +#[test] +#[ignore] +fn part2() { + assert_eq!("05ace8e3", solve2("abc")); +} -- cgit 1.4.1 be1d4bec5fc0eea8b5548ad074fb1cff09b55ac7&showmsg=1&follow=1'>root/bin/dash/src/mkbuiltins (unfollow)
Commit message (Collapse)Author
2021-01-13Update Terminal.app coloursJune McEnroe
God what is this garbage I only changed one colour.
2021-01-13Increase dark white brightness slightlyJune McEnroe
2021-01-13Add hilex example to htagml manualJune McEnroe
2021-01-12Style causal.agency like bin HTMLJune McEnroe
2021-01-12Avoid matching tag text inside HTML elementsJune McEnroe
2021-01-12Use hilex for up -hJune McEnroe
2021-01-12Use hilex for bin HTMLJune McEnroe
2021-01-12Don't output a pre in hilex by defaultJune McEnroe
2021-01-12Move hilex out of hilex directoryJune McEnroe
2021-01-12Consolidate hilex formatters into hilex.cJune McEnroe
2021-01-12Remove hacky tagging from hilexJune McEnroe
God that makes the lexers so much simpler.
2021-01-12Add htagml -iJune McEnroe
2021-01-12Render tag index in HTMLJune McEnroe
2021-01-12Add htagml -xJune McEnroe
2021-01-12Prevent matching the same tag twiceJune McEnroe
2021-01-12Process htagml file line by lineJune McEnroe
This simplifies some things, adds support for line number tag definitions, and should enable combining htagml with other preprocessors in the future.
2021-01-12Split fields by tab onlyJune McEnroe
Also don't fail hard on non-forward-search definitions.
2021-01-12List both Makefile and html.sh under README.7June McEnroe
2021-01-12Add htagml exampleJune McEnroe
2021-01-12Use mandoc and htagml for bin htmlJune McEnroe
2021-01-12Add htagmlJune McEnroe
2021-01-12Replace causal.agency with a simple mdoc pageJune McEnroe
2021-01-11Publish "Using vi"June McEnroe
2021-01-11Enable diff.colorMovedJune McEnroe
2021-01-10Set less search case-insensitiveJune McEnroe
2021-01-10Set EXINITJune McEnroe
neovim is laggy as hell in my OpenBSD VM, so I switched to vi so I could type without getting frustrated.
2021-01-09Add c -t flag to print expression typeJune McEnroe
Also add missing float case.
2021-01-05Update taglineJune McEnroe