Skip to content

Instantly share code, notes, and snippets.

@su27
Last active December 14, 2016 10:34
Show Gist options
  • Select an option

  • Save su27/0ee08e887923628ba73023bcb54e7d29 to your computer and use it in GitHub Desktop.

Select an option

Save su27/0ee08e887923628ba73023bcb54e7d29 to your computer and use it in GitHub Desktop.

Revisions

  1. su27 revised this gist Dec 14, 2016. 1 changed file with 44 additions and 14 deletions.
    58 changes: 44 additions & 14 deletions comb.py
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,9 @@
    from collections import Counter

    xs = [i for i in range(5) for k in range(5)]
    ys = [i for k in range(5) for i in range(5)]


    def next_z(zs):
    def next_z(ys, zs):
    cur_len = len(zs)
    if cur_len == 25:
    return
    @@ -23,14 +22,50 @@ def next_z(zs):
    yield z


    def queens(zs):
    def next_y(ys):
    cur_len = len(ys)
    if cur_len == 25:
    return

    xy_pairs = set(zip(xs, ys))
    next_x = xs[cur_len]
    nums = Counter(ys)

    avail_y = [y for y in range(5) if nums[y] < 5 and
    (next_x, y) not in xy_pairs]

    for y in avail_y:
    yield y


    def arrange_ys(ys):
    if len(ys) == 25:
    yield ys

    for y in next_y(ys):
    for solution_y in arrange_ys(ys + [y]):
    yield solution_y


    def arrange_zs(ys, zs):
    if len(zs) == 25:
    yield list(zip(xs, ys, zs))

    for i in next_z(zs):
    appended = zs + [i]
    for solution in queens(appended):
    for z in next_z(ys, zs):
    for solution in arrange_zs(ys, zs + [z]):
    yield solution


    def find_queen():
    for ys in arrange_ys([]):
    n = 0
    print('ys:', ys)
    for solution in arrange_zs(ys, []):
    n += 1
    if n // 5000 * 5000 == n:
    print(n)
    yield solution
    print('total:', n)


    def check(qs, n):
    @@ -45,14 +80,9 @@ def check(qs, n):


    if __name__ == '__main__':
    n = 0
    for solution in queens([]):
    for solution in find_queen():
    solution_10 = solution + [(9 - x, 9 - y, 9 - z)
    for x, y, z in solution]
    n += 1
    if n // 1000 * 1000 == n:
    print(n)
    # print ','.join("%s%s%s" % (x, y, z) for x, y, z in solution_10)
    # print(','.join("%s%s%s" % (x, y, z) for x, y, z in solution_10))
    # if not check(solution_10, 10):
    # print '.......'
    print('total', n)
    # print('.......')
  2. su27 revised this gist Dec 14, 2016. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions comb.py
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ def next_z(zs):
    next_y = ys[cur_len]
    nums = Counter(zs)

    avail_z = [z for z in xrange(5) if nums[z] < 5 and
    avail_z = [z for z in range(5) if nums[z] < 5 and
    (next_x, z) not in xz_pairs and
    (next_y, z) not in yz_pairs]

    @@ -25,7 +25,7 @@ def next_z(zs):

    def queens(zs):
    if len(zs) == 25:
    yield zip(xs, ys, zs)
    yield list(zip(xs, ys, zs))

    for i in next_z(zs):
    appended = zs + [i]
    @@ -50,9 +50,9 @@ def check(qs, n):
    solution_10 = solution + [(9 - x, 9 - y, 9 - z)
    for x, y, z in solution]
    n += 1
    if n / 1000 * 1000 == n:
    print n
    # print n, ','.join("%s%s%s" % (x, y, z) for x, y, z in solution_10)
    if n // 1000 * 1000 == n:
    print(n)
    # print ','.join("%s%s%s" % (x, y, z) for x, y, z in solution_10)
    # if not check(solution_10, 10):
    # print '.......'
    print 'total:', n
    print('total', n)
  3. su27 revised this gist Dec 14, 2016. 1 changed file with 20 additions and 11 deletions.
    31 changes: 20 additions & 11 deletions comb.py
    Original file line number Diff line number Diff line change
    @@ -1,28 +1,31 @@
    all_zs = [(i, k) for k in range(5) for i in range(5)]
    xs = [(i, k) for i in range(5) for k in range(5)]
    ys = [(i, k) for k in range(5) for i in range(5)]
    from collections import Counter

    xs = [i for i in range(5) for k in range(5)]
    ys = [i for k in range(5) for i in range(5)]


    def next_z(zs):
    cur_len = len(zs)
    if cur_len == 25:
    return

    yz_pairs = set(zip([v for v, n in ys], [v for v, n in zs]))
    xz_pairs = set(zip([v for v, n in xs], [v for v, n in zs]))
    yz_pairs = set(zip(ys, zs))
    xz_pairs = set(zip(xs, zs))
    next_x = xs[cur_len]
    next_y = ys[cur_len]
    avail_z = [z for z in all_zs if z not in zs and
    (next_x[0], z[0]) not in xz_pairs and
    (next_y[0], z[0]) not in yz_pairs]
    nums = Counter(zs)

    avail_z = [z for z in xrange(5) if nums[z] < 5 and
    (next_x, z) not in xz_pairs and
    (next_y, z) not in yz_pairs]

    for z in avail_z:
    yield z


    def queens(zs):
    if len(zs) == 25:
    yield zip([v for v, num in xs], [v for v, num in ys], [v for v, num in zs])
    yield zip(xs, ys, zs)

    for i in next_z(zs):
    appended = zs + [i]
    @@ -44,6 +47,12 @@ def check(qs, n):
    if __name__ == '__main__':
    n = 0
    for solution in queens([]):
    solution_10 = solution + [(9 - x, 9 - y, 9 - z) for x, y, z in solution]
    solution_10 = solution + [(9 - x, 9 - y, 9 - z)
    for x, y, z in solution]
    n += 1
    print n, ','.join("%s%s%s" % (x, y, z) for x, y, z in solution_10)
    if n / 1000 * 1000 == n:
    print n
    # print n, ','.join("%s%s%s" % (x, y, z) for x, y, z in solution_10)
    # if not check(solution_10, 10):
    # print '.......'
    print 'total:', n
  4. su27 revised this gist Dec 14, 2016. 1 changed file with 3 additions and 6 deletions.
    9 changes: 3 additions & 6 deletions comb.py
    Original file line number Diff line number Diff line change
    @@ -44,9 +44,6 @@ def check(qs, n):
    if __name__ == '__main__':
    n = 0
    for solution in queens([]):
    ch = check(solution, 5)
    if ch:
    solution_10 = solution + [(9 - x, 9 - y, 9 - z) for x, y, z in solution]
    if check(solution_10, 10):
    n += 1
    print n, ','.join("%s%s%s" % (x, y, z) for x, y, z in solution_10)
    solution_10 = solution + [(9 - x, 9 - y, 9 - z) for x, y, z in solution]
    n += 1
    print n, ','.join("%s%s%s" % (x, y, z) for x, y, z in solution_10)
  5. su27 created this gist Dec 14, 2016.
    52 changes: 52 additions & 0 deletions comb.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    all_zs = [(i, k) for k in range(5) for i in range(5)]
    xs = [(i, k) for i in range(5) for k in range(5)]
    ys = [(i, k) for k in range(5) for i in range(5)]


    def next_z(zs):
    cur_len = len(zs)
    if cur_len == 25:
    return

    yz_pairs = set(zip([v for v, n in ys], [v for v, n in zs]))
    xz_pairs = set(zip([v for v, n in xs], [v for v, n in zs]))
    next_x = xs[cur_len]
    next_y = ys[cur_len]
    avail_z = [z for z in all_zs if z not in zs and
    (next_x[0], z[0]) not in xz_pairs and
    (next_y[0], z[0]) not in yz_pairs]

    for z in avail_z:
    yield z


    def queens(zs):
    if len(zs) == 25:
    yield zip([v for v, num in xs], [v for v, num in ys], [v for v, num in zs])

    for i in next_z(zs):
    appended = zs + [i]
    for solution in queens(appended):
    yield solution


    def check(qs, n):
    tried = set()
    for x, y, z in qs:
    for i in range(n):
    tried.add((i, y, z))
    tried.add((x, i, z))
    tried.add((x, y, i))
    if len(tried) == n ** 3:
    return True


    if __name__ == '__main__':
    n = 0
    for solution in queens([]):
    ch = check(solution, 5)
    if ch:
    solution_10 = solution + [(9 - x, 9 - y, 9 - z) for x, y, z in solution]
    if check(solution_10, 10):
    n += 1
    print n, ','.join("%s%s%s" % (x, y, z) for x, y, z in solution_10)