Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use List::AllUtils qw(pairwise sum);
- #
- # The calculation of a step is pos = pos + (vec * matrix).
- # With the center of the matrix set to aim for next.
- #
- # pos is (x, y, a), matrix is [ 1 0 0 ]
- # [ 0 a 0 ]
- # [ 0 0 1 ]
- #
- # vec is (m, m, 0) for forward, (0, 0, +/-m) for down/up
- #
- my @matrix = ([1, 0, 0],
- [0, 0, 0],
- [0, 0, 1]);
- my @pos = (0, 0, 0);
- while (<>) {
- my ($cmd, $mag) = m#^(\w)\w+ (\d+)#;
- # scalar * vector using map
- my @vec = map { $mag * $_ } ('f' eq $cmd, 'f' eq $cmd, 'f' cmp $cmd);
- # vector * matrix with map-sum-pairwise
- my @prod = map { sum pairwise { $a * $b } @vec, @$_ } @matrix;
- # vector + vector with pairwise
- @pos = pairwise { $a + $b } @pos, @prod;
- # set center value of matrix to aim
- $matrix[1][1] = $pos[2];
- }
- print "Part 1: ", $pos[0] * $pos[2], "\n";
- print "Part 2: ", $pos[0] * $pos[1], "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement