day 5 with awk

This commit is contained in:
setop 2021-12-08 01:12:56 +01:00
parent f0f10a53e5
commit 1b266b322c
2 changed files with 27 additions and 8 deletions

23
d05/d05_2.awk Normal file
View File

@ -0,0 +1,23 @@
BEGIN { FS = ",| -> " }
function min(a, b) { return a < b ? a : b }
function max(a, b) { return a > b ? a : b }
function abs(a) { return a < 0 ? -a : a }
function sign(a) { return a < 0 ? -1 : 1 }
$1 == $3 {
for (i = min($2, $4); i <= max($2, $4); i++)
n+= (++floor[$1, i] == 2)
next
}
$2 == $4 {
for (i = min($1, $3); i <= max($1, $3); i++)
n+= (++floor[i, $2] == 2)
next
}
{
i = sign($3-$1)
j = sign($4-$2)
for (a = 0; a <= abs($3-$1); a++)
n+= (++floor[$1+i*a, $2+a*j] == 2)
next
}
END { print n }

View File

@ -1,15 +1,12 @@
import sys sign = lambda x: (1,-1)[x<0]
W,H = 1000,1000 grid = [[0 for _ in range(1000)] for _ in range(1000)]
grid = [ [ 0 for _ in range(W) ] for _ in range(H) ]
L = eval(sys.stdin.read())
sign = lambda x: (1, -1)[x<0]
n=0 n=0
def incr(x,y): def incr(x,y):
global n global n
grid[x][y]+=1 grid[x][y]+=1
if grid[x][y]==2: if grid[x][y]==2:
n+=1 n+=1
for ((x1,y1),(x2,y2)) in L: for ((x1,y1),(x2,y2)) in eval(__import__("sys").stdin.read()):
if x1 == x2: if x1 == x2:
for y in range(min(y1,y2), max(y1,y2)+1): for y in range(min(y1,y2), max(y1,y2)+1):
incr(x1,y) incr(x1,y)
@ -17,9 +14,8 @@ for ((x1,y1),(x2,y2)) in L:
for x in range(min(x1,x2), max(x1,x2)+1): for x in range(min(x1,x2), max(x1,x2)+1):
incr(x,y1) incr(x,y1)
else: else:
r = abs(x1-x2)
i = sign(x2-x1) i = sign(x2-x1)
j = sign(y2-y1) j = sign(y2-y1)
for a in range(r+1): for a in range(abs(x1-x2)+1):
incr(x1+a*i,y1+a*j) incr(x1+a*i,y1+a*j)
print(n) print(n)