2023(2): WIP add regex for capturing sets of a game
This commit is contained in:
44
2023/day/2/main.rs
Normal file
44
2023/day/2/main.rs
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
use std::fs::File;
|
||||||
|
use std::io::{self, BufRead};
|
||||||
|
use std::path::Path;
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
if let Ok(lines) = read_lines("./input") {
|
||||||
|
for line in lines {
|
||||||
|
if let Ok(text) = line {
|
||||||
|
Game g = parse_game(text);
|
||||||
|
} else {
|
||||||
|
println!("Error on line \"{:?}\"", line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Bag {
|
||||||
|
red: u32,
|
||||||
|
green: u32,
|
||||||
|
blue: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Set {
|
||||||
|
red: u32,
|
||||||
|
green: u32,
|
||||||
|
blue: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Game {
|
||||||
|
ID: u32,
|
||||||
|
Vec<Set>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_game(text: &String) -> Result<Game> {
|
||||||
|
let re = Regex::new(r"^Game ([0-9]+)(?:(?:(?::|;) )((?:[0-9]+ (?:red|blue|green)(?:, )?)+))+$").unwrap();
|
||||||
|
// TODO: Extract all captures. The second capture group only returns the last of the match.
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||||
|
where P: AsRef<Path>, {
|
||||||
|
let file = File::open(filename)?;
|
||||||
|
Ok(io::BufReader::new(file).lines())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user