Fix day2/part1 (finally), includes alternate method

This commit is contained in:
Evie Litherland-Smith 2024-01-01 10:53:39 +00:00
parent 2706887b7c
commit 50478acd7f
4 changed files with 99 additions and 62 deletions

View file

@ -1,6 +1,8 @@
mod part1; mod part1;
mod part1alt;
fn main() { fn main() {
println!("Part1: {}", part1::main(include_str!("input.txt"))); println!("Part1: {}", part1::main(include_str!("input.txt")));
println!("Part1alt: {}", part1alt::main(include_str!("input.txt")));
println!("Part2: TODO"); println!("Part2: TODO");
} }

View file

@ -88,7 +88,7 @@ impl Contents {
} }
} }
fn valid(self: &Contents, hand: &Contents) -> bool { fn valid(self: &Contents, hand: &Contents) -> bool {
self.red.gt(&hand.red) & self.blue.gt(&hand.blue) & self.green.gt(&hand.green) self.red.ge(&hand.red) & self.blue.ge(&hand.blue) & self.green.ge(&hand.green)
} }
fn get_value(slice: &str) -> i32 { fn get_value(slice: &str) -> i32 {
Regex::new(r"(\d)+") Regex::new(r"(\d)+")

View file

@ -1,61 +0,0 @@
use std::fs;
use regex::Regex;
pub(crate) fn main() {
let input = String::from("inputs/2023/day2_1.txt");
println!(
"{:?}",
Game::from(fs::read_to_string(input).unwrap().lines().next())
)
}
#[derive(Debug)]
struct Game {
id: u32,
draws: Vec<Draw>,
}
impl TryFrom<&str> for Game {
type Error = String;
fn try_from(value: &str) -> Result<Self, Self::Error> {
let re = Regex::new(r"^Game [\d]+: ([\d]+ (red|blue|green){1}(;)?)+$").unwrap();
println!("{:?}", re.captures(value).unwrap());
Ok(Self {
id: 1,
draws: vec![Draw {
red: 1,
green: 1,
blue: 1,
}],
})
}
}
#[derive(Debug)]
struct Draw {
red: i32,
green: i32,
blue: i32,
}
impl TryFrom<&str> for Draw {
type Error = String;
fn try_from(value: &str) -> Result<Self, Self::Error> {
todo!()
}
}
impl Draw {
fn is_valid(&self, bag: &Bag) -> bool {
self.red < bag.red && self.green < bag.green && self.blue < bag.blue
}
}
struct Bag {
red: i32,
green: i32,
blue: i32,
}

96
day2/src/part1alt.rs Normal file
View file

@ -0,0 +1,96 @@
use regex::Regex;
// type Round<'a> = &'a str;
// type Game<'a> = Vec<Round>;
#[derive(Debug)]
struct Bag {
red: usize,
green: usize,
blue: usize,
}
#[derive(Debug)]
struct Game {
id: usize,
rounds: Vec<Round>,
}
impl Game {
fn new(input: &str) -> Self {
let (id, games) = input
.split_once(':')
.expect("All input lines follow this format");
Self {
id: id.split(' ').last().unwrap().parse().unwrap(),
rounds: games.split(';').map(Round::new).collect(),
}
}
fn is_valid(&self, bag: &Bag) -> bool {
self.rounds.iter().all(|m| m.is_valid(bag))
}
}
#[derive(Debug)]
struct Round {
red: usize,
green: usize,
blue: usize,
}
impl Round {
fn new(input: &str) -> Self {
fn get_colour(rstring: &str, input: &str) -> usize {
match Regex::new(rstring).unwrap().find(input) {
Some(val) => val.as_str().split(' ').next().unwrap().parse().unwrap(),
None => 0,
}
}
Self {
red: get_colour(r"\d+ (red)", input),
green: get_colour(r"\d+ (green)", input),
blue: get_colour(r"\d+ (blue)", input),
}
}
fn is_valid(&self, bag: &Bag) -> bool {
self.red <= bag.red && self.green <= bag.green && self.blue <= bag.blue
}
}
pub fn main(input: &str) -> usize {
let bag = Bag {
red: 12,
green: 13,
blue: 14,
};
let games: Vec<Game> = input.lines().map(Game::new).collect();
// dbg!(&games);
// dbg!(&games.iter().map(|m| m.is_valid(&bag)));
games
.iter()
.filter_map(|m| if m.is_valid(&bag) { Some(m.id) } else { None })
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn example() {
let input = "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";
assert_eq!(main(input), 8);
}
#[test]
fn solution() {
assert_eq!(main(include_str!("input.txt")), 2476)
}
}