From 6be86a674eb82605d05c1b29cbee325c767027a1 Mon Sep 17 00:00:00 2001 From: Leandro Lisboa Penz Date: Mon, 4 Dec 2023 18:09:45 +0000 Subject: [PATCH] Day 04a --- Cargo.lock | 9 +++++++++ Cargo.toml | 1 + day04/Cargo.toml | 9 +++++++++ day04/src/bin/day04a.rs | 29 +++++++++++++++++++++++++++ day04/src/lib.rs | 43 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 91 insertions(+) create mode 100644 day04/Cargo.toml create mode 100644 day04/src/bin/day04a.rs create mode 100644 day04/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index f26304e..ff38ca2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -169,6 +169,15 @@ dependencies = [ "sqrid", ] +[[package]] +name = "day04" +version = "0.1.0" +dependencies = [ + "aoc", + "color-eyre", + "nom", +] + [[package]] name = "either" version = "1.9.0" diff --git a/Cargo.toml b/Cargo.toml index 7ebe468..f89f367 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,5 +6,6 @@ members = [ "day01", "day02", "day03", + "day04", ] diff --git a/day04/Cargo.toml b/day04/Cargo.toml new file mode 100644 index 0000000..514e63f --- /dev/null +++ b/day04/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day04" +version = "0.1.0" +edition = "2021" + +[dependencies] +aoc = { path = "../aoc" } +color-eyre = "0.6.2" +nom = "7.1.3" diff --git a/day04/src/bin/day04a.rs b/day04/src/bin/day04a.rs new file mode 100644 index 0000000..169ed57 --- /dev/null +++ b/day04/src/bin/day04a.rs @@ -0,0 +1,29 @@ +// Copyright (C) 2023 Leandro Lisboa Penz +// This file is subject to the terms and conditions defined in +// file 'LICENSE', which is part of this source code package. + +use std::io::{stdin, BufRead}; + +use day04::*; + +fn process(bufin: impl BufRead) -> Result { + let input = parser::parse(bufin)?; + Ok(input + .into_iter() + .map(|(winners, have)| have.into_iter().filter(|h| winners.contains(h)).count() as u32) + .filter(|m| m > &0) + .map(|m| 2_u32.pow(m - 1)) + .sum()) +} + +#[test] +fn test() -> Result<()> { + assert_eq!(process(EXAMPLE.as_bytes())?, 13); + Ok(()) +} + +fn main() -> Result<()> { + color_eyre::install()?; + println!("{}", process(stdin().lock())?); + Ok(()) +} diff --git a/day04/src/lib.rs b/day04/src/lib.rs new file mode 100644 index 0000000..17903db --- /dev/null +++ b/day04/src/lib.rs @@ -0,0 +1,43 @@ +// Copyright (C) 2023 Leandro Lisboa Penz +// This file is subject to the terms and conditions defined in +// file 'LICENSE', which is part of this source code package. + +pub use color_eyre::{eyre::eyre, Result}; + +pub const EXAMPLE: &str = "Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 +Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 +Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 +Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 +Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 +Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11 +"; + +pub mod parser { + use aoc::parser::*; + + // use super::*; + + fn line(input: &str) -> IResult<&str, (Vec, Vec)> { + let (input, _) = bytes::tag("Card")(input)?; + let (input, _) = character::space1(input)?; + let (input, _) = character::u32(input)?; + let (input, _) = bytes::tag(":")(input)?; + let (input, _) = character::space1(input)?; + let (input, winners) = multi::separated_list1(character::space1, character::u32)(input)?; + let (input, _) = bytes::tag(" |")(input)?; + let (input, _) = character::space1(input)?; + let (input, have) = multi::separated_list1(character::space1, character::u32)(input)?; + let (input, _) = character::newline(input)?; + Ok((input, (winners, have))) + } + + pub fn parse(mut bufin: impl BufRead) -> Result, Vec)>> { + aoc::parse_with!(multi::many1(line), bufin) + } +} + +#[test] +fn test() -> Result<()> { + assert_eq!(parser::parse(EXAMPLE.as_bytes())?.len(), 6); + Ok(()) +}