Skip to content

Commit

Permalink
remove zs
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcacheux committed Dec 8, 2023
1 parent 875a589 commit 0a330d2
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions src/aoc2023/day8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl Solution<Day8> for Aoc2023 {
loop {
let (iter_state, next_dir) = inst_stream.next();
// iter_state == 0 is a complete hack, but it works
if fast_mapper.ends_with_z(current) && iter_state == 0 {
if iter_state == 0 && fast_mapper.id_to_str(current).ends_with('Z') {
if let Some(&previous_step) = states.get(&current) {
let delta = step - previous_step;
assert_eq!(previous_step, delta);
Expand Down Expand Up @@ -150,13 +150,11 @@ struct FastEdgeMapper<'d> {
keys: Vec<&'d str>,
map: HashMap<&'d str, usize>,
edges: Vec<usize>,
zs: Vec<usize>,
}

impl<'d> FastEdgeMapper<'d> {
fn new(edges: &'d HashMap<String, (String, String)>) -> Self {
let mut map: HashMap<&'d str, usize> = HashMap::new();
let mut zs = Vec::new();
let mut keys = vec![""; edges.len()];

let mut counter = 0;
Expand All @@ -174,10 +172,6 @@ impl<'d> FastEdgeMapper<'d> {

keys[from_id] = from.as_str();

if from.ends_with('Z') {
zs.push(from_id);
}

data[2 * from_id] = left_id;
data[2 * from_id + 1] = right_id;
}
Expand All @@ -186,16 +180,15 @@ impl<'d> FastEdgeMapper<'d> {
keys,
map,
edges: data,
zs,
}
}

fn str_to_id(&self, s: &str) -> usize {
*self.map.get(s).unwrap()
}

fn ends_with_z(&self, id: usize) -> bool {
self.zs.contains(&id)
fn id_to_str(&self, id: usize) -> &str {
self.keys[id]
}

fn iter_keys(&self) -> impl Iterator<Item = (usize, &'d str)> + '_ {
Expand Down

0 comments on commit 0a330d2

Please sign in to comment.