Skip to content

Commit

Permalink
Day 04a
Browse files Browse the repository at this point in the history
  • Loading branch information
lpenz committed Dec 4, 2023
1 parent 0bbf74d commit 6be86a6
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ members = [
"day01",
"day02",
"day03",
"day04",
]

9 changes: 9 additions & 0 deletions day04/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
29 changes: 29 additions & 0 deletions day04/src/bin/day04a.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (C) 2023 Leandro Lisboa Penz <[email protected]>
// 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<u32> {
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(())
}
43 changes: 43 additions & 0 deletions day04/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (C) 2023 Leandro Lisboa Penz <[email protected]>
// 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<u32>, Vec<u32>)> {
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<(Vec<u32>, Vec<u32>)>> {
aoc::parse_with!(multi::many1(line), bufin)
}
}

#[test]
fn test() -> Result<()> {
assert_eq!(parser::parse(EXAMPLE.as_bytes())?.len(), 6);
Ok(())
}

0 comments on commit 6be86a6

Please sign in to comment.