Parse the puzzle input for 2023 day 4
This commit is contained in:
parent
d636cbf5f9
commit
3a87c13750
@ -1,8 +1,9 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::iter::FromIterator;
|
||||||
|
|
||||||
const FILE_STR: &'static str = include_str!("input.txt");
|
const FILE_STR: &'static str = include_str!("input.txt");
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default, Debug)]
|
||||||
struct ScratchCard {
|
struct ScratchCard {
|
||||||
winning: Vec<usize>,
|
winning: Vec<usize>,
|
||||||
chosen: Vec<usize>,
|
chosen: Vec<usize>,
|
||||||
@ -16,18 +17,36 @@ impl ScratchCard {
|
|||||||
|
|
||||||
type GameMap = HashMap<usize, ScratchCard>;
|
type GameMap = HashMap<usize, ScratchCard>;
|
||||||
|
|
||||||
|
/// Parse numbers from a line, separated/surrounded by whitespace
|
||||||
|
fn parse_num_str(s: &str) -> Vec<usize> {
|
||||||
|
s.split_ascii_whitespace()
|
||||||
|
.filter(|s| s.len() > 0)
|
||||||
|
.map(|s| s.trim())
|
||||||
|
.map(|s| usize::from_str_radix(s, 10).unwrap())
|
||||||
|
.collect::<Vec<usize>>()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_game_table_line(line: &str) -> (usize, ScratchCard) {
|
||||||
|
let parts: Vec<&str> = line.split(": ").collect();
|
||||||
|
let card_num_parts = parts[0].replace("Card", "");
|
||||||
|
let card_num = parse_num_str(&card_num_parts)[0];
|
||||||
|
let nums: Vec<Vec<usize>> = parts[1].split(" | ").map(parse_num_str).collect();
|
||||||
|
|
||||||
|
(card_num, ScratchCard::new(nums[0].clone(), nums[1].clone()))
|
||||||
|
}
|
||||||
|
|
||||||
fn parse_game_table(table: &str) -> GameMap {
|
fn parse_game_table(table: &str) -> GameMap {
|
||||||
let mut map = HashMap::new();
|
HashMap::from_iter(
|
||||||
|
table
|
||||||
table.split('\n').for_each(|line| {
|
.split('\n')
|
||||||
todo!();
|
.filter(|s| s.len() > 0)
|
||||||
});
|
.map(parse_game_table_line),
|
||||||
|
)
|
||||||
map
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part_one() {
|
fn part_one() {
|
||||||
todo!();
|
let map = parse_game_table(FILE_STR);
|
||||||
|
println!("{:#?}", map);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
Loading…
Reference in New Issue
Block a user