]> localhost Git - adventofcode2024.git/commitdiff
Complete day 19 part 1
authorJack Kinsey <j.jameskinsey@gmail.com>
Fri, 20 Dec 2024 03:20:41 +0000 (22:20 -0500)
committerJack Kinsey <j.jameskinsey@gmail.com>
Fri, 20 Dec 2024 03:20:41 +0000 (22:20 -0500)
I tidied it up slightly before committing.

src/day19.rs [new file with mode: 0644]
src/main.rs

diff --git a/src/day19.rs b/src/day19.rs
new file mode 100644 (file)
index 0000000..0c6544a
--- /dev/null
@@ -0,0 +1,91 @@
+use std::collections::HashSet;
+
+fn parse(input: &str) -> (HashSet<&str>, Vec<&str>) {
+    let [tokens, buffers] = input.split("\n\n").collect::<Vec<_>>()[..] else {
+        panic!()
+    };
+    (tokens.split(", ").collect(), buffers.lines().collect())
+}
+
+fn consume<'a>(tokens: &HashSet<&str>, buf: &'a str, i: usize, j: usize) -> &'a str {
+    if j > buf.len() {
+        &buf[i..]
+    } else if tokens.contains(&buf[i..j]) {
+        if j == buf.len() {
+            ""
+        } else if !consume(tokens, buf, j, j + 1).is_empty() {
+            consume(tokens, buf, i, j + 1)
+        } else {
+            ""
+        }
+    } else {
+        consume(tokens, buf, i, j + 1)
+    }
+}
+
+pub fn part1(input: &str) -> u64 {
+    let (tokens, buffers) = parse(input);
+    buffers
+        .iter()
+        .filter(|&b| consume(&tokens, b, 0, 0).is_empty())
+        .count()
+        .try_into()
+        .unwrap()
+}
+
+pub fn part2(input: &str) -> u64 {
+    0
+}
+
+#[cfg(test)]
+mod test {
+    use super::*;
+
+    const INPUT_STR: &str = concat!(
+        "r, wr, b, g, bwu, rb, gb, br\n",
+        "\n",
+        "brwrr\n",
+        "bggr\n",
+        "gbbr\n",
+        "rrbgbr\n",
+        "ubwu\n",
+        "bwurrg\n",
+        "brgr\n",
+        "bbrgwb\n",
+    );
+
+    #[test]
+    fn test_parse() {
+        assert_eq!(
+            parse(INPUT_STR),
+            (
+                HashSet::from(["g", "rb", "bwu", "wr", "r", "br", "b", "gb"]),
+                vec!["brwrr", "bggr", "gbbr", "rrbgbr", "ubwu", "bwurrg", "brgr", "bbrgwb"]
+            )
+        )
+    }
+
+    #[test]
+    fn test_consume() {
+        let (tokens, _) = parse(INPUT_STR);
+        assert_eq!(consume(&tokens, "brwrr", 0, 0), "");
+        assert_eq!(consume(&tokens, "bggr", 0, 0), "");
+        assert_eq!(consume(&tokens, "gbbr", 0, 0), "");
+        assert_eq!(consume(&tokens, "rrbgbr", 0, 0), "");
+        assert_eq!(consume(&tokens, "ubwu", 0, 0), "ubwu");
+        assert_eq!(consume(&tokens, "bwurrg", 0, 0), "");
+        assert_eq!(consume(&tokens, "brgr", 0, 0), "");
+        assert_eq!(consume(&tokens, "bbrgwb", 0, 0), "bbrgwb");
+    }
+
+    #[test]
+    fn test_part1() {
+        assert_eq!(part1(&crate::input(19).unwrap()), 285)
+    }
+
+    #[test]
+    #[ignore]
+    fn test_part2() {
+        assert_eq!(part2(&crate::input(0).unwrap()), 0)
+    }
+}
index 119095a28de6b25d6561633d322f3ace2b61cf55..c52b16896a047cdc7ebd79a554ded562e3b8e491 100644 (file)
@@ -12,11 +12,11 @@ pub mod day11;
 pub mod day12;
 pub mod day13;
 pub mod day14;
-pub mod day15;
+// pub mod day15;
 // pub mod day16;
 // pub mod day17;
 // pub mod day18;
-// pub mod day19;
+pub mod day19;
 // pub mod day20;
 // pub mod day21;
 // pub mod day22;
@@ -41,11 +41,15 @@ const DAYS: &[(Part, Part)] = &[
     (day12::part1, day12::part2),
     (day13::part1, day13::part2),
     (day14::part1, day14::part2),
-    (day15::part1, day15::part2),
+    // (day15::part1, day15::part2),
     // (day16::part1, day16::part2),
     // (day17::part1, day17::part2),
     // (day18::part1, day18::part2),
-    // (day19::part1, day19::part2),
+    (day19::part1, day19::part2),
+    (day19::part1, day19::part2),
+    (day19::part1, day19::part2),
+    (day19::part1, day19::part2),
+    (day19::part1, day19::part2),
     // (day20::part1, day20::part2),
     // (day21::part1, day21::part2),
     // (day22::part1, day22::part2),