Skip to content

Account for remainder in 2015 Day 21 #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/year2015/day21.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use std::ops::Add;

#[derive(Clone, Copy)]
struct Item {
cost: i32,
damage: i32,
armor: i32,
cost: u32,
damage: u32,
armor: u32,
}

impl Add for Item {
Expand All @@ -25,11 +25,11 @@ impl Add for Item {
}
}

type Result = (bool, i32);
type Result = (bool, u32);

pub fn parse(input: &str) -> Vec<Result> {
let [boss_health, boss_damage, boss_armor]: [i32; 3] =
input.iter_signed().chunk::<3>().next().unwrap();
let [boss_health, boss_damage, boss_armor]: [u32; 3] =
input.iter_unsigned().chunk::<3>().next().unwrap();

let weapon = [
Item { cost: 8, damage: 4, armor: 0 },
Expand Down Expand Up @@ -74,8 +74,10 @@ pub fn parse(input: &str) -> Vec<Result> {
for &third in &combinations {
let Item { cost, damage, armor } = first + second + third;

let hero_turns = boss_health / (damage - boss_armor).max(1);
let boss_turns = 100 / (boss_damage - armor).max(1);
let hero_hit = damage.saturating_sub(boss_armor).max(1);
let hero_turns = boss_health.div_ceil(hero_hit);
let boss_hit = boss_damage.saturating_sub(armor).max(1);
let boss_turns = 100_u32.div_ceil(boss_hit);
let win = hero_turns <= boss_turns;

results.push((win, cost));
Expand All @@ -86,10 +88,10 @@ pub fn parse(input: &str) -> Vec<Result> {
results
}

pub fn part1(input: &[Result]) -> i32 {
pub fn part1(input: &[Result]) -> u32 {
*input.iter().filter_map(|(w, c)| w.then_some(c)).min().unwrap()
}

pub fn part2(input: &[Result]) -> i32 {
pub fn part2(input: &[Result]) -> u32 {
*input.iter().filter_map(|(w, c)| (!w).then_some(c)).max().unwrap()
}