finish part 1
This commit is contained in:
parent
3afeb2b681
commit
4a9fc590d2
2 changed files with 94 additions and 0 deletions
36
day02/part2Instructions.txt
Normal file
36
day02/part2Instructions.txt
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
--- Part Two ---
|
||||||
|
|
||||||
|
The Elf says they've stopped producing snow because they aren't getting any
|
||||||
|
water! He isn't sure why the water stopped; however, he can show you how to
|
||||||
|
get to the water source to check it out for yourself. It's just up ahead!
|
||||||
|
|
||||||
|
As you continue your walk, the Elf poses a second question: in each game you
|
||||||
|
played, what is the fewest number of cubes of each color that could have been
|
||||||
|
in the bag to make the game possible?
|
||||||
|
|
||||||
|
Again consider the example games from earlier:
|
||||||
|
|
||||||
|
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
|
||||||
|
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
|
||||||
|
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
|
||||||
|
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
|
||||||
|
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
|
||||||
|
|
||||||
|
• In game 1, the game could have been played with as few as 4 red, 2 green,
|
||||||
|
and 6 blue cubes. If any color had even one fewer cube, the game would
|
||||||
|
have been impossible.
|
||||||
|
• Game 2 could have been played with a minimum of 1 red, 3 green, and 4
|
||||||
|
blue cubes.
|
||||||
|
•
|
||||||
|
• Game 3 must have been played with at least 20 red, 13 green, and 6 blue
|
||||||
|
cubes.
|
||||||
|
• Game 4 required at least 14 red, 3 green, and 15 blue cubes.
|
||||||
|
• Game 5 needed no fewer than 6 red, 3 green, and 2 blue cubes in the bag.
|
||||||
|
|
||||||
|
The power of a set of cubes is equal to the numbers of red, green, and blue
|
||||||
|
cubes multiplied together. The power of the minimum set of cubes in game 1 is
|
||||||
|
48. In games 2-5 it was 12, 1560, 630, and 36, respectively. Adding up these
|
||||||
|
five powers produces the sum 2286.
|
||||||
|
|
||||||
|
For each game, find the minimum set of cubes that must have been present.
|
||||||
|
What is the sum of the power of these sets?
|
|
@ -1,11 +1,69 @@
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
|
const HEADER:&str = "Game";
|
||||||
|
const RED:&str = "red";
|
||||||
|
const RED_MAX:u8 = 12;
|
||||||
|
const GREEN:&str = "green";
|
||||||
|
const GREEN_MAX:u8 = 13;
|
||||||
|
const BLUE:&str = "blue";
|
||||||
|
const BLUE_MAX:u8 = 14;
|
||||||
|
const SPACE:char = ' ';
|
||||||
|
const COLOUR_SEPARATOR:char = ',';
|
||||||
|
const PULL_SEPARATOR:char = ';';
|
||||||
|
const HEADER_SEPARATOR:char = ':';
|
||||||
|
|
||||||
const FILE_NAME:&str = "./data/data.txt";
|
const FILE_NAME:&str = "./data/data.txt";
|
||||||
fn main() {
|
fn main() {
|
||||||
let file_contents = fs::read_to_string(FILE_NAME).unwrap();
|
let file_contents = fs::read_to_string(FILE_NAME).unwrap();
|
||||||
let lines:Vec<&str> = file_contents.split('\n').collect();
|
let lines:Vec<&str> = file_contents.split('\n').collect();
|
||||||
let mut final_output = 0;
|
let mut final_output = 0;
|
||||||
for line in lines{
|
for line in lines{
|
||||||
|
//Game #: # colour, # colour; # colour, # colour
|
||||||
|
if !line.contains(HEADER_SEPARATOR) { break; }
|
||||||
|
let (header,pulls) = line.split_once(HEADER_SEPARATOR).unwrap();
|
||||||
|
let (_,index) = header.trim().split_at(HEADER.len());
|
||||||
|
let pulls:Vec<&str> = pulls.split(PULL_SEPARATOR).collect();
|
||||||
|
if check_game(pulls){
|
||||||
|
final_output += index.trim().parse::<i64>().unwrap();
|
||||||
|
};
|
||||||
};
|
};
|
||||||
println!("{}",final_output);
|
println!("{}",final_output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn check_game(pulls:Vec<&str>) -> bool{
|
||||||
|
for pull in pulls{
|
||||||
|
if !check_pull(pull) { return false };
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_pull(pull:&str) -> bool{
|
||||||
|
let colours:Vec<&str> = pull.split(COLOUR_SEPARATOR).collect();
|
||||||
|
for colour in colours{
|
||||||
|
let (count,name) = colour.trim().split_once(SPACE).unwrap();
|
||||||
|
match is_safe(name.trim(),count.trim().parse::<u8>().unwrap()){
|
||||||
|
true => {
|
||||||
|
continue;
|
||||||
|
},
|
||||||
|
false => {
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_safe(colour:&str,value:u8) -> bool{
|
||||||
|
match colour{
|
||||||
|
RED => {
|
||||||
|
return value <= RED_MAX;
|
||||||
|
},
|
||||||
|
GREEN => {
|
||||||
|
return value <= GREEN_MAX;
|
||||||
|
},
|
||||||
|
BLUE => {
|
||||||
|
return value <= BLUE_MAX;
|
||||||
|
},
|
||||||
|
_ => { panic!("invalid input file!"); }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue