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
|
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
|
||||||
eightwothree
|
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
|
||||||
abcone2threexyz
|
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
|
||||||
xtwone3four
|
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
|
||||||
4nineeightseven2
|
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
|
||||||
zoneight234
|
|
||||||
7pqrstsixteen
|
|
||||||
|
|
61
src/main.rs
61
src/main.rs
|
@ -1,50 +1,39 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use fancy_regex::Regex;
|
use fancy_regex::Regex;
|
||||||
use std::{
|
use std::{collections::HashMap, io::stdin};
|
||||||
collections::HashMap,
|
|
||||||
fs::File,
|
|
||||||
io::{BufRead, BufReader},
|
|
||||||
};
|
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let input = File::open("input.txt")?;
|
// let input = File::open("input.txt")?;
|
||||||
let reader = BufReader::new(input);
|
// let reader = BufReader::new(input);
|
||||||
|
|
||||||
let patterns = Regex::new(
|
let game_re = Regex::new(r"Game (\d+): (.+)")?;
|
||||||
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 cube_re = Regex::new(r"(\d+) (blue|red|green)")?;
|
||||||
)?;
|
let max_colors = HashMap::from([("red", 12), ("green", 13), ("blue", 14)]);
|
||||||
let num_values = HashMap::from([
|
|
||||||
("on", "1"),
|
|
||||||
("tw", "2"),
|
|
||||||
("thre", "3"),
|
|
||||||
("four", "4"),
|
|
||||||
("fiv", "5"),
|
|
||||||
("six", "6"),
|
|
||||||
("seve", "7"),
|
|
||||||
("eigh", "8"),
|
|
||||||
("nin", "9"),
|
|
||||||
]);
|
|
||||||
|
|
||||||
let mut sum = 0;
|
let mut sum = 0;
|
||||||
for line in reader.lines() {
|
for line in stdin().lines() {
|
||||||
let line = line?;
|
let line = line?;
|
||||||
let mut nums = vec![];
|
let c = game_re.captures(&line)?.unwrap();
|
||||||
for m in patterns.find_iter(&line) {
|
let game_number: i32 = c.get(1).unwrap().as_str().parse()?;
|
||||||
let m = m?;
|
let desc = c.get(2).unwrap().as_str();
|
||||||
let text = m.as_str();
|
println!("game={game_number}");
|
||||||
|
|
||||||
let v: &str = match num_values.get(text) {
|
let mut valid = true;
|
||||||
Some(digit) => digit,
|
for round in desc.split(';') {
|
||||||
None => text,
|
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}");
|
println!("{sum}");
|
||||||
|
|
Loading…
Reference in a new issue