47 lines
1.3 KiB
Rust
Executable File
47 lines
1.3 KiB
Rust
Executable File
use std::fs::File;
|
|
use std::io::{self, prelude::*, BufReader};
|
|
use regex::Regex;
|
|
|
|
fn get_number(line: &str) -> u32 {
|
|
let re = Regex::new(r"\d+").unwrap();
|
|
let num: u32 = re.find(line).unwrap().as_str().parse().unwrap();
|
|
return num;
|
|
}
|
|
|
|
|
|
fn process_line(line: String) -> u32 {
|
|
let parts = line.split(&[':']).collect::<Vec<&str>>();
|
|
// Parts is a vector of strings? where the first index will be the game
|
|
// and all consecutive indeces will be the game "sets"
|
|
let possible_colors = ["red", "blue", "green"];
|
|
let mut color_max = [0, 0, 0];
|
|
let sets = parts[1].split(&[';']);
|
|
for set in sets {
|
|
let colors = set.split(", ");
|
|
for color in colors {
|
|
for idx in 0..3 {
|
|
if color.contains(possible_colors[idx]) {
|
|
if color_max[idx] < get_number(color) {
|
|
color_max[idx] = get_number(color);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return color_max[0] * color_max[1] * color_max[2];
|
|
}
|
|
|
|
fn main() -> io::Result<()> {
|
|
let mut sum: u32 = 0;
|
|
let file = File::open("../input.txt")?;
|
|
let reader = BufReader::new(file);
|
|
for line in reader.lines() {
|
|
sum = process_line(line?) + sum;
|
|
}
|
|
// Print text to the console.
|
|
println!("{}", sum);
|
|
|
|
Ok(())
|
|
}
|