finished day 04 part 2

I know it's shit. Don't judge me
This commit is contained in:
Kristofers Solo
2023-12-04 19:51:39 +02:00
parent b4af5e8632
commit 0d2ce8d0c8
4 changed files with 299 additions and 39 deletions

View File

@@ -1,4 +1,4 @@
use color_eyre::Result;
use color_eyre::eyre::Result;
use day_04::part2::process;
fn main() -> Result<()> {

View File

@@ -1,21 +1,16 @@
use std::{borrow::BorrowMut, str::FromStr};
use color_eyre::eyre::Result;
use std::{collections::HashSet, str::FromStr};
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct Card {
id: usize,
win_nums: Vec<usize>,
my_nums: Vec<usize>,
win_nums: HashSet<usize>,
my_nums: HashSet<usize>,
points: usize,
}
impl Card {
fn add_point(&mut self) {
if self.points == 0 {
self.points = 1
} else {
self.points *= 2
}
fn score(&mut self) {
self.points = if self.points == 0 { 1 } else { self.points * 2 };
}
}
@@ -24,13 +19,7 @@ impl FromStr for Card {
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
let win_nums = line
.get(1)
.ok_or("Missing winning numbers field")
.and_then(|field| {
@@ -43,21 +32,20 @@ impl FromStr for Card {
.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()
});
let my_nums = 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,
@@ -65,7 +53,7 @@ impl FromStr for Card {
}
}
pub fn process(input: &str) -> color_eyre::Result<usize> {
pub fn process(input: &str) -> Result<usize> {
let mut cards: Vec<Card> = input.lines().flat_map(Card::from_str).collect();
let sum = cards
@@ -73,7 +61,7 @@ pub fn process(input: &str) -> color_eyre::Result<usize> {
.flat_map(|card| {
for num in card.win_nums.clone() {
if card.my_nums.contains(&num) {
card.add_point();
card.score();
}
}
Some(card.points)
@@ -88,7 +76,7 @@ mod tests {
use super::*;
#[test]
fn test_process() -> color_eyre::Result<()> {
fn test_process() -> 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

View File

@@ -1,7 +1,69 @@
use color_eyre::Result;
use std::{collections::HashSet, str::FromStr};
pub fn process(_input: &str) -> Result<u32> {
Ok(0)
#[derive(Debug, Clone)]
struct Card {
win_nums: HashSet<usize>,
my_nums: HashSet<usize>,
}
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 win_nums = 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 = 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 {
win_nums: win_nums?,
my_nums: my_nums?,
})
}
}
pub fn process(input: &str) -> Result<usize> {
let cards: Vec<_> = input.lines().flat_map(Card::from_str).collect();
let mut instances = vec![1; cards.len()];
cards.iter().enumerate().for_each(|(index, card)| {
let amount = instances[index];
(1..=amount).for_each(|_| {
let mut found = index;
card.win_nums.iter().for_each(|num| {
if card.my_nums.contains(num) {
found += 1;
instances.get_mut(found).map(|val| *val += 1);
}
});
});
});
let sum = instances.iter().sum();
Ok(sum)
}
#[cfg(test)]
@@ -10,8 +72,13 @@ mod tests {
#[test]
fn test_process() -> Result<()> {
let input = "";
assert_eq!(0, process(input)?);
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!(30, process(input)?);
Ok(())
}
}