use std::fs::read_to_string;
use std::collections::HashMap;

fn main() {
    // Read the input file
    let input = read_to_string("input_aoc_2.txt")
        .expect("Failed to read the input file.");

    part_1(input)
}

fn part_1(input :String) {
    // Make an interator over the lines
    let lines = input.lines();
    let hashmapofnumbers: HashMap<&str, u32> = HashMap::from([
            ("red", 12),
            ("green", 13),
            ("blue", 14),
        ]);
    let mut sum = 0;
    let mut secondsum = 0;
    'lineloop: for line in lines {
        let doubledotindex = line.find(':').expect("This is a shitty line");
        let splitgame = line.split_at(doubledotindex);
        let gameid = splitgame.0.split_at(5).1.to_string();
        //dbg!(&gameid);
        //let gameid = game.rsplitn(n, pat);

        let sets :Vec<&str> = splitgame.1.splitn(10, ';').collect();
        dbg!(&sets);
        
        // for set in sets.clone() {
        //     let subsets :Vec<&str> = set.splitn(10, ',').collect();
        //     dbg!(&subsets);
        //     for subset in subsets {
        //         let resultbool = colourfind(subset.to_string());
        //         println!("Result: {resultbool}");
        //         if !resultbool {
        //             continue 'lineloop;
        //         }
        //     }
        // }
        // sum += gameid.parse::<i32>().unwrap();


        let mut minimumred = 1;
        let mut minimumgreen = 1;
        let mut minimumblue = 1;

        // Counting minimums
        for set in sets {
            let subsets :Vec<&str> = set.splitn(10, ',').collect();
            dbg!(&subsets);
            for subset in subsets {
                let resultamount = colouramount(subset.to_string());
                println!("Result: {:#?}", resultamount);
                if resultamount.red > minimumred {
                    minimumred = resultamount.red
                }
                if resultamount.green > minimumgreen {
                    minimumgreen = resultamount.green
                }
                if resultamount.blue > minimumblue {
                    minimumblue = resultamount.blue
                }

            }
        }
        secondsum += (minimumred*minimumgreen*minimumblue);
        

        
    }
    println!("RESULTING SUM = {secondsum}");
}



fn colourfind(cube :String) -> bool {
   // println!("Cube: {}", cube);
    let redamount = match cube.find("red") {
        Some(val) => {
           // println!(" cube list {cube}");
            let result = cube.split_at(val);
           // println!(" result {}", result.0);
            let result = result.0.replace(":", " ");
            let finalnumber = result.trim().parse::<i32>().unwrap();
            if finalnumber > 12 {
                println!("FALSE RED: {finalnumber}");
                return false;
            }
            finalnumber
        },
        None => 0,
    };
   // println!("{redamount}");
    let greenamount = match cube.find("green") {
        Some(val) => {
           // println!(" cube list {cube}");
            let result = cube.split_at(val);
           // println!(" result {}", result.0);
            let result = result.0.replace(":", " ");
            let finalnumber = result.trim().parse::<i32>().unwrap();
            if finalnumber > 13 {
                println!("FALSE GREEN: {finalnumber}");
                return false;
            }
            finalnumber
        },
        None => 0,
    };
    println!("{greenamount}");
    let blueamount = match cube.find("blue") {
        Some(val) => {
            //println!(" cube list {cube}");
            let result = cube.split_at(val);
            //println!(" result {}", result.0);
            let result = result.0.replace(":", " ");
            let finalnumber = result.trim().parse::<i32>().unwrap();
            if finalnumber > 14 {
                println!("FALSE BLUE: {finalnumber}");
                return false;
            }
            finalnumber
        },
        None => 0,
    };
    //println!("{blueamount}");
    true
}

#[derive(Debug)]
struct COLOURS {
    red: i32,
    green: i32,
    blue: i32,
}


fn colouramount(cube :String) -> COLOURS {
    // println!("Cube: {}", cube);
     let redamount = match cube.find("red") {
         Some(val) => {
            // println!(" cube list {cube}");
             let result = cube.split_at(val);
            // println!(" result {}", result.0);
             let result = result.0.replace(":", " ");
             let finalnumber = result.trim().parse::<i32>().unwrap();

             finalnumber
         },
         None => 0,
     };
    // println!("{redamount}");
     let greenamount = match cube.find("green") {
         Some(val) => {
            // println!(" cube list {cube}");
             let result = cube.split_at(val);
            // println!(" result {}", result.0);
             let result = result.0.replace(":", " ");
             let finalnumber = result.trim().parse::<i32>().unwrap();

             finalnumber
         },
         None => 0,
     };
     //println!("{greenamount}");
     let blueamount = match cube.find("blue") {
         Some(val) => {
             //println!(" cube list {cube}");
             let result = cube.split_at(val);
             //println!(" result {}", result.0);
             let result = result.0.replace(":", " ");
             let finalnumber = result.trim().parse::<i32>().unwrap();

             finalnumber
         },
         None => 0,
     };
     //println!("{blueamount}");
    COLOURS { red: redamount, green: greenamount, blue: blueamount }
 }




 