Compare commits

...

2 commits

Author SHA1 Message Date
6a74138d9e day 2 part 2 2023-12-02 00:54:39 -05:00
dcb65c8da2 day 2 part 1 2023-12-02 00:43:42 -05:00
3 changed files with 122 additions and 1044 deletions

View file

@ -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

1100
input.txt

File diff suppressed because it is too large Load diff

View file

@ -1,50 +1,30 @@
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 game_re = Regex::new(r"Game (\d+): (.+)")?;
let reader = BufReader::new(input); let cube_re = Regex::new(r"(\d+) (blue|red|green)")?;
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 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 desc = c.get(2).unwrap().as_str();
let m = m?;
let text = m.as_str();
let v: &str = match num_values.get(text) { let mut max_colors = HashMap::from([("red", 0), ("green", 0), ("blue", 0)]);
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();
nums.push(v); let n = max_colors.get(&color).unwrap();
max_colors.insert(color, i32::max(*n, count));
}
} }
let mut value = String::new(); let power: i32 = max_colors.values().product();
value.push_str(nums.first().unwrap()); sum += power;
value.push_str(nums.last().unwrap());
sum += value.parse::<i32>().unwrap();
} }
println!("{sum}"); println!("{sum}");