From 8b8e8e053ef8031cbba8a8409a714bfa3196651f Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Fri, 3 Dec 2021 07:07:50 +0100 Subject: Add day 3 solutions --- 2021/03/puzzle-1.awk | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100755 2021/03/puzzle-1.awk (limited to '2021/03/puzzle-1.awk') 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 +} -- cgit v1.2.3