summary refs log tree commit diff homepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/bin/day07.rs83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/bin/day07.rs b/src/bin/day07.rs
new file mode 100644
index 0000000..b2289f0
--- /dev/null
+++ b/src/bin/day07.rs
@@ -0,0 +1,83 @@
+use std::io::{self, Read};
+use std::str::FromStr;
+
+struct Sequence {
+    sequence: String,
+    hypernet: bool,
+}
+
+impl Sequence {
+    fn has_abba(&self) -> bool {
+        self.sequence
+            .as_bytes()
+            .windows(4)
+            .any(|window| {
+                window[0] == window[3]
+                    && window[1] == window[2]
+                    && window[0] != window[1]
+            })
+    }
+}
+
+struct Ip(Vec<Sequence>);
+
+impl Ip {
+    fn supports_tls(&self) -> bool {
+        let any_abbas = self.0
+            .iter()
+            .any(Sequence::has_abba);
+
+        let any_hypernet_abbas = self.0
+            .iter()
+            .filter(|sequence| sequence.hypernet)
+            .any(Sequence::has_abba);
+
+        any_abbas && !any_hypernet_abbas
+    }
+}
+
+impl FromStr for Ip {
+    type Err = ();
+    fn from_str(s: &str) -> Result<Ip, ()> {
+        let mut sequences = Vec::new();
+        let mut hypernet = false;
+
+        for sequence in s.split(|ch| ch == '[' || ch == ']') {
+            sequences.push(
+                Sequence {
+                    sequence: sequence.to_owned(),
+                    hypernet: hypernet
+                }
+            );
+            hypernet = !hypernet;
+        }
+
+        Ok(Ip(sequences))
+    }
+}
+
+fn solve(input: &str) -> usize {
+    input.lines()
+        .map(str::parse)
+        .map(Result::unwrap)
+        .filter(Ip::supports_tls)
+        .count()
+}
+
+fn main() {
+    let mut input = String::new();
+    io::stdin().read_to_string(&mut input).unwrap();
+
+    println!("Part 1: {}", solve(&input));
+}
+
+#[test]
+fn part1() {
+    let input = "
+abba[mnop]qrst
+abcd[bddb]xyyx
+aaaa[qwer]tyui
+ioxxoj[asdfgh]zxcvbn
+";
+    assert_eq!(2, solve(input.trim()));
+}
> 2022-08-04Add Conversations With FriendsJune McEnroe 2022-07-30Add Normal PeopleJune McEnroe 2022-07-26Rewrite glitch from new pngoJune McEnroe 2022-07-26Update Care with time-to-ID and piercingsJune McEnroe 2022-07-26Add -w to upJune McEnroe 2022-07-13Set push.autoSetupRemoteJune McEnroe 2022-07-08Remove TOURJune McEnroe 2022-07-03Add The Bone Shard EmperorJune McEnroe 2022-06-25Bump xterm font size to 12June McEnroe 2022-06-10Handle subshells (and functions) inside substitutionsJune McEnroe 2022-06-10Switch to jorts Install scriptJune McEnroe 2022-06-08Indicate if still reading or no resultsJune McEnroe 2022-06-08Add Maiden, Mother, CroneJune McEnroe 2022-06-05FIRST SHOW IN 2.5 YEARS BABEY!!!June McEnroe 2022-06-03Set line number on File linesJune McEnroe 2022-06-03Stop polling stdin after EOFJune McEnroe 2022-06-02Set TABSIZE=4June McEnroe 2022-06-02Do basic match highlightingJune McEnroe 2022-06-02Clean up parsing a littleJune McEnroe 2022-06-02Don't duplicate path stringJune McEnroe 2022-06-02Use stderr instead of /dev/tty, realloc buffer if lines too longJune McEnroe 2022-06-02Add initial working version of qfJune McEnroe 2022-05-29Set prompt for okshJune McEnroe