finished day 04 part 1

This commit is contained in:
Kristofers Solo
2023-12-04 17:17:36 +02:00
parent 59e09a0eb7
commit b4af5e8632
9 changed files with 359 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
use color_eyre::Result;
use day_04::part1::process;
fn main() -> Result<()> {
let file = include_str!("../../input1.txt");
let result = process(file)?;
println!("{}", result);
Ok(())
}

View File

@@ -0,0 +1,9 @@
use color_eyre::Result;
use day_04::part2::process;
fn main() -> Result<()> {
let file = include_str!("../../input2.txt");
let result = process(file)?;
println!("{}", result);
Ok(())
}

2
2023/day-04/src/lib.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod part1;
pub mod part2;

101
2023/day-04/src/part1.rs Normal file
View File

@@ -0,0 +1,101 @@
use std::{borrow::BorrowMut, str::FromStr};
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct Card {
id: usize,
win_nums: Vec<usize>,
my_nums: Vec<usize>,
points: usize,
}
impl Card {
fn add_point(&mut self) {
if self.points == 0 {
self.points = 1
} else {
self.points *= 2
}
}
}
impl FromStr for Card {
type Err = &'static str;
fn from_str(s: &str) -> std::prelude::v1::Result<Self, Self::Err> {
let line: Vec<_> = s.split(|ch| ch == ':' || ch == '|').collect();
let id = line
.get(0)
.ok_or("Missing ID field")
.and_then(|field| field.split_whitespace().last().ok_or("Invalid ID format"))
.and_then(|id_str| id_str.parse::<usize>().map_err(|_| "Failed to parse ID"))?;
let win_nums: Result<Vec<_>, _> = line
.get(1)
.ok_or("Missing winning numbers field")
.and_then(|field| {
field
.split_whitespace()
.map(|num| {
num.parse::<usize>()
.map_err(|_| "Winning numbers should be numbers")
})
.collect()
});
let my_nums: Result<Vec<_>, _> =
line.get(2)
.ok_or("Missing 'my' numbers field")
.and_then(|field| {
field
.split_whitespace()
.map(|num| {
num.parse::<usize>()
.map_err(|_| "'My' numbers should be numbers")
})
.collect()
});
Ok(Self {
id,
win_nums: win_nums?,
my_nums: my_nums?,
points: 0,
})
}
}
pub fn process(input: &str) -> color_eyre::Result<usize> {
let mut cards: Vec<Card> = input.lines().flat_map(Card::from_str).collect();
let sum = cards
.iter_mut()
.flat_map(|card| {
for num in card.win_nums.clone() {
if card.my_nums.contains(&num) {
card.add_point();
}
}
Some(card.points)
})
.sum();
Ok(sum)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_process() -> color_eyre::Result<()> {
let input = "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";
assert_eq!(13, process(input)?);
Ok(())
}
}

17
2023/day-04/src/part2.rs Normal file
View File

@@ -0,0 +1,17 @@
use color_eyre::Result;
pub fn process(_input: &str) -> Result<u32> {
Ok(0)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_process() -> Result<()> {
let input = "";
assert_eq!(0, process(input)?);
Ok(())
}
}