summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2021-12-21 09:35:55 +0200
committerJustin Wernick <justin@worthe-it.co.za>2021-12-21 09:35:55 +0200
commitd655a146082321096aa686368850efd807192cbf (patch)
treecff5410cf3c5069ceba0da85095283298a7599dd
parent2a92b9d77f787f898d2db5e5d88205b7b4d6e94e (diff)
Day 21 part 1
-rw-r--r--inputs/day_21.txt2
-rw-r--r--src/bin/day_21.rs41
2 files changed, 43 insertions, 0 deletions
diff --git a/inputs/day_21.txt b/inputs/day_21.txt
new file mode 100644
index 0000000..bfb2937
--- /dev/null
+++ b/inputs/day_21.txt
@@ -0,0 +1,2 @@
+Player 1 starting position: 7
+Player 2 starting position: 5
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)
+}