1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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)
}
|