Skip to content

Commit 477110d

Browse files
committed
Refactoring.
1 parent 85dee71 commit 477110d

File tree

7 files changed

+17
-11
lines changed

7 files changed

+17
-11
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"git.enabled": false
3+
}

2024/21/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2024"
55

66
[dependencies]
77
enum-iterator = "2.1.0"
8-
itertools.workspace = true
8+
# itertools.workspace = true
99
parse-display.workspace = true
1010
parse-display-with.workspace = true
1111
pathfinding.workspace = true

2024/21/src/costs.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::directional_keypad::{self, DirectionalKeypad};
22
use crate::keypad::Keypad;
3-
use itertools::Itertools as _;
43
use pathfinding::prelude::dijkstra;
54
use std::collections::HashMap;
65
use std::hash::Hash;
@@ -17,8 +16,10 @@ impl<KP: Keypad> PrecomputedCosts<KP> {
1716
pub fn new(keypad: &KP, costs: &dyn Costs<KP>) -> Self {
1817
PrecomputedCosts(
1918
enum_iterator::all::<KP::Key>()
20-
.cartesian_product(enum_iterator::all::<KP::Key>())
21-
.map(|(source, target)| ((source, target), costs.cost(keypad, source, target)))
19+
.flat_map(|source| {
20+
enum_iterator::all::<KP::Key>()
21+
.map(move |target| ((source, target), costs.cost(keypad, source, target)))
22+
})
2223
.collect(),
2324
)
2425
}
@@ -46,7 +47,7 @@ impl<KP: Keypad> Costs<KP> for Human {
4647

4748
#[derive(Debug, Clone, Copy)]
4849
pub struct Robot<C: Costs<DirectionalKeypad>> {
49-
parent: C,
50+
operator: C,
5051
}
5152

5253
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
@@ -56,8 +57,8 @@ struct State<Key> {
5657
}
5758

5859
impl<C: Costs<DirectionalKeypad>> Robot<C> {
59-
pub fn new(parent: C) -> Self {
60-
Self { parent }
60+
pub fn new(operator: C) -> Self {
61+
Self { operator }
6162
}
6263

6364
// Next states and the costs to get there.
@@ -84,7 +85,7 @@ impl<C: Costs<DirectionalKeypad>> Robot<C> {
8485
parent_key: new_parent_key,
8586
key,
8687
},
87-
self.parent
88+
self.operator
8889
.cost(&DirectionalKeypad, parent_key, new_parent_key),
8990
));
9091
}
@@ -103,11 +104,11 @@ impl<KP: Keypad, C: Costs<DirectionalKeypad>> Costs<KP> for Robot<C> {
103104
// Find the cost of the shortest path to the state where the
104105
// robot is hovering over `target`.
105106
let start: State<<KP as Keypad>::Key> = State {
106-
parent_key: directional_keypad::Key::A,
107+
parent_key: DirectionalKeypad::ACTIVATE,
107108
key: source,
108109
};
109110
let target: State<<KP as Keypad>::Key> = State {
110-
parent_key: directional_keypad::Key::A,
111+
parent_key: DirectionalKeypad::ACTIVATE,
111112
key: target,
112113
};
113114
let (_, cost) = dijkstra(

2024/21/src/directional_keypad.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ impl From<Direction> for Key {
3030

3131
impl Keypad for DirectionalKeypad {
3232
type Key = Key;
33+
const ACTIVATE: Self::Key = Key::A;
3334

3435
fn pos(&self, key: Self::Key) -> (usize, usize) {
3536
use Direction::*;

2024/21/src/keypad.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::hash::Hash;
55

66
pub trait Keypad: Clone + Copy + Eq + Hash + Debug + Default {
77
type Key: Copy + Clone + Default + PartialEq + Eq + Hash + Display + Debug + Sequence;
8+
const ACTIVATE: Self::Key;
89

910
fn pos(&self, key: Self::Key) -> (usize, usize);
1011
fn at(&self, pos: (usize, usize)) -> Option<Self::Key>;

2024/21/src/numeric_keypad.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub struct NumericKeypad;
1919

2020
impl Keypad for NumericKeypad {
2121
type Key = Key;
22+
const ACTIVATE: Self::Key = Key::A;
2223

2324
fn pos(&self, key: Self::Key) -> (usize, usize) {
2425
use Key::*;

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)