Compare commits

..

No commits in common. "6a74138d9eae86d6d34448ecdf9df5515eeb59d7" and "c1264926053dec366cec88d7d32ca7cf4e07c541" have entirely different histories.

3 changed files with 1044 additions and 122 deletions

View file

@ -1,5 +1,7 @@
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green two1nine
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue eightwothree
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red abcone2threexyz
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red xtwone3four
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green 4nineeightseven2
zoneight234
7pqrstsixteen

1100
input.txt

File diff suppressed because it is too large Load diff

View file

@ -1,30 +1,50 @@
use anyhow::Result; use anyhow::Result;
use fancy_regex::Regex; use fancy_regex::Regex;
use std::{collections::HashMap, io::stdin}; use std::{
collections::HashMap,
fs::File,
io::{BufRead, BufReader},
};
fn main() -> Result<()> { fn main() -> Result<()> {
let game_re = Regex::new(r"Game (\d+): (.+)")?; let input = File::open("input.txt")?;
let cube_re = Regex::new(r"(\d+) (blue|red|green)")?; 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 mut sum = 0; let mut sum = 0;
for line in stdin().lines() { for line in reader.lines() {
let line = line?; let line = line?;
let c = game_re.captures(&line)?.unwrap(); let mut nums = vec![];
let desc = c.get(2).unwrap().as_str(); for m in patterns.find_iter(&line) {
let m = m?;
let text = m.as_str();
let mut max_colors = HashMap::from([("red", 0), ("green", 0), ("blue", 0)]); let v: &str = match num_values.get(text) {
for round in desc.split(';') { Some(digit) => digit,
for cube in cube_re.captures_iter(round) { None => text,
let cube = cube?; };
let count: i32 = cube.get(1).unwrap().as_str().parse()?;
let color = cube.get(2).unwrap().as_str();
let n = max_colors.get(&color).unwrap(); nums.push(v);
max_colors.insert(color, i32::max(*n, count));
}
} }
let power: i32 = max_colors.values().product(); let mut value = String::new();
sum += power; value.push_str(nums.first().unwrap());
value.push_str(nums.last().unwrap());
sum += value.parse::<i32>().unwrap();
} }
println!("{sum}"); println!("{sum}");