Skip to content

Commit

Permalink
day8: finish by using LCM
Browse files Browse the repository at this point in the history
  • Loading branch information
mirsella committed Dec 8, 2023
1 parent 9757ce8 commit 7de915f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
1 change: 1 addition & 0 deletions 2023/day8/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
num = "0.4.1"
50 changes: 19 additions & 31 deletions 2023/day8/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::collections::HashMap;

use num::integer::lcm;

enum Direction {
Left,
Right,
Expand Down Expand Up @@ -63,48 +65,34 @@ fn part1(input: &str) -> usize {
}
count
}

/// from the subreddit, we can use the LCM algorithm (couldn't figure it out myself this time)
fn part2(input: &str) -> usize {
let map = Map::parse(input);
let mut currents = map
let currents = map
.network
.keys()
.filter(|k| k.ends_with('A'))
.copied()
.collect::<Vec<_>>();
let mut directions = map.directions.iter().cycle();
let mut count: usize = 0;
while !currents.iter().all(|c| c.ends_with('Z')) {
let direction = directions.next().unwrap();
for current in currents.iter_mut() {
let mut lowest: usize = 0;
for mut current in currents {
let mut directions = map.directions.iter().cycle();
let mut count = 0;
while !current.ends_with('Z') {
let node = map.network.get(current).unwrap();
match direction {
Direction::Left => *current = node.0,
Direction::Right => *current = node.1,
}
if current.ends_with('Z') {
println!("{}: {}", count, current);
count += 1;
match directions.next().unwrap() {
Direction::Left => current = node.0,
Direction::Right => current = node.1,
}
}
count += 1;
match lowest {
0 => lowest = count,
_ => lowest = lcm(lowest, count),
}
}

// let mut gcount: usize = 0;
// for mut current in currents {
// let mut directions = map.directions.iter().cycle();
// let mut count = 0;
// while !current.ends_with('Z') {
// let node = map.network.get(current).unwrap();
// count += 1;
// match directions.next().unwrap() {
// Direction::Left => current = node.0,
// Direction::Right => current = node.1,
// }
// }
// println!("count: {}", count);
// gcount += count;
// }
// gcount
count
lowest
}
fn main() {
let input = include_str!("../input.txt");
Expand Down

0 comments on commit 7de915f

Please sign in to comment.