#[path = "../lib.rs"] mod lib; use std::collections::HashSet; fn simulate(len: usize) -> usize { let mut rope: Vec<(i64, i64)> = vec![(0, 0); len]; let mut visited = HashSet::new(); for line in lib::iter_input() { let (dir, steps_s) = lib::split_once(&line, ' ').unwrap(); let steps = steps_s.parse::().unwrap(); for _ in 0..steps { match dir { "U" => { rope[len - 1].1 += 1; }, "D" => { rope[len - 1].1 -= 1; }, "L" => { rope[len - 1].0 -= 1; }, "R" => { rope[len - 1].0 += 1; }, _ => unreachable!(), } for i1 in (0..len - 1).rev() { let i2 = i1 + 1; if (rope[i2].0 - rope[i1].0).abs() > 1 || (rope[i2].1 - rope[i1].1).abs() > 1 { if rope[i1].0 < rope[i2].0 { rope[i1].0 += 1; } else if rope[i1].0 > rope[i2].0 { rope[i1].0 -= 1; } if rope[i1].1 < rope[i2].1 { rope[i1].1 += 1; } else if rope[i1].1 > rope[i2].1 { rope[i1].1 -= 1; } } } visited.insert(rope[0]); } } return visited.len(); } fn main() { println!("part1: {}", simulate(2)); println!("part2: {}", simulate(10)); }