my %delta = <U D L R> Z=> i, -i, -1.Complex, 1.Complex;
sub simulate(@movements, UInt:D \knot-num --> UInt:D) {
my @knots = 0.Complex xx knot-num;
my %trail is SetHash;
for @movements -> \drt, \dst {
for ^dst.Int {
@knots[0] += %delta{drt};
for 1 ..^ knot-num -> \k {
my (\dx, \dy) = (@knots[k-1] - @knots[k]).reals;
@knots[k] += dx.sign + (dy.sign)i if dx² + dy² > 2;
}
%trail.set(@knots.tail);
}
}
+%trail
}
with $*IN.words -> @movements {
put 'part 1: ', simulate(@movements, 2);
put 'part 2: ', simulate(@movements, 10);
}