@@ -8,9 +8,9 @@ use std::ops::Add;
8
8
9
9
#[ derive( Clone , Copy ) ]
10
10
struct Item {
11
- cost : i32 ,
12
- damage : i32 ,
13
- armor : i32 ,
11
+ cost : u32 ,
12
+ damage : u32 ,
13
+ armor : u32 ,
14
14
}
15
15
16
16
impl Add for Item {
@@ -25,11 +25,11 @@ impl Add for Item {
25
25
}
26
26
}
27
27
28
- type Result = ( bool , i32 ) ;
28
+ type Result = ( bool , u32 ) ;
29
29
30
30
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 ( ) ;
33
33
34
34
let weapon = [
35
35
Item { cost : 8 , damage : 4 , armor : 0 } ,
@@ -74,8 +74,10 @@ pub fn parse(input: &str) -> Vec<Result> {
74
74
for & third in & combinations {
75
75
let Item { cost, damage, armor } = first + second + third;
76
76
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) ;
79
81
let win = hero_turns <= boss_turns;
80
82
81
83
results. push ( ( win, cost) ) ;
@@ -86,10 +88,10 @@ pub fn parse(input: &str) -> Vec<Result> {
86
88
results
87
89
}
88
90
89
- pub fn part1 ( input : & [ Result ] ) -> i32 {
91
+ pub fn part1 ( input : & [ Result ] ) -> u32 {
90
92
* input. iter ( ) . filter_map ( |( w, c) | w. then_some ( c) ) . min ( ) . unwrap ( )
91
93
}
92
94
93
- pub fn part2 ( input : & [ Result ] ) -> i32 {
95
+ pub fn part2 ( input : & [ Result ] ) -> u32 {
94
96
* input. iter ( ) . filter_map ( |( w, c) | ( !w) . then_some ( c) ) . max ( ) . unwrap ( )
95
97
}
0 commit comments