aboutsummaryrefslogtreecommitdiff
path: root/2021/05/puzzles.awk
diff options
context:
space:
mode:
authorThomas Voss <thomasvoss@live.com> 2021-12-05 06:47:55 +0100
committerThomas Voss <thomasvoss@live.com> 2021-12-05 06:47:55 +0100
commit5cf278d358e0b267b70d63bc72ae1effdb85a882 (patch)
treeb0af38643c974f08c673fb9729fb16209b88528e /2021/05/puzzles.awk
parent0f4b26a35cc3f3d805272e9f2e542afea42b4752 (diff)
Add day 5 solutions
Diffstat (limited to '2021/05/puzzles.awk')
-rw-r--r--2021/05/puzzles.awk25
1 files changed, 25 insertions, 0 deletions
diff --git a/2021/05/puzzles.awk b/2021/05/puzzles.awk
new file mode 100644
index 0000000..976b6aa
--- /dev/null
+++ b/2021/05/puzzles.awk
@@ -0,0 +1,25 @@
+#!/usr/bin/env -S awk -f
+
+BEGIN { FS = ",| -> " }
+
+$1 == $3 && $2 <= $4 { for (i = $2; i <= $4; i++) grid[i][$1]++; next }
+$1 == $3 { for (i = $4; i <= $2; i++) grid[i][$1]++; next }
+$2 == $4 && $1 <= $3 { for (i = $1; i <= $3; i++) grid[$2][i]++; next }
+$2 == $4 { for (i = $3; i <= $1; i++) grid[$2][i]++; next }
+
+# START PART 2
+$1 <= $3 && $2 <= $4 { while (!($1 > $3)) { grid[$2][$1]++; $1++; $2++ } next }
+$1 <= $3 { while (!($1 > $3)) { grid[$2][$1]++; $1++; $2-- } next }
+$2 <= $4 { while (!($1 < $3)) { grid[$2][$1]++; $1--; $2++ } next }
+ { while (!($1 < $3)) { grid[$2][$1]++; $1--; $2-- } next }
+# END PART 2
+
+END {
+ for (i = 0; i < 1000; i++) {
+ for (j = 0; j < 1000; j++) {
+ if (grid[i][j] > 1)
+ acc++
+ }
+ }
+ print acc
+}