Skip to content

Instantly share code, notes, and snippets.

@JoaoFelipe
Last active July 12, 2018 14:05
Show Gist options
  • Save JoaoFelipe/1080ab52a3a94d99f82a2e3a74b3c2ca to your computer and use it in GitHub Desktop.
Save JoaoFelipe/1080ab52a3a94d99f82a2e3a74b3c2ca to your computer and use it in GitHub Desktop.
from itertools import product, tee
template = ('D','D')
max_operations = 4
pairwise = lambda iterable: (lambda x: [next(x[1], None), zip(*x)])(tee(iterable))[-1]
REVERSED_TILES = set(map((lambda x: tuple(reversed(x))), BASIC_TILES))
[
answer
for i in range(1, max_operations + 1)
for answer in product(BASIC_TILES | REVERSED_TILES, repeat=i)
if all(first[1] == second[0] for first,second in pairwise(answer))
if answer[0][0] == template[0]
if answer[-1][1] == template[-1]
]
from functools import reduce
simulate = lambda template, max_operations, BASIC_TILES: (lambda canvas, sort: ####
print('Ranking of the correct answer in the suggestions compatibl' 'e' ' with ['##
'{}' ']:' '{}' '\nR' 'ank' ' o'##
'f ' 'pos' 'sible compati' 'ble' ' a' 'nswers:{})' . format##
( '|' .join( template ), len( canvas ) , sort(canvas##
) ))) ( (( lambda canvas : {(tile, op_count) for tile,###
op_count in canvas if op_count == min(other for other_tile###
,other in canvas if tile == other_tile ) if tile[0]###
== template [0]and tile [-1] == template[-1] })( reduce###
(( lambda canvas, max_op_count: canvas | {( (tile[::-1]),
op_count +1 ) for ( tile , op_count ) in canvas if op_count <##
max_op_count } | {( (tile + other_tile),
op_count + other + 1) for (tile, op_count ) in
canvas for (other_tile, other) in canvas if tile[-1] == other_tile[0] if op_count
+ other < max_op_count} | { ( #######
(other_tile + tile ), op_count + other + 1) for ( tile ,
op_count ) in canvas for (other_tile , other )in canvas if
other_tile [-1] == tile[0] if op_count + other <max_op_count
}), range(1 ,max_operations + 1),{(tile,0 ) for tile in #####
BASIC_TILES }))), (lambda canvas: [x[0] for x in sorted (
canvas , key=lambda x: x[1]) ])) ##### #############
########### ######### ############# ############## #############
########### ############# #############
###################################################################################
BASIC_TILES = {('Cl','M'), ('F','Cl'), ('C','M'), ('D','C'), ('P','F'), ('I','C')}
template = ('D','D')
max_operations = 4
simulate(template, max_operations, BASIC_TILES)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment