summary refs log tree commit diff homepage
path: root/2016/src/bin/day05.rs
blob: eefa3df86c126cdb913d5e113ee2e27a8b646294 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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"));
}
lass='logsubject'>Add two Kim Petras songsJune McEnroe 2019-10-02Update neovim 0.4.2June McEnroe Finally a release with my man mode fix... Meanwhile neovim itself has probably gotten worse. 2019-10-02Claim to be curl(1) in titleJune McEnroe IMDB serves a page to our dumb User-Agent whose <title> is past the 8K boundary but serves something normal to curl(1). 2019-10-02Add The Red Threads of FortuneJune McEnroe 2019-09-28Add The Black Tides of HeavenJune McEnroe 2019-09-27Fail on HTTP failure status in titleJune McEnroe 2019-09-23Add Trail of LightningJune McEnroe 2019-09-22Revert "Enable cookies in title"June McEnroe This reverts commit 279111dda15dd9170e11b9688eb973f2af2e6300. 2019-09-20Enable cookies in titleJune McEnroe Perhaps this will make it less suspicious to Google. Who knows. 2019-09-16Use sensitivity aliases in TF2June McEnroe 2019-09-16Add The Just CityJune McEnroe 2019-09-12Only GET the final redirect locationJune McEnroe 2019-09-12Consume entire bodyJune McEnroe Aborting the request and leaving data around may be causing intermittent errors. Just discard the rest of the data. 2019-09-10Add title -v flagJune McEnroe 2019-09-10Use curl error bufferJune McEnroe 2019-09-10Set Accept-Encoding in titleJune McEnroe Because apparently it's fine for servers to respond with Content-Encoding you didn't ask for, and curl won't decode it if you didn't ask for it. 2019-09-08Set title User-AgentJune McEnroe Some things don't like you if you don't send one. 2019-09-07Add -x flag to titleJune McEnroe 2019-09-07Ignore SIGPIPE in relayJune McEnroe Allows restarting consumers safely. 2019-09-07Add A Memory Called EmpireJune McEnroe 2019-09-05Handle lack of Content-TypeJune McEnroe 2019-09-05Use CURLINFO_CONTENT_TYPEJune McEnroe Oops, didn't see this. 2019-09-05Decode entities in titlesJune McEnroe 2019-09-05Print title as soon as it's availableJune McEnroe 2019-09-05Use CURL_PREFIX to set flagsJune McEnroe 2019-09-05Add titleJune McEnroe 2019-09-04Add Avorter n'est pas tuerJune McEnroe 2019-08-29Unset executable on shell scriptsJune McEnroe 2019-08-29Add long-missing setopt to bin.7June McEnroe 2019-08-29Add editJune McEnroe