Skip to content

Commit 44e95c6

Browse files
authored
Account for remainder in 2015 Day 21 (#12)
1 parent e89137b commit 44e95c6

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/year2015/day21.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use std::ops::Add;
88

99
#[derive(Clone, Copy)]
1010
struct Item {
11-
cost: i32,
12-
damage: i32,
13-
armor: i32,
11+
cost: u32,
12+
damage: u32,
13+
armor: u32,
1414
}
1515

1616
impl Add for Item {
@@ -25,11 +25,11 @@ impl Add for Item {
2525
}
2626
}
2727

28-
type Result = (bool, i32);
28+
type Result = (bool, u32);
2929

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

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

77-
let hero_turns = boss_health / (damage - boss_armor).max(1);
78-
let boss_turns = 100 / (boss_damage - armor).max(1);
77+
let hero_hit = damage.saturating_sub(boss_armor).max(1);
78+
let hero_turns = boss_health.div_ceil(hero_hit);
79+
let boss_hit = boss_damage.saturating_sub(armor).max(1);
80+
let boss_turns = 100_u32.div_ceil(boss_hit);
7981
let win = hero_turns <= boss_turns;
8082

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

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

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

0 commit comments

Comments
 (0)