From 543c82574c111066691d8b05931214b5295f0f83 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Thu, 15 Dec 2022 18:06:53 -0500 Subject: Solve day 14 part 1 --- 2022/day14.awk | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 2022/day14.awk (limited to '2022') diff --git a/2022/day14.awk b/2022/day14.awk new file mode 100644 index 0000000..45bc5fe --- /dev/null +++ b/2022/day14.awk @@ -0,0 +1,44 @@ +BEGIN { + FS = "[ ,>-]+"; +} +{ + x = $1; + y = $2; + for (i = 3; i <= NF; i += 2) { + tx = $i; + ty = $(i+1); + while (x != tx) { + m[x,y] = "#"; + if (x < tx) x++; + if (x > tx) x--; + } + while (y != ty) { + m[x,y] = "#"; + if (y < ty) y++; + if (y > ty) y--; + } + m[x,y] = "#"; + if (y > ymax) ymax = y; + } +} +END { + x = 500; + y = 0; + while (y < ymax) { + if (!m[x,y+1]) { + y++; + } else if (!m[x-1,y+1]) { + x--; + y++; + } else if (!m[x+1,y+1]) { + x++; + y++; + } else { + m[x,y] = "o"; + sand++; + x = 500; + y = 0; + } + } + print sand; +} -- cgit 1.4.1