Skip to content

Commit

Permalink
Day 07 parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Cadiac committed Dec 7, 2023
1 parent ab9c91e commit 930c30a
Show file tree
Hide file tree
Showing 4 changed files with 1,109 additions and 5 deletions.
5 changes: 1 addition & 4 deletions aoc-solver/src/y2023/day05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,6 @@ mod tests {

#[test]
fn it_solves_part2_real_input() {
assert_eq!(
Day05.part_2(include_str!("../../../inputs/2023/day05.txt")),
Ok(26829166)
);
assert_eq!(Day05.part_2(Day05.default_input()), Ok(26829166));
}
}
104 changes: 104 additions & 0 deletions aoc-solver/src/y2023/day07.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use std::collections::HashMap;

use crate::solution::{AocError, Solution};

pub struct Day07;

#[derive(Debug, Clone)]
struct Card {}

type Hand = [u8; 5];

fn parse(input: &str) -> Result<Vec<(Hand, u32)>, AocError> {
let card_scores: HashMap<char, u8> = [
'A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2',
]
.into_iter()
.rev()
.enumerate()
.map(|(score, card)| (card, score as u8))
.collect();

let hands = input
.trim()
.lines()
.map(|line| {
let (hand_str, bid_str) = line
.split_once(' ')
.ok_or(AocError::parse(line, "Invalid line"))?;

let hand = parse_cards(hand_str, &card_scores)?;
let bid = parse_number(bid_str)?;

Ok((hand, bid))
})
.collect::<Result<_, _>>()?;

Ok(hands)
}

fn parse_cards(cards: &str, card_scores: &HashMap<char, u8>) -> Result<Hand, AocError> {
cards
.chars()
.take(5)
.map(|card| {
card_scores
.get(&card)
.copied()
.ok_or(AocError::parse(card, "Unknown card"))
})
.collect::<Result<Vec<_>, AocError>>()?
.try_into()
.map_err(|_| AocError::parse(cards, "Wrong number of cards"))
}

fn parse_number(number: &str) -> Result<u32, AocError> {
number
.parse()
.map_err(|_| AocError::parse(number, "Error parsing number"))
}

fn score_hand(hand: Hand) -> u32 {
todo!()
}

impl Solution for Day07 {
type F = u32;
type S = u32;

fn default_input(&self) -> &'static str {
include_str!("../../../inputs/2023/day07.txt")
}

fn part_1(&self, input: &str) -> Result<u32, AocError> {
let total_winnings = parse(input)?
.into_iter()
.map(|(hand, bid)| score_hand(hand) * bid)
.sum();

Ok(total_winnings)
}

fn part_2(&self, input: &str) -> Result<u32, AocError> {
unimplemented!();
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_solves_part1_example() {
assert_eq!(
Day07.part_1(
"32T3K 765\n\
T55J5 684\n\
KK677 28\n\
KTJJT 220\n\
QQQJA 483\n"
),
Ok(6440)
);
}
}
5 changes: 4 additions & 1 deletion aoc-solver/src/y2023/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ pub mod day03;
pub mod day04;
pub mod day05;
pub mod day06;
pub mod day07;

pub const MAX_DAYS: u8 = 6;
pub const MAX_DAYS: u8 = 7;

pub struct Y2023;

Expand All @@ -20,6 +21,7 @@ impl Solver for Y2023 {
4 => day04::Day04.run(input, 4, 2023),
5 => day05::Day05.run(input, 5, 2023),
6 => day06::Day06.run(input, 6, 2023),
7 => day06::Day06.run(input, 7, 2023),
_ => vec![String::from("Solution not implemented (yet?)")],
}
}
Expand All @@ -43,6 +45,7 @@ impl Solver for Y2023 {
4 => include_str!("./day04.rs"),
5 => include_str!("./day05.rs"),
6 => include_str!("./day06.rs"),
7 => include_str!("./day07.rs"),
_ => unimplemented!(),
}
}
Expand Down
Loading

0 comments on commit 930c30a

Please sign in to comment.