summary refs log tree commit diff homepage
path: root/2016/src/bin/day07.rs
blob: b4967ffc15cd98f3ce3b55cebd4e6173b03fea4c (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use std::io::{self, Read};
use std::str::FromStr;

fn has_abba(s: &str) -> bool {
    s.as_bytes()
        .windows(4)
        .any(|window| {
            window[0] == window[3]
                && window[1] == window[2]
                && window[0] != window[1]
        })
}

fn abas(s: &str) -> Vec<&[u8]> {
    s.as_bytes()
        .windows(3)
        .filter(|window| {
            window[0] == window[2]
                && window[0] != window[1]
        })
        .collect()
}

fn has_bab(s: &str, aba: &[u8]) -> bool {
    s.as_bytes()
        .windows(3)
        .any(|window| {
            window[0] == aba[1]
                && window[1] == aba[0]
                && window[2] == aba[1]
        })
}

#[derive(Default)]
struct Ip {
    supernet: Vec<String>,
    hypernet: Vec<String>,
}

impl Ip {
    fn supports_tls(&self) -> bool {
        self.supernet.iter().any(|s| has_abba(s))
            && !self.hypernet.iter().any(|s| has_abba(s))
    }

    fn supports_ssl(&self) -> bool {
        self.supernet
            .iter()
            .flat_map(|s| abas(s))
            .any(|aba| {
                self.hypernet
                    .iter()
                    .any(|s| has_bab(s, aba))
            })
    }
}

impl FromStr for Ip {
    type Err = ();
    fn from_str(s: &str) -> Result<Ip, ()> {
        let mut ip = Ip::default();

        for (i, seq) in s.split(|ch| ch == '[' || ch == ']').enumerate() {
            if i % 2 == 0 {
                ip.supernet.push(seq.to_owned());
            } else {
                ip.hypernet.push(seq.to_owned());
            }
        }

        Ok(ip)
    }
}

fn solve1(input: &str) -> usize {
    input.lines()
        .map(str::parse)
        .map(Result::unwrap)
        .filter(Ip::supports_tls)
        .count()
}

fn solve2(input: &str) -> usize {
    input.lines()
        .map(str::parse)
        .map(Result::unwrap)
        .filter(Ip::supports_ssl)
        .count()
}

fn main() {
    let mut input = String::new();
    io::stdin().read_to_string(&mut input).unwrap();

    println!("Part 1: {}", solve1(&input));
    println!("Part 2: {}", solve2(&input));
}

#[test]
fn part1() {
    let input = "
abba[mnop]qrst
abcd[bddb]xyyx
aaaa[qwer]tyui
ioxxoj[asdfgh]zxcvbn
";
    assert_eq!(2, solve1(input.trim()));
}

#[test]
fn part2() {
    let input = "
aba[bab]xyz
xyx[xyx]xyx
aaa[kek]eke
zazbz[bzb]cdb
";
    assert_eq!(3, solve2(input.trim()));
}
0500'>2022-02-20Clear edit buffer before running commandJune McEnroe Otherwise a command that switches windows will update the status line while the edit buffer still has input "pending", showing an indicator. 2022-02-20Show indicator in status when window has pending inputJune McEnroe 2022-02-20Use separate edit buffers for each IDJune McEnroe 2022-02-20Make sure new cap is actually larger than new lengthJune McEnroe 2022-02-20Remove unused mbs.len field from struct EditJune McEnroe 2022-02-19Remove unneeded includes in ui.cJune McEnroe 2022-02-19Reimplement tab completeJune McEnroe 2022-02-19Handle errors from editFn, etc.June McEnroe 2022-02-19Reimplement text macrosJune McEnroe 2022-02-19Factor out input handling to input.cJune McEnroe 2022-02-19Factor out window management to window.cJune McEnroe 2022-02-19Enable -Wmissing-prototypesJune McEnroe In other words, warn when a function is missing static. I don't see why this isn't in -Wextra. 2022-02-19Fix edit.[ch] license notice additional permissionsJune McEnroe 2022-02-19Run line editing testsJune McEnroe I know, it feels wrong. 2022-02-18Implement new line editing "library"June McEnroe Losing tab complete and text macros, for now. This new implementation works on an instance of a struct and does not interact with the rest of catgirl, making it possible to copy into another project. Unlike existing line editing libraries, this one is entirely abstract and can be rendered externally. My goal with this library is to be able to implement vi mode. Since it operates on struct instances rather than globals, it might also be possible to give catgirl separate line editing buffers for each window, which would be a nice UX improvement. 2022-02-18Simplify cursor positioning in inputJune McEnroe Do some extra work by adding the portion before the cursor to the input window twice, but simplify the interaction with the split point. This fixes the awkward behaviour when moving the cursor across colour codes where the code would be partially interpreted up to the cursor. 2022-02-18Fix M-f orderingJune McEnroe 2022-02-12Move sandman build to scripts/MakefileJune McEnroe 2022-02-12Use compat_readpassphrase.c on LinuxJune McEnroe 2022-02-12Copy RPP defines from oconfigureJune McEnroe