summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bin/day_21.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/bin/day_21.rs b/src/bin/day_21.rs
new file mode 100644
index 0000000..e937bcb
--- /dev/null
+++ b/src/bin/day_21.rs
@@ -0,0 +1,41 @@
+use nom::{
+ bytes::complete::tag,
+ character::complete::u32 as nom_u32,
+ combinator::map,
+ sequence::{preceded, tuple},
+ IResult,
+};
+use std::fs;
+
+fn main() -> Result<(), Box<dyn std::error::Error>> {
+ let input = fs::read_to_string("inputs/day_21.txt")?;
+ let mut player_positions = parse_starting_positions(&input).unwrap().1;
+ let mut player_scores = [0, 0];
+ let mut dice = (1..=100).cycle().enumerate().peekable();
+ let mut player_turn = 0;
+ while player_scores[0] < 1000 && player_scores[1] < 1000 {
+ let dice_roll: u32 = dice.by_ref().take(3).map(|(_, roll)| roll).sum();
+ player_positions[player_turn] = (player_positions[player_turn] + dice_roll) % 10;
+ if player_positions[player_turn] == 0 {
+ player_positions[player_turn] = 10;
+ }
+ player_scores[player_turn] += player_positions[player_turn];
+ player_turn = (player_turn + 1) % 2;
+ }
+ let losing_score = player_scores.iter().min().cloned().unwrap_or(0);
+ let dice_roll_count = dice.peek().unwrap().0;
+ dbg!((losing_score, dice_roll_count));
+ dbg!(losing_score * dice_roll_count as u32);
+
+ Ok(())
+}
+
+fn parse_starting_positions(input: &str) -> IResult<&str, [u32; 2]> {
+ map(
+ tuple((
+ preceded(tag("Player 1 starting position: "), nom_u32),
+ preceded(tag("\nPlayer 2 starting position: "), nom_u32),
+ )),
+ |(a, b)| [a, b],
+ )(input)
+}