Skip to content

Commit

Permalink
2023: Cleanup day 2
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Dec 2, 2023
1 parent 47a584f commit ae5e3db
Showing 1 changed file with 49 additions and 77 deletions.
126 changes: 49 additions & 77 deletions aoc_2023/src/day_02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,95 +11,63 @@ impl Solution for Day02 {
}

fn part_a(&self, input: &str) -> Answer {
let runs = input
.lines()
.map(|line| {
let (game, cubes) = line.split_once(':').unwrap();
let game = game.trim_start_matches("Game ").parse::<u32>().unwrap();
let sets = cubes.split(';').collect::<Vec<_>>();

let mut cube_sets = Vec::new();
for part in sets {
let iter = part.trim().split(',');

let mut cubes = CubeSet::default();
for i in iter {
let mut iter = i.trim().split_whitespace();
let count = iter.next().unwrap().parse::<u32>().unwrap();
let color = iter.next().unwrap();

match color {
"red" => cubes.red += count,
"green" => cubes.green += count,
"blue" => cubes.blue += count,
_ => unreachable!(),
}
}
cube_sets.push(cubes);
}

(game, cube_sets)
})
.filter(|(_, games)| {
for game in games {
if game.red <= MAX_CUBES[0]
&& game.green <= MAX_CUBES[1]
&& game.blue <= MAX_CUBES[2]
{
continue;
} else {
return false;
}
}
true
})
.map(|x| x.0)
.sum::<u32>();
runs.into()
parse(input)
.iter()
.enumerate()
.filter(|(_, games)| games.iter().all(|game| game.is_possible()))
.map(|x| x.0 + 1)
.sum::<usize>()
.into()
}

fn part_b(&self, input: &str) -> Answer {
let runs = input
.lines()
.map(|line| {
let (game, cubes) = line.split_once(':').unwrap();
let game = game.trim_start_matches("Game ").parse::<u32>().unwrap();
let sets = cubes.split(';').collect::<Vec<_>>();

let mut cube_sets = Vec::new();
for part in sets {
let iter = part.trim().split(',');

let mut cubes = CubeSet::default();
for i in iter {
let mut iter = i.trim().split_whitespace();
let count = iter.next().unwrap().parse::<u32>().unwrap();
let color = iter.next().unwrap();

match color {
"red" => cubes.red += count,
"green" => cubes.green += count,
"blue" => cubes.blue += count,
_ => unreachable!(),
}
}
cube_sets.push(cubes);
}

(game, cube_sets)
})
.map(|(num, games)| {
parse(input)
.iter()
.map(|games| {
let mut max = CubeSet::default();
for game in games {
max = max.max(&game);
}
max.red * max.green * max.blue
})
.sum::<u32>();
runs.into()
.sum::<u32>()
.into()
}
}

// am very tired, todo clean this up
fn parse(input: &str) -> Vec<Vec<CubeSet>> {
input
.lines()
.map(|line| {
let cubes = line.split_once(':').unwrap().1;
let sets = cubes.split(';').collect::<Vec<_>>();

let mut cube_sets = Vec::new();
for part in sets {
let iter = part.trim().split(',');

let mut cubes = CubeSet::default();
for i in iter {
let mut iter = i.trim().split_whitespace();
let count = iter.next().unwrap().parse::<u32>().unwrap();
let color = iter.next().unwrap();

match color {
"red" => cubes.red += count,
"green" => cubes.green += count,
"blue" => cubes.blue += count,
_ => unreachable!(),
}
}
cube_sets.push(cubes);
}

cube_sets
})
.collect()
}

#[derive(Debug, Default)]
struct CubeSet {
red: u32,
Expand All @@ -115,6 +83,10 @@ impl CubeSet {
blue: self.blue.max(other.blue),
}
}

fn is_possible(&self) -> bool {
self.red <= MAX_CUBES[0] && self.green <= MAX_CUBES[1] && self.blue <= MAX_CUBES[2]
}
}

#[cfg(test)]
Expand Down

0 comments on commit ae5e3db

Please sign in to comment.