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; }