61 lines
1.5 KiB
Rust
61 lines
1.5 KiB
Rust
use std::collections::HashMap;
|
|
use std::iter::FromIterator;
|
|
|
|
const FILE_STR: &'static str = include_str!("input.txt");
|
|
|
|
#[derive(Default, Debug)]
|
|
struct ScratchCard {
|
|
winning: Vec<usize>,
|
|
chosen: Vec<usize>,
|
|
}
|
|
|
|
impl ScratchCard {
|
|
fn new(winning: Vec<usize>, chosen: Vec<usize>) -> Self {
|
|
ScratchCard { winning, chosen }
|
|
}
|
|
}
|
|
|
|
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 {
|
|
HashMap::from_iter(
|
|
table
|
|
.split('\n')
|
|
.filter(|s| s.len() > 0)
|
|
.map(parse_game_table_line),
|
|
)
|
|
}
|
|
|
|
fn part_one() {
|
|
let map = parse_game_table(FILE_STR);
|
|
println!("{:#?}", map);
|
|
}
|
|
|
|
fn main() {
|
|
part_one();
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
const EXAMPLE_FILE_STR: &'static str = include_str!("example_input.txt");
|
|
}
|