From ba28cdf3409922f88d444d01f0a597eff2232d0c Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Wed, 5 Jul 2023 20:20:11 +0200 Subject: [PATCH] day(22): factor out common parts of solver Signed-off-by: Matej Focko --- src/bin/day22.rs | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/bin/day22.rs b/src/bin/day22.rs index c37ea9c..0fa5ded 100644 --- a/src/bin/day22.rs +++ b/src/bin/day22.rs @@ -96,6 +96,25 @@ lazy_static! { HashMap::from([((1, 0), 0), ((0, 1), 1), ((-1, 0), 2), ((0, -1), 3)]); } +fn solve(wrapper: &impl Wrap, input: &Input) -> Output { + let final_state = input + .instructions + .iter() + .fold(State::new(input), |mut state, y| { + match &y { + Instruction::Move(steps) => state.step(wrapper, *steps), + Instruction::TurnLeft => state.turn_left(), + Instruction::TurnRight => state.turn_right(), + } + + state + }); + + (1000 * (final_state.y + 1) + 4 * (final_state.x + 1) + DIRECTIONS[&final_state.direction]) + .try_into() + .unwrap() +} + struct Day22; impl Solution for Day22 { fn parse_input>(pathname: P) -> Input { @@ -163,24 +182,7 @@ impl Solution for Day22 { } fn part_1(input: &Input) -> Output { - let wrapper = Wrap2D; - - let final_state = input - .instructions - .iter() - .fold(State::new(input), |mut state, y| { - match &y { - Instruction::Move(steps) => state.step(&wrapper, *steps), - Instruction::TurnLeft => state.turn_left(), - Instruction::TurnRight => state.turn_right(), - } - - state - }); - - (1000 * (final_state.y + 1) + 4 * (final_state.x + 1) + DIRECTIONS[&final_state.direction]) - .try_into() - .unwrap() + solve(&Wrap2D, input) } fn part_2(_input: &Input) -> Output {