diff --git a/d05/d05_2.awk b/d05/d05_2.awk new file mode 100644 index 0000000..2cce06a --- /dev/null +++ b/d05/d05_2.awk @@ -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 } diff --git a/d05/d05_2.py b/d05/d05_2.py index a59c2a0..452e202 100644 --- a/d05/d05_2.py +++ b/d05/d05_2.py @@ -1,15 +1,12 @@ -import sys -W,H = 1000,1000 -grid = [ [ 0 for _ in range(W) ] for _ in range(H) ] -L = eval(sys.stdin.read()) -sign = lambda x: (1, -1)[x<0] +sign = lambda x: (1,-1)[x<0] +grid = [[0 for _ in range(1000)] for _ in range(1000)] n=0 def incr(x,y): global n grid[x][y]+=1 if grid[x][y]==2: n+=1 -for ((x1,y1),(x2,y2)) in L: +for ((x1,y1),(x2,y2)) in eval(__import__("sys").stdin.read()): if x1 == x2: for y in range(min(y1,y2), max(y1,y2)+1): 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): incr(x,y1) else: - r = abs(x1-x2) i = sign(x2-x1) 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) print(n)