summaryrefslogtreecommitdiffstats
path: root/day06b/src/main.rs
blob: 7e98082b17210fa78c1f5002fd1683e1f72fd3a0 (plain)
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
/// --- Part Two ---
///
/// Your device's communication system is correctly detecting packets, but still isn't working.  It
/// looks like it also needs to look for messages.
///
/// A start-of-message marker is just like a start-of-packet marker, except it consists of 14
/// distinct characters rather than 4.
///
/// Here are the first positions of start-of-message markers for all of the above examples:
///
///     mjqjpqmgbljsphdztnvjfqwrcgsmlb: first marker after character 19
///     bvwbjplbgvbhsrlpgdmjqwftvncz: first marker after character 23
///     nppdvjthqldpwncqszvftbrmjlhg: first marker after character 23
///     nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg: first marker after character 29
///     zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw: first marker after character 26
///
/// How many characters need to be processed before the first start-of-message marker is detected?
use clap::Parser;
use itertools::Itertools;

use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::iter::*;
use std::path::PathBuf;

const FILEPATH: &'static str = "examples/input.txt";
const CONS_CHARS: usize = 14;

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Cli {
    #[clap(short, long, default_value = FILEPATH)]
    file: PathBuf,
}

fn main() {
    let args = Cli::parse();

    let file = File::open(&args.file).unwrap();
    let reader = BufReader::new(file);
    let line = reader.lines().next().unwrap().unwrap();
    let res = line
        .into_bytes()
        .windows(CONS_CHARS)
        .position(|b| b.iter().all_unique())
        .unwrap()
        + CONS_CHARS;

    println!("{:?}", res);
}