mirror of
https://gitlab.com/MisterBiggs/aoc-2023-rust.git
synced 2025-07-23 22:51:32 +00:00
part 1
This commit is contained in:
@@ -62,11 +62,6 @@ fn part2(calibration_input: &str) -> usize {
|
||||
index_and_digit[0].1 * 11
|
||||
} else {
|
||||
index_and_digit.sort_by(|a, b| a.0.cmp(&b.0));
|
||||
println!(
|
||||
"{} {}",
|
||||
&line,
|
||||
index_and_digit[0].1 * 10 + index_and_digit.last().unwrap().1
|
||||
);
|
||||
|
||||
index_and_digit[0].1 * 10 + index_and_digit.last().unwrap().1
|
||||
}
|
||||
|
98
src/day2.rs
Normal file
98
src/day2.rs
Normal file
@@ -0,0 +1,98 @@
|
||||
// use itertools::Itertools;
|
||||
use std::fs;
|
||||
|
||||
pub fn run() {
|
||||
println!("Day 1:");
|
||||
let input = fs::read_to_string("./inputs/day2.txt").expect("Could not read file");
|
||||
|
||||
println!("\tPart 1: {}", part1(&input));
|
||||
// println!("\tPart 2: {}", part2(&input));
|
||||
}
|
||||
|
||||
struct Game {
|
||||
red: usize,
|
||||
green: usize,
|
||||
blue: usize,
|
||||
}
|
||||
|
||||
impl Game {
|
||||
fn contains(&self, other: Game) -> bool {
|
||||
other.red <= self.red && other.green <= self.green && other.blue <= self.blue
|
||||
}
|
||||
}
|
||||
|
||||
fn part1(games: &str) -> usize {
|
||||
let mut valid_games: Vec<usize> = vec![];
|
||||
|
||||
let main_game = Game {
|
||||
red: 12,
|
||||
green: 13,
|
||||
blue: 14,
|
||||
};
|
||||
|
||||
for game in games.lines() {
|
||||
let (game_part, rounds) = game.split_once(':').unwrap();
|
||||
let game_number = game_part
|
||||
.split_once(' ')
|
||||
.unwrap()
|
||||
.1
|
||||
.parse::<usize>()
|
||||
.unwrap();
|
||||
|
||||
let mut valid_round: Vec<bool> = vec![];
|
||||
for round in rounds.split(';') {
|
||||
let mut round_counts = Game {
|
||||
red: 0,
|
||||
green: 0,
|
||||
blue: 0,
|
||||
};
|
||||
let dice = round.split(',');
|
||||
for die in dice {
|
||||
let (amount, color) = die.trim_start().split_once(' ').unwrap();
|
||||
|
||||
match color {
|
||||
"red" => round_counts.red = amount.parse().unwrap(),
|
||||
"green" => round_counts.green = amount.parse().unwrap(),
|
||||
"blue" => round_counts.blue = amount.parse().unwrap(),
|
||||
_ => panic!("No color match found"),
|
||||
}
|
||||
}
|
||||
valid_round.push(main_game.contains(round_counts));
|
||||
}
|
||||
if valid_round.iter().all(|x| x.to_owned()) {
|
||||
valid_games.push(game_number);
|
||||
}
|
||||
}
|
||||
valid_games.into_iter().sum()
|
||||
}
|
||||
|
||||
// fn part2(calibration_input: &str) -> usize {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_1() {
|
||||
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!(part1(input), 8);
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn test_2() {
|
||||
// let input = "two1nine
|
||||
// eightwothree
|
||||
// abcone2threexyz
|
||||
// xtwone3four
|
||||
// 4nineeightseven2
|
||||
// zoneight234
|
||||
// 7pqrstsixteen";
|
||||
|
||||
// assert_eq!(part2(input), 281);
|
||||
// }
|
||||
}
|
@@ -2,8 +2,10 @@
|
||||
extern crate scan_fmt;
|
||||
|
||||
mod day1;
|
||||
mod day2;
|
||||
|
||||
fn main() {
|
||||
println!("Running Advent of Code 2023");
|
||||
day1::run();
|
||||
day2::run();
|
||||
}
|
||||
|
Reference in New Issue
Block a user