Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,136 @@
|
|||
global W H img[][] .
|
||||
dx[] = [ 0 1 1 1 0 -1 -1 -1 0 ]
|
||||
dy[] = [ -1 -1 0 1 1 1 0 -1 -1 ]
|
||||
#
|
||||
func A x y .
|
||||
cur = img[y + dy[1]][x + dx[1]]
|
||||
for i = 2 to 9
|
||||
nxt = img[y + dy[i]][x + dx[i]]
|
||||
if cur = 0 and nxt = 1 : c += 1
|
||||
cur = nxt
|
||||
.
|
||||
return c
|
||||
.
|
||||
func B x y .
|
||||
for i = 2 to 9
|
||||
c += img[y + dy[i]][x + dx[i]]
|
||||
.
|
||||
return c
|
||||
.
|
||||
func commonStep x y .
|
||||
if img[y][x] <> 1 or x <= 1 or x >= W or y <= 1 or y >= H : return 0
|
||||
b = B x y
|
||||
if b < 2 or b > 6 : return 0
|
||||
a = A x y
|
||||
if a <> 1 : return 0
|
||||
return 1
|
||||
.
|
||||
func stepOne x y .
|
||||
if commonStep x y = 0 : return 0
|
||||
p2 = img[y + dy[1]][x + dx[1]]
|
||||
p4 = img[y + dy[3]][x + dx[3]]
|
||||
p6 = img[y + dy[5]][x + dx[5]]
|
||||
p8 = img[y + dy[7]][x + dx[7]]
|
||||
if p4 = 0 or p6 = 0 or p2 + p8 = 0 : return 1
|
||||
return 0
|
||||
.
|
||||
func stepTwo x y .
|
||||
if commonStep x y = 0 : return 0
|
||||
p2 = img[y + dy[1]][x + dx[1]]
|
||||
p4 = img[y + dy[3]][x + dx[3]]
|
||||
p6 = img[y + dy[5]][x + dx[5]]
|
||||
p8 = img[y + dy[7]][x + dx[7]]
|
||||
if p2 = 0 or p8 = 0 or p4 + p6 = 0 : return 1
|
||||
return 0
|
||||
.
|
||||
func doStepOneAll .
|
||||
len del[][] H
|
||||
for y = 1 to H : len del[y][] W
|
||||
for y = 2 to H - 1 : for x = 2 to W - 1
|
||||
if stepOne x y = 1
|
||||
del[y][x] = 1
|
||||
cnt += 1
|
||||
.
|
||||
.
|
||||
if cnt > 0
|
||||
for y = 2 to H - 1 : for x = 2 to W - 1
|
||||
if del[y][x] = 1 : img[y][x] = 0
|
||||
.
|
||||
return 1
|
||||
.
|
||||
return 0
|
||||
.
|
||||
func doStepTwoAll .
|
||||
len del[][] H
|
||||
for y = 1 to H : len del[y][] W
|
||||
for y = 2 to H - 1 : for x = 2 to W - 1
|
||||
if stepTwo x y = 1
|
||||
del[y][x] = 1
|
||||
cnt += 1
|
||||
.
|
||||
.
|
||||
if cnt > 0
|
||||
for y = 2 to H - 1 : for x = 2 to W - 1
|
||||
if del[y][x] = 1 : img[y][x] = 0
|
||||
.
|
||||
return 1
|
||||
.
|
||||
return 0
|
||||
.
|
||||
proc zhangSuenThin .
|
||||
repeat
|
||||
changed = 0
|
||||
if doStepOneAll = 1 : changed = 1
|
||||
if doStepTwoAll = 1 : changed = 1
|
||||
until changed = 0
|
||||
.
|
||||
.
|
||||
proc readAscii .
|
||||
W = 0
|
||||
repeat
|
||||
s$ = input
|
||||
until s$ = ""
|
||||
lines$[] &= s$
|
||||
if len s$ > W : W = len s$
|
||||
.
|
||||
H = len lines$[]
|
||||
len img[][] H
|
||||
for y = 1 to H
|
||||
len img[y][] W
|
||||
c$[] = strchars lines$[y]
|
||||
for x = 1 to len c$[]
|
||||
h = strpos " #" c$[x]
|
||||
img[y][x] = h - 1
|
||||
.
|
||||
.
|
||||
.
|
||||
proc printAscii .
|
||||
for y = 1 to H
|
||||
for x = 1 to W
|
||||
write substr " #" (img[y][x] + 1) 1
|
||||
.
|
||||
print ""
|
||||
.
|
||||
.
|
||||
readAscii
|
||||
zhangSuenThin
|
||||
printAscii
|
||||
#
|
||||
#
|
||||
input_data
|
||||
################# #############
|
||||
################## ################
|
||||
################### ##################
|
||||
######## ####### ###################
|
||||
###### ####### ####### ######
|
||||
###### ####### #######
|
||||
################# #######
|
||||
################ #######
|
||||
################# #######
|
||||
###### ####### #######
|
||||
###### ####### #######
|
||||
###### ####### ####### ######
|
||||
######## ####### ###################
|
||||
######## ####### ###### ################## ######
|
||||
######## ####### ###### ################ ######
|
||||
######## ####### ###### ############# ######
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
$ 00000000000000000000000000000000
|
||||
$ 01111111110000000111111110000000
|
||||
$ 01110001111000001111001111000000
|
||||
$ 01110000111000001110000111000000
|
||||
$ 01110001111000001110000000000000
|
||||
$ 01111111110000001110000000000000
|
||||
$ 01110111100000001110000111000000
|
||||
$ 01110011110011101111001111011100
|
||||
$ 01110001111011100111111110011100
|
||||
$ 00000000000000000000000000000000
|
||||
⊜≡⋕⊸≠@\n
|
||||
A ← /+⧈>⊏1_2_5_8_7_6_3_0_1♭
|
||||
B ← -⊃(⊡1_1|/+/+)
|
||||
ABC ← /↧[⊃(⊡1_1|↧⊃≥₂≤₆B|=1A)]
|
||||
DE ← ¬/↧⊏⊙♭
|
||||
ZhangSuen ← (
|
||||
∧⍜⊡⋅0⊸(+¤1_1⊚⧈(/↧[⊃(ABC|DE1_5_7|DE3_5_7)])3_3)
|
||||
∧⍜⊡⋅0⊸(+¤1_1⊚⧈(/↧[⊃(ABC|DE1_3_5|DE1_3_7)])3_3)
|
||||
)
|
||||
⊙◌⍢(⟜⊙◌ZhangSuen|¬≍)⊸ZhangSuen
|
||||
&s≡₀⨬@_@#
|
||||
|
|
@ -0,0 +1,112 @@
|
|||
struct Point {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
const image = [
|
||||
" ",
|
||||
" ################# ############# ",
|
||||
" ################## ################ ",
|
||||
" ################### ################## ",
|
||||
" ######## ####### ################### ",
|
||||
" ###### ####### ####### ###### ",
|
||||
" ###### ####### ####### ",
|
||||
" ################# ####### ",
|
||||
" ################ ####### ",
|
||||
" ################# ####### ",
|
||||
" ###### ####### ####### ",
|
||||
" ###### ####### ####### ",
|
||||
" ###### ####### ####### ###### ",
|
||||
" ######## ####### ################### ",
|
||||
" ######## ####### ###### ################## ###### ",
|
||||
" ######## ####### ###### ################ ###### ",
|
||||
" ######## ####### ###### ############# ###### ",
|
||||
" ",
|
||||
]
|
||||
|
||||
const nbrs = [
|
||||
[0, -1], [1, -1], [1, 0],
|
||||
[1, 1], [0, 1], [-1, 1],
|
||||
[-1, 0], [-1, -1], [0, -1],
|
||||
]
|
||||
|
||||
const nbr_groups = [
|
||||
[[0, 2, 4], [2, 4, 6]],
|
||||
[[0, 2, 6], [0, 4, 6]],
|
||||
]
|
||||
|
||||
|
||||
fn num_neighbors(grid [][]u8, r int, c int) int {
|
||||
mut count := 0
|
||||
for i := 0; i < nbrs.len - 1; i++ {
|
||||
dx := nbrs[i][0]
|
||||
dy := nbrs[i][1]
|
||||
if grid[r + dy][c + dx] == `#` { count++ }
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
fn num_transitions(grid [][]u8, r int, c int) int {
|
||||
mut count := 0
|
||||
for i := 0; i < nbrs.len - 1; i++ {
|
||||
dx1 := nbrs[i][0]
|
||||
dy1 := nbrs[i][1]
|
||||
dx2 := nbrs[i + 1][0]
|
||||
dy2 := nbrs[i + 1][1]
|
||||
if grid[r + dy1][c + dx1] == ` ` && grid[r + dy2][c + dx2] == `#` { count++ }
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
fn at_least_one_is_white(grid [][]u8, r int, c int, step int) bool {
|
||||
mut count := 0
|
||||
group := nbr_groups[step]
|
||||
for i in 0..2 {
|
||||
for j in 0..group[i].len {
|
||||
nbr := nbrs[group[i][j]]
|
||||
if grid[r + nbr[1]][c + nbr[0]] == ` ` {
|
||||
count++
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return count > 1
|
||||
}
|
||||
|
||||
fn thin_image() {
|
||||
mut to_white := []Point{}
|
||||
mut grid := [][]u8{}
|
||||
mut first_step := false
|
||||
mut has_changed := false
|
||||
for line in image {
|
||||
grid << line.bytes() // line.bytes() returns []u8
|
||||
}
|
||||
for {
|
||||
has_changed = false
|
||||
first_step = !first_step
|
||||
for r := 1; r < grid.len - 1; r++ {
|
||||
for c := 1; c < grid[0].len - 1; c++ {
|
||||
if grid[r][c] != `#` { continue }
|
||||
nn := num_neighbors(grid, r, c)
|
||||
if nn < 2 || nn > 6 { continue }
|
||||
if num_transitions(grid, r, c) != 1 { continue }
|
||||
step := if first_step { 0 } else { 1 }
|
||||
if !at_least_one_is_white(grid, r, c, step) { continue }
|
||||
to_white << Point{c, r}
|
||||
has_changed = true
|
||||
}
|
||||
}
|
||||
for p in to_white {
|
||||
grid[p.y][p.x] = ` `
|
||||
}
|
||||
to_white.clear()
|
||||
if !first_step && !has_changed { break }
|
||||
}
|
||||
for row in grid {
|
||||
println(row.bytestr())
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
thin_image()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue