aboutsummaryrefslogtreecommitdiff
path: root/2021/03/puzzle-1.awk
diff options
context:
space:
mode:
authorThomas Voss <thomasvoss@live.com> 2021-12-03 07:07:50 +0100
committerThomas Voss <thomasvoss@live.com> 2021-12-03 07:07:50 +0100
commit8b8e8e053ef8031cbba8a8409a714bfa3196651f (patch)
tree406a6e7f2868ae2862abc154cda3c61e900e0408 /2021/03/puzzle-1.awk
parent4bc4482a1a2c2e5b652d1fd6163ef5d0b26365e7 (diff)
Add day 3 solutions
Diffstat (limited to '2021/03/puzzle-1.awk')
-rwxr-xr-x2021/03/puzzle-1.awk22
1 files changed, 22 insertions, 0 deletions
diff --git a/2021/03/puzzle-1.awk b/2021/03/puzzle-1.awk
new file mode 100755
index 0000000..4f02561
--- /dev/null
+++ b/2021/03/puzzle-1.awk
@@ -0,0 +1,22 @@
+#!/usr/bin/env -S awk -f
+
+BEGIN { FS = "" }
+
+{
+ for (i = 1; i <= NF; i++) {
+ if ($i == "0")
+ z[i]++
+ else
+ o[i]++
+ }
+}
+
+END {
+ for (i = 1; i <= NF; i++) {
+ g = g (z[i] > o[i] ? "0" : "1")
+ e = e (z[i] < o[i] ? "0" : "1")
+ }
+ (cmd = "echo 'ibase = 2;" g "*" e "' | bc") | getline out
+ close(cmd)
+ print out
+}