# aoc2021

Advent of Code 2021 solutions in Perl.
git clone git://git.samirparikh.com/aoc2021
Log | Files | Refs | README

```commit aea1e9ebf5d71e3f25a82fcea9fc4f4447191d63
parent e87a5bf731234a77f15b6b0817e03df783487764
Date:   Thu,  9 Dec 2021 00:17:41 +0000

update to start using newer logic

Diffstat:
Mday05-2.pl | 51++++++++++++++++++++-------------------------------
```
```1 file changed, 20 insertions(+), 31 deletions(-)
diff --git a/day05-2.pl b/day05-2.pl
@@ -7,13 +7,13 @@ use List::Util qw( max );
use diagnostics;

sub draw_matrix {
-    my (\$matrix, \$rows, \$columns) = @_;
+    my (\$matrix) = shift;
-    foreach my \$y (0 .. \$rows) {
-        foreach my \$x (0 .. \$columns) {
-            if (defined( \$matrix->[\$y][\$x] )) {
-                print \$matrix->[\$y][\$x];
-                \$answer++ if (\$matrix->[\$y][\$x] > 1);
+    foreach my \$y (@\$matrix) {
+        foreach my \$x (@\$y) {
+            if (defined( \$x )) {
+                print \$x;
+                \$answer++ if (\$x > 1);
} else {
print ".";
}
@@ -37,34 +37,23 @@ my @matrix = ();
my (\$rows, \$columns) = (0, 0);
foreach (@input) {
my (\$x1, \$y1, \$x2, \$y2) = ( /(\d+),(\d+) -> (\d+),(\d+)/ );
-    if ((\$x2 <= \$x1) && (\$y2 <= \$y1)) {
-        (\$x2, \$x1) = (\$x1, \$x2);
-        (\$y2, \$y1) = (\$y1, \$y2);
-    }
-    if ((\$x2 <= \$x1) && (\$y2 >= \$y1)) {
-        (\$x2, \$x1) = (\$x1, \$x2);
-        (\$y2, \$y1) = (\$y1, \$y2);
-    }
-    \$columns = max( \$columns, \$x1, \$x2);
-    \$rows    = max( \$rows, \$y1, \$y2);
-    if ((\$x2 >= \$x1) && (\$y2 >= \$y1)) {
-        for (my \$x = \$x1; \$x < \$x2 + 1; \$x++) {
-            for (my \$y = \$y1; \$y < \$y2 + 1; \$y++) {
-                #if ((\$x1 == \$x2) || (\$y1 == \$y2)) {
-                    \$matrix[\$y][\$x]++;
-                #}
-            }
+    say "input is ", join(", ", (\$x1, \$y1, \$x2, \$y2));
+    if (\$x1 == \$x2) { # line is vertical
+        say "line is vertical";
+        (\$y1, \$y2) = (\$y2, \$y1) if (\$y1 > \$y2); # swap so that \$y1 < \$y2
+        for (my \$y = \$y1; \$y <= \$y2; \$y++) {
+            \$matrix[\$y][\$x1]++;
}
}
-    elsif ((\$x2 >= \$x1) && (\$y2 <= \$y1)) {
-        for (my \$x = \$x1; \$x < \$x2 + 1; \$x++) {
-            for (my \$y = \$y2; \$y < \$y1 + 1; \$y++) {
-                #if ((\$x1 == \$x2) || (\$y1 == \$y2)) {
-                    \$matrix[\$y][\$x]++;
-                #}
-            }
+    elsif (\$y1 == \$y2) { # line is horizontal
+        say "line is horizontal";
+        (\$x1, \$x2) = (\$x2, \$x1) if (\$x1 > \$x2); # swap so that \$x1 < \$x2
+        for (my \$x = \$x1; \$x <= \$x2; \$x++) {
+            \$matrix[\$y1][\$x]++;
}
}
+    draw_matrix( \@matrix, \$rows, \$columns);
+    \$wait = <STDIN>;
}

-say draw_matrix( \@matrix, \$rows, \$columns);
+say draw_matrix( \@matrix );
```