September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,41 +0,0 @@
|
|||
# Calculate a zig-zag pattern of numbers like so:
|
||||
# 0 1 5
|
||||
# 2 4 6
|
||||
# 3 7 8
|
||||
#
|
||||
# There are many interesting ways to solve this; we
|
||||
# try for an algebraic approach, calculating triangle
|
||||
# areas, so that me minimize space requirements.
|
||||
|
||||
zig_zag_value = (x, y, n) ->
|
||||
|
||||
upper_triangle_zig_zag = (x, y) ->
|
||||
# calculate the area of the triangle from the prior
|
||||
# diagonals
|
||||
diag = x + y
|
||||
triangle_area = diag * (diag+1) / 2
|
||||
# then add the offset along the diagonal
|
||||
if diag % 2 == 0
|
||||
triangle_area + y
|
||||
else
|
||||
triangle_area + x
|
||||
|
||||
if x + y < n
|
||||
upper_triangle_zig_zag x, y
|
||||
else
|
||||
# For the bottom right part of the matrix, we essentially
|
||||
# use reflection to count backward.
|
||||
bottom_right_cell = n * n - 1
|
||||
n -= 1
|
||||
v = upper_triangle_zig_zag(n-x, n-y)
|
||||
bottom_right_cell - v
|
||||
|
||||
zig_zag_matrix = (n) ->
|
||||
row = (i) -> (zig_zag_value i, j, n for j in [0...n])
|
||||
(row i for i in [0...n])
|
||||
|
||||
do ->
|
||||
for n in [4..6]
|
||||
console.log "---- n=#{n}"
|
||||
console.log zig_zag_matrix(n)
|
||||
console.log "\n"
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
> coffee zigzag.coffee
|
||||
---- n=4
|
||||
[ [ 0, 1, 5, 6 ],
|
||||
[ 2, 4, 7, 12 ],
|
||||
[ 3, 8, 11, 13 ],
|
||||
[ 9, 10, 14, 15 ] ]
|
||||
|
||||
|
||||
---- n=5
|
||||
[ [ 0, 1, 5, 6, 14 ],
|
||||
[ 2, 4, 7, 13, 15 ],
|
||||
[ 3, 8, 12, 16, 21 ],
|
||||
[ 9, 11, 17, 20, 22 ],
|
||||
[ 10, 18, 19, 23, 24 ] ]
|
||||
|
||||
|
||||
---- n=6
|
||||
[ [ 0, 1, 5, 6, 14, 15 ],
|
||||
[ 2, 4, 7, 13, 16, 25 ],
|
||||
[ 3, 8, 12, 17, 24, 26 ],
|
||||
[ 9, 11, 18, 23, 27, 32 ],
|
||||
[ 10, 19, 22, 28, 31, 33 ],
|
||||
[ 20, 21, 29, 30, 34, 35 ] ]
|
||||
Loading…
Add table
Add a link
Reference in a new issue