day 2 part 1
This commit is contained in:
parent
c126492605
commit
dcb65c8da2
3 changed files with 130 additions and 1043 deletions
12
example.txt
12
example.txt
|
@ -1,7 +1,5 @@
|
|||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
||||
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
|
||||
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
|
||||
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
|
||||
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
|
||||
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
|
||||
|
|
61
src/main.rs
61
src/main.rs
|
@ -1,50 +1,39 @@
|
|||
use anyhow::Result;
|
||||
use fancy_regex::Regex;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fs::File,
|
||||
io::{BufRead, BufReader},
|
||||
};
|
||||
use std::{collections::HashMap, io::stdin};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let input = File::open("input.txt")?;
|
||||
let reader = BufReader::new(input);
|
||||
// let input = File::open("input.txt")?;
|
||||
// let reader = BufReader::new(input);
|
||||
|
||||
let patterns = Regex::new(
|
||||
r#"on(?=e)|tw(?=o)|thre(?=e)|four|fiv(?=e)|six|seve(?=n)|eigh(?=t)|nin(?=e)|1|2|3|4|5|6|7|8|9"#,
|
||||
)?;
|
||||
let num_values = HashMap::from([
|
||||
("on", "1"),
|
||||
("tw", "2"),
|
||||
("thre", "3"),
|
||||
("four", "4"),
|
||||
("fiv", "5"),
|
||||
("six", "6"),
|
||||
("seve", "7"),
|
||||
("eigh", "8"),
|
||||
("nin", "9"),
|
||||
]);
|
||||
let game_re = Regex::new(r"Game (\d+): (.+)")?;
|
||||
let cube_re = Regex::new(r"(\d+) (blue|red|green)")?;
|
||||
let max_colors = HashMap::from([("red", 12), ("green", 13), ("blue", 14)]);
|
||||
|
||||
let mut sum = 0;
|
||||
for line in reader.lines() {
|
||||
for line in stdin().lines() {
|
||||
let line = line?;
|
||||
let mut nums = vec![];
|
||||
for m in patterns.find_iter(&line) {
|
||||
let m = m?;
|
||||
let text = m.as_str();
|
||||
let c = game_re.captures(&line)?.unwrap();
|
||||
let game_number: i32 = c.get(1).unwrap().as_str().parse()?;
|
||||
let desc = c.get(2).unwrap().as_str();
|
||||
println!("game={game_number}");
|
||||
|
||||
let v: &str = match num_values.get(text) {
|
||||
Some(digit) => digit,
|
||||
None => text,
|
||||
};
|
||||
let mut valid = true;
|
||||
for round in desc.split(';') {
|
||||
for cube in cube_re.captures_iter(round) {
|
||||
let cube = cube?;
|
||||
let count: i32 = cube.get(1).unwrap().as_str().parse()?;
|
||||
let color = cube.get(2).unwrap().as_str();
|
||||
println!("count={count} color={color}");
|
||||
|
||||
nums.push(v);
|
||||
if count > *max_colors.get(&color).unwrap() {
|
||||
valid = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if valid {
|
||||
sum += game_number;
|
||||
}
|
||||
let mut value = String::new();
|
||||
value.push_str(nums.first().unwrap());
|
||||
value.push_str(nums.last().unwrap());
|
||||
|
||||
sum += value.parse::<i32>().unwrap();
|
||||
}
|
||||
|
||||
println!("{sum}");
|
||||
|
|
Loading…
Reference in a new issue