From 5e4395eab49524f41f217f664388a170924ec933 Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Sun, 18 Dec 2022 02:28:47 +0900 Subject: [aoc/2022] Finish day 10 --- aoc/2022/10/part-one.awk | 8 ++++++++ aoc/2022/10/part-two.awk | 10 ++++++++++ 2 files changed, 18 insertions(+) create mode 100755 aoc/2022/10/part-one.awk create mode 100755 aoc/2022/10/part-two.awk diff --git a/aoc/2022/10/part-one.awk b/aoc/2022/10/part-one.awk new file mode 100755 index 0000000..756464e --- /dev/null +++ b/aoc/2022/10/part-one.awk @@ -0,0 +1,8 @@ +#!/usr/bin/env -S awk -f +BEGIN { cycle = x = 1 } +{ cycle++ } +cycle % 40 == 20 { strength += cycle * x } +{ x += $2 } +/^addx/ { cycle++ } +/^addx/ && cycle % 40 == 20 { strength += cycle * x } +END { print strength } diff --git a/aoc/2022/10/part-two.awk b/aoc/2022/10/part-two.awk new file mode 100755 index 0000000..9126306 --- /dev/null +++ b/aoc/2022/10/part-two.awk @@ -0,0 +1,10 @@ +#!/usr/bin/env -S awk -f +function cycle (c, x) { + printf x - 2 < c && c < x + 2 ? "#" : "." + printf c == 39 ? "\n" : "" + return (c + 1) % 40 +} +BEGIN { x = 1 } +{ c = cycle(c, x) } +/^addx/ { c = cycle(c, x) } +{ x += $2 } -- cgit 1.4.1