From: Jack Kinsey Date: Fri, 20 Dec 2024 03:20:41 +0000 (-0500) Subject: Complete day 19 part 1 X-Git-Url: http://git.jkinsey.net/?a=commitdiff_plain;h=bb02fc2443813df6da0a5d3aad5570e8072f85d1;p=adventofcode2024.git Complete day 19 part 1 I tidied it up slightly before committing. --- diff --git a/src/day19.rs b/src/day19.rs new file mode 100644 index 0000000..0c6544a --- /dev/null +++ b/src/day19.rs @@ -0,0 +1,91 @@ +use std::collections::HashSet; + +fn parse(input: &str) -> (HashSet<&str>, Vec<&str>) { + let [tokens, buffers] = input.split("\n\n").collect::>()[..] 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) + } +} diff --git a/src/main.rs b/src/main.rs index 119095a..c52b168 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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),