heh
bendn 2023-12-08
parent bbfdf6c · commit 3d87d10
-rw-r--r--src/main.rs47
-rw-r--r--src/util.rs2
2 files changed, 36 insertions, 13 deletions
diff --git a/src/main.rs b/src/main.rs
index 04689a9..2a4fda5 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -16,22 +16,45 @@ fn solve(i: &str) -> impl Display {
.ml(str::trim)
})
.collect::<HashMap<_, _>>();
- let mut position = "AAA";
- let mut steps = 1;
+ let mut positions = map
+ .keys()
+ .map(|&x| x)
+ .filter(|x| x.ends_with('A'))
+ .collect::<Box<[_]>>();
+ let mut steps = 1u64;
+ let mut findings = HashMap::new();
+ let mut cycle = HashMap::new();
for &instruction in line.iter().cycle() {
- println!("{position}");
- let at = map[position];
- position = match instruction {
- b'L' => at.0,
- b'R' => at.1,
- _ => dang!(),
- };
- if position == "ZZZ" {
- return steps;
+ if cycle.len() >= positions.len() {
+ break;
+ }
+ for p in &mut *positions {
+ let at = map[*p];
+ *p = match instruction {
+ b'L' => at.0,
+ b'R' => at.1,
+ _ => dang!(),
+ };
+ if p.ends_with('Z') {
+ if let Some(&c) = findings.get(*p) {
+ if !cycle.contains_key(*p) {
+ println!("cycle {} ({steps})", steps - c);
+ cycle.insert(*p, steps - c);
+ }
+ } else {
+ println!("register {p} ({steps})");
+ findings.insert(*p, steps);
+ }
+ }
}
steps += 1;
}
- dang!();
+ print!("lcm(");
+ for cycle in cycle.values() {
+ print!("{cycle},")
+ }
+ println!(")");
+ 0
}
fn main() {
diff --git a/src/util.rs b/src/util.rs
index 24c9958..5320a8d 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -21,7 +21,7 @@ pub mod prelude {
macro_rules! dang {
() => {
- panic!();
+ panic!()
};
}
pub(crate) use dang;