aboutsummaryrefslogtreecommitdiff
path: root/2021/03/puzzle-1.awk
diff options
context:
space:
mode:
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
+}