implemented day 2 in rust
parent
e7c46bed7a
commit
15a765f505
|
|
@ -5,3 +5,50 @@ version = 3
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "advent_of_code"
|
name = "advent_of_code"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"regex",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aho-corasick"
|
||||||
|
version = "1.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memchr"
|
||||||
|
version = "2.6.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex"
|
||||||
|
version = "1.10.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"
|
||||||
|
dependencies = [
|
||||||
|
"aho-corasick",
|
||||||
|
"memchr",
|
||||||
|
"regex-automata",
|
||||||
|
"regex-syntax",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex-automata"
|
||||||
|
version = "0.4.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
|
||||||
|
dependencies = [
|
||||||
|
"aho-corasick",
|
||||||
|
"memchr",
|
||||||
|
"regex-syntax",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex-syntax"
|
||||||
|
version = "0.8.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
|
||||||
|
|
|
||||||
|
|
@ -6,3 +6,4 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
regex = "1.10.2"
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
#[derive(PartialEq, Eq)]
|
||||||
|
pub enum CubeColor {
|
||||||
|
BLUE,
|
||||||
|
RED,
|
||||||
|
GREEN,
|
||||||
|
}
|
||||||
|
pub struct CubeSet {
|
||||||
|
pub quantity: u32,
|
||||||
|
pub colour: CubeColor
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is currently unused as it was only for debugging.
|
||||||
|
impl fmt::Display for CubeSet {
|
||||||
|
fn fmt(&self, f:&mut fmt::Formatter) -> fmt::Result{
|
||||||
|
let cube_color = match self.colour{
|
||||||
|
CubeColor::GREEN => "green",
|
||||||
|
CubeColor::BLUE => "blue",
|
||||||
|
CubeColor::RED => "red"
|
||||||
|
};
|
||||||
|
write!(f, "Cube color: {}, quantity: {}", cube_color, self.quantity)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CubeSet {
|
||||||
|
// The constructor expects a string that looks like
|
||||||
|
// Number Color, f.ex 3 blue, or 20 red
|
||||||
|
pub fn new(input: &String)-> Self{
|
||||||
|
let tokens: Vec<&str> = input.split_whitespace().collect();
|
||||||
|
|
||||||
|
CubeSet {
|
||||||
|
quantity:match tokens[0].parse(){
|
||||||
|
Ok(digit) => digit,
|
||||||
|
_=> panic!("Found no number in the CubeSet Constructor")
|
||||||
|
},
|
||||||
|
colour:match tokens[1]{
|
||||||
|
"red" => CubeColor::RED,
|
||||||
|
"blue" => CubeColor::BLUE,
|
||||||
|
"green" => CubeColor::GREEN,
|
||||||
|
_ => panic!("Found no colour in the CubeSet Constructor")
|
||||||
|
} }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
use crate::day2::cube_set::CubeSet;
|
||||||
|
|
||||||
|
pub struct Game {
|
||||||
|
pub id: i32,
|
||||||
|
pub draws: Vec<CubeSet>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Game {
|
||||||
|
pub fn new(input: &str)-> Self{
|
||||||
|
let tokens = input.split(":").collect::<Vec<&str>>();
|
||||||
|
// The first token looks like: Game N, where N is the game number
|
||||||
|
let game_number_string = tokens[0].split_whitespace().collect::<Vec<&str>>()[1];
|
||||||
|
let parsed_game_id: i32 = match game_number_string.to_string().parse(){
|
||||||
|
Ok(digit) => digit,
|
||||||
|
Err(_)=> panic!("Found no number parsing Game Id")
|
||||||
|
};
|
||||||
|
let draws = Self::parse_draws(tokens[1]);
|
||||||
|
|
||||||
|
Game { id: parsed_game_id, draws }
|
||||||
|
}
|
||||||
|
fn parse_draws(input: &str)-> Vec::<CubeSet>{
|
||||||
|
let mut return_vec = Vec::<CubeSet>::new();
|
||||||
|
|
||||||
|
let comma_and_semicolon = regex::Regex::new(r",|;").unwrap();
|
||||||
|
for draw_string in comma_and_semicolon.split(input){
|
||||||
|
return_vec.push(CubeSet::new(&draw_string.to_string()));
|
||||||
|
};
|
||||||
|
|
||||||
|
return_vec
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,79 @@
|
||||||
|
use std::fs;
|
||||||
|
use cube_set::{CubeColor, CubeSet};
|
||||||
|
use game::Game;
|
||||||
|
|
||||||
|
mod cube_set;
|
||||||
|
mod game;
|
||||||
|
|
||||||
|
pub fn run_day2(){
|
||||||
|
|
||||||
|
println!("Pls implement me");
|
||||||
|
let contents = fs::read_to_string("input2.txt")
|
||||||
|
.expect("Could not find the file");
|
||||||
|
|
||||||
|
let possible_games_sum = part_one(&contents);
|
||||||
|
println!("The answer to the first part of day2 is: {possible_games_sum}");
|
||||||
|
let minimum_powers = part_two(&contents);
|
||||||
|
println!("The answer to the second part of day2 is: {minimum_powers}");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part_one(contents: &String)-> i32{
|
||||||
|
let game_rules = [
|
||||||
|
CubeSet{ quantity: 12, colour: CubeColor::RED},
|
||||||
|
CubeSet{quantity: 13, colour: CubeColor::GREEN},
|
||||||
|
CubeSet{quantity: 14, colour:CubeColor::BLUE}];
|
||||||
|
let mut possible_games_sum = 0;
|
||||||
|
|
||||||
|
for line in contents.lines() {
|
||||||
|
|
||||||
|
if !line.is_empty(){
|
||||||
|
let current_game = Game::new(line);
|
||||||
|
let mut possible_game = true;
|
||||||
|
|
||||||
|
for draw in current_game.draws{
|
||||||
|
let applicable_rule_set = game_rules.iter().find(|rule_set| rule_set.colour == draw.colour).unwrap();
|
||||||
|
|
||||||
|
if applicable_rule_set.quantity < draw.quantity{
|
||||||
|
possible_game = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if possible_game{
|
||||||
|
possible_games_sum += current_game.id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
possible_games_sum
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part_two(contents: &String)-> u32{
|
||||||
|
let mut games_power = 0;
|
||||||
|
|
||||||
|
for line in contents.lines(){
|
||||||
|
if !line.is_empty(){
|
||||||
|
let current_game = Game::new(line);
|
||||||
|
let mut minimum_viable_cubeset = [
|
||||||
|
CubeSet{ quantity: 0, colour: CubeColor::RED},
|
||||||
|
CubeSet{ quantity: 0, colour: CubeColor::GREEN},
|
||||||
|
CubeSet{ quantity: 0, colour: CubeColor::BLUE}];
|
||||||
|
|
||||||
|
for draw in current_game.draws{
|
||||||
|
let matching_color_set: &mut CubeSet = minimum_viable_cubeset.iter_mut().find(|color_set| color_set.colour == draw.colour).unwrap();
|
||||||
|
|
||||||
|
if matching_color_set.quantity < draw.quantity {
|
||||||
|
*matching_color_set = CubeSet{quantity: draw.quantity, colour: draw.colour};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut accumulator = 1;
|
||||||
|
|
||||||
|
for minimum_set in minimum_viable_cubeset.iter(){
|
||||||
|
accumulator *= minimum_set.quantity;
|
||||||
|
}
|
||||||
|
games_power += accumulator;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
games_power
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
|
|
||||||
mod day1;
|
mod day1;
|
||||||
|
mod day2;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
day1::run_day1()
|
day1::run_day1();
|
||||||
}
|
day2::run_day2();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue