Skip to content

Commit 930c30a

Browse files
committed
Day 07 parsing
1 parent ab9c91e commit 930c30a

File tree

4 files changed

+1109
-5
lines changed

4 files changed

+1109
-5
lines changed

aoc-solver/src/y2023/day05.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,6 @@ mod tests {
305305

306306
#[test]
307307
fn it_solves_part2_real_input() {
308-
assert_eq!(
309-
Day05.part_2(include_str!("../../../inputs/2023/day05.txt")),
310-
Ok(26829166)
311-
);
308+
assert_eq!(Day05.part_2(Day05.default_input()), Ok(26829166));
312309
}
313310
}

aoc-solver/src/y2023/day07.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
use std::collections::HashMap;
2+
3+
use crate::solution::{AocError, Solution};
4+
5+
pub struct Day07;
6+
7+
#[derive(Debug, Clone)]
8+
struct Card {}
9+
10+
type Hand = [u8; 5];
11+
12+
fn parse(input: &str) -> Result<Vec<(Hand, u32)>, AocError> {
13+
let card_scores: HashMap<char, u8> = [
14+
'A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2',
15+
]
16+
.into_iter()
17+
.rev()
18+
.enumerate()
19+
.map(|(score, card)| (card, score as u8))
20+
.collect();
21+
22+
let hands = input
23+
.trim()
24+
.lines()
25+
.map(|line| {
26+
let (hand_str, bid_str) = line
27+
.split_once(' ')
28+
.ok_or(AocError::parse(line, "Invalid line"))?;
29+
30+
let hand = parse_cards(hand_str, &card_scores)?;
31+
let bid = parse_number(bid_str)?;
32+
33+
Ok((hand, bid))
34+
})
35+
.collect::<Result<_, _>>()?;
36+
37+
Ok(hands)
38+
}
39+
40+
fn parse_cards(cards: &str, card_scores: &HashMap<char, u8>) -> Result<Hand, AocError> {
41+
cards
42+
.chars()
43+
.take(5)
44+
.map(|card| {
45+
card_scores
46+
.get(&card)
47+
.copied()
48+
.ok_or(AocError::parse(card, "Unknown card"))
49+
})
50+
.collect::<Result<Vec<_>, AocError>>()?
51+
.try_into()
52+
.map_err(|_| AocError::parse(cards, "Wrong number of cards"))
53+
}
54+
55+
fn parse_number(number: &str) -> Result<u32, AocError> {
56+
number
57+
.parse()
58+
.map_err(|_| AocError::parse(number, "Error parsing number"))
59+
}
60+
61+
fn score_hand(hand: Hand) -> u32 {
62+
todo!()
63+
}
64+
65+
impl Solution for Day07 {
66+
type F = u32;
67+
type S = u32;
68+
69+
fn default_input(&self) -> &'static str {
70+
include_str!("../../../inputs/2023/day07.txt")
71+
}
72+
73+
fn part_1(&self, input: &str) -> Result<u32, AocError> {
74+
let total_winnings = parse(input)?
75+
.into_iter()
76+
.map(|(hand, bid)| score_hand(hand) * bid)
77+
.sum();
78+
79+
Ok(total_winnings)
80+
}
81+
82+
fn part_2(&self, input: &str) -> Result<u32, AocError> {
83+
unimplemented!();
84+
}
85+
}
86+
87+
#[cfg(test)]
88+
mod tests {
89+
use super::*;
90+
91+
#[test]
92+
fn it_solves_part1_example() {
93+
assert_eq!(
94+
Day07.part_1(
95+
"32T3K 765\n\
96+
T55J5 684\n\
97+
KK677 28\n\
98+
KTJJT 220\n\
99+
QQQJA 483\n"
100+
),
101+
Ok(6440)
102+
);
103+
}
104+
}

aoc-solver/src/y2023/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ pub mod day03;
66
pub mod day04;
77
pub mod day05;
88
pub mod day06;
9+
pub mod day07;
910

10-
pub const MAX_DAYS: u8 = 6;
11+
pub const MAX_DAYS: u8 = 7;
1112

1213
pub struct Y2023;
1314

@@ -20,6 +21,7 @@ impl Solver for Y2023 {
2021
4 => day04::Day04.run(input, 4, 2023),
2122
5 => day05::Day05.run(input, 5, 2023),
2223
6 => day06::Day06.run(input, 6, 2023),
24+
7 => day06::Day06.run(input, 7, 2023),
2325
_ => vec![String::from("Solution not implemented (yet?)")],
2426
}
2527
}
@@ -43,6 +45,7 @@ impl Solver for Y2023 {
4345
4 => include_str!("./day04.rs"),
4446
5 => include_str!("./day05.rs"),
4547
6 => include_str!("./day06.rs"),
48+
7 => include_str!("./day07.rs"),
4649
_ => unimplemented!(),
4750
}
4851
}

0 commit comments

Comments
 (0)