Last active
November 23, 2023 11:09
-
-
Save oguz-ismail/3c4792abbcc8ec6bde0253dba16ab062 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
gsub(/./, "& ") | |
} | |
NR == 1 { | |
for (i = 1; i <= NF; i++) | |
light[bin(i-1)] = ($i == "#") | |
next | |
} | |
{ | |
for (x = 1; x <= NF; x++) | |
lit[NR, x] = ($x == "#") | |
} | |
END { | |
x0 = 1; xn = NF | |
y0 = 3; yn = NR | |
enhance_twice() | |
print count_lit() | |
for (i = 1; i < 25; i++) | |
enhance_twice() | |
print count_lit() | |
} | |
function enhance_twice() { | |
enhance(lit, tmp, 0) | |
enhance(tmp, lit, light["000000000"]) | |
} | |
function enhance(src, dst, fill, x, y) { | |
x0--; xn++ | |
y0--; yn++ | |
for (y = y0; y <= yn; y++) | |
for (x = x0; x <= xn; x++) | |
enhance_pixel(src, x, y, dst, fill) | |
} | |
function enhance_pixel(src, x, y, dst, fill, | |
ind, xi, yi) { | |
for (yi = y-1; yi <= y+1; yi++) | |
for (xi = x-1; xi <= x+1; xi++) | |
if ((yi, xi) in src) | |
ind = ind src[yi, xi] | |
else | |
ind = ind fill | |
dst[y, x] = light[ind] | |
} | |
function count_lit( n, i) { | |
for (i in lit) | |
n += lit[i] | |
return n | |
} | |
function bin(x, ret, i) { | |
for (i = 0; i < 9; i++) { | |
ret = int(x%2) ret | |
x /= 2 | |
} | |
return ret | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment