1
0
Fork 0

day(22): factor out common parts of solver

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2023-07-05 20:20:11 +02:00
parent fcdff3c129
commit ba28cdf340
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -96,6 +96,25 @@ lazy_static! {
HashMap::from([((1, 0), 0), ((0, 1), 1), ((-1, 0), 2), ((0, -1), 3)]); 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; struct Day22;
impl Solution<Input, Output> for Day22 { impl Solution<Input, Output> for Day22 {
fn parse_input<P: AsRef<Path>>(pathname: P) -> Input { fn parse_input<P: AsRef<Path>>(pathname: P) -> Input {
@ -163,24 +182,7 @@ impl Solution<Input, Output> for Day22 {
} }
fn part_1(input: &Input) -> Output { fn part_1(input: &Input) -> Output {
let wrapper = Wrap2D; solve(&Wrap2D, input)
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()
} }
fn part_2(_input: &Input) -> Output { fn part_2(_input: &Input) -> Output {