use std::io::{Read, stdin};
use libc::{clock_gettime, timespec, CLOCK_PROCESS_CPUTIME_ID};

fn main() {
    let mut begin = timespec {
        tv_sec: 0,
        tv_nsec: 0,
    };
    unsafe {
        clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mut begin);
    };

    let mut sum = 0;
    let mut data = String::new();
    stdin().read_to_string(&mut data).unwrap();
    for line in data.lines() {
        let after_id = line.split(':').nth(1).unwrap().trim();
        let get_colors = after_id.replace(';', ",");
        let get_colors = get_colors.split(',');
        let mut red = vec![];
        let mut blue = vec![];
        let mut green = vec![];

        for get_color in get_colors {
            let color_num: String = get_color
                .chars()
                .filter(char::is_ascii_digit)
                .collect();
            let color_num: i32 = color_num.parse().unwrap();

            if get_color.contains("red") {
                red.push(color_num);
            }
            if get_color.contains("green") {
                green.push(color_num);
            }
            if get_color.contains("blue") {
                blue.push(color_num);
            }
        }
        let red_largest = red.iter().max();
        let blue_largest = blue.iter().max();
        let green_largest = green.iter().max();
        let temp = red_largest.unwrap() * blue_largest.unwrap() * green_largest.unwrap();
        let before_sum = sum;
        sum += temp;
        println!("{red_largest:?} * {blue_largest:?} * {green_largest:?} + {before_sum:?} = {sum:?}")
    }
    println!("{sum}");

    let mut end = timespec {
        tv_sec: 0,
        tv_nsec: 0,
    };
    unsafe {
        clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mut end);
    };
    println!(
        "time: {:.6}ms",
        (end.tv_nsec - begin.tv_nsec) as f32 / 1000.0 / 1000.0
    );
}
