Day 4 part 1
This commit is contained in:
parent
a5969ced97
commit
4284a6997a
6
src/day04/input/test1.txt
Normal file
6
src/day04/input/test1.txt
Normal file
@ -0,0 +1,6 @@
|
||||
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
|
103
src/day04/mod.rs
Normal file
103
src/day04/mod.rs
Normal file
@ -0,0 +1,103 @@
|
||||
use std::{collections::HashSet, str::FromStr};
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
|
||||
use crate::instrument::instrument;
|
||||
|
||||
const INPUT: &str = include_str!("input/input.txt");
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Card {
|
||||
winning_numbers: HashSet<u8>,
|
||||
numbers: HashSet<u8>,
|
||||
id: usize,
|
||||
}
|
||||
|
||||
impl FromStr for Card {
|
||||
type Err = anyhow::Error;
|
||||
fn from_str(s: &str) -> Result<Self> {
|
||||
let mut s = s.trim().split(':');
|
||||
let id = s
|
||||
.next()
|
||||
.ok_or_else(|| anyhow!("Invalid card: no ':' found"))?;
|
||||
let id = id
|
||||
.split(' ')
|
||||
.last()
|
||||
.ok_or_else(|| anyhow!("Invalid card: no number found"))?
|
||||
.parse()?;
|
||||
|
||||
let mut nums = s
|
||||
.next()
|
||||
.ok_or_else(|| anyhow!("Invalid card: nothing after ':' found"))?
|
||||
.split('|');
|
||||
let first_nums = nums
|
||||
.next()
|
||||
.ok_or_else(|| anyhow!("Invalid card: no '|' found"))?
|
||||
.trim();
|
||||
|
||||
let winning_numbers = first_nums
|
||||
.split_whitespace()
|
||||
.map(|num| Ok(num.parse()?))
|
||||
.collect::<Result<HashSet<u8>>>()?;
|
||||
let second_nums = nums
|
||||
.next()
|
||||
.ok_or_else(|| anyhow!("Invalid card: nothing after '|' found"))?
|
||||
.trim();
|
||||
let numbers = second_nums
|
||||
.split_whitespace()
|
||||
.map(|num| Ok(num.parse()?))
|
||||
.collect::<Result<HashSet<u8>>>()?;
|
||||
Ok(Self {
|
||||
winning_numbers,
|
||||
numbers,
|
||||
id,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Card {
|
||||
fn score(&self) -> u32 {
|
||||
let winning = self.winning_numbers.intersection(&self.numbers).count() as u32;
|
||||
if winning == 0 {
|
||||
0
|
||||
} else {
|
||||
2_u32.pow(winning.saturating_sub(1))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn part1(input: &str) -> Result<u32> {
|
||||
let mut sum = 0;
|
||||
for card in input.trim().split('\n').map(|card| card.parse::<Card>()) {
|
||||
let card = card?;
|
||||
sum += card.score();
|
||||
}
|
||||
Ok(sum)
|
||||
}
|
||||
|
||||
fn part2(input: &str) -> Result<u32> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
pub fn solve() -> Result<()> {
|
||||
println!("\nDay 04");
|
||||
instrument!(part1(INPUT)?, part2(INPUT)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const TEST_INPUT1: &str = include_str!("input/test1.txt");
|
||||
|
||||
#[test]
|
||||
fn test_part1() {
|
||||
assert_eq!(part1(TEST_INPUT1).unwrap(), 13);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part2() {
|
||||
assert_eq!(part2(TEST_INPUT1).unwrap(), 0);
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
pub mod day01;
|
||||
pub mod day02;
|
||||
pub mod day03;
|
||||
pub mod day04;
|
||||
pub mod instrument;
|
||||
|
||||
use anyhow::Result;
|
||||
@ -10,6 +11,7 @@ fn main() -> Result<()> {
|
||||
day01::solve()?;
|
||||
day02::solve()?;
|
||||
day03::solve()?;
|
||||
day04::solve()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user