Created
August 8, 2025 05:46
-
-
Save mgritter/67aa9f2bc9e98f1d52afafeab2081697 to your computer and use it in GitHub Desktop.
Enumerate all colorings of the 4x4 grid where no two colored cells touch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import cairo | |
| start = np.zeros( (4,4), dtype=np.int32 ) | |
| configurations = [] | |
| def dfs( y, x, prev, leaf ): | |
| if y >= 4: | |
| leaf( prev ) | |
| return | |
| neighbors = [ | |
| (y-1,x-1), | |
| (y-1,x), | |
| (y-1,x+1), | |
| (y,x-1) | |
| ] | |
| filtered_neighbors = sum( | |
| prev[y][x] for (y,x) in neighbors | |
| if y >= 0 and y < 4 and x >= 0 and x < 4 | |
| ) | |
| next_y = y | |
| next_x = x+1 | |
| if next_x >= 4: | |
| next_y += 1 | |
| next_x = 0 | |
| if filtered_neighbors > 0 : | |
| dfs( next_y, next_x, prev, leaf ) | |
| else: | |
| dfs( next_y, next_x, prev, leaf ) | |
| prev[y][x] = 1 | |
| dfs( next_y, next_x, prev, leaf ) | |
| prev[y][x] = 0 | |
| has_middle = 0 | |
| has_edge = 0 | |
| has_corner = 0 | |
| other = 0 | |
| def count(m): | |
| global has_middle | |
| global has_edge | |
| global has_corner | |
| global other | |
| if m[1][1] + m[1][2] + m[2][1] + m[2][2] > 0: | |
| has_middle += 1 | |
| elif m[0][0] + m[0][3] + m[3][0] + m[3][3] > 0: | |
| has_corner += 1 | |
| elif m[0][1] + m[0][2] + m[1][0] + m[2][0] + m[1][3] + m[2][3] + m[3][1] + m[3][2] > 0: | |
| has_edge += 1 | |
| else: | |
| other += 1 | |
| dfs( 0, 0, start, count ) | |
| print( has_middle, has_edge, has_corner, other ) | |
| cell_width = 4 * 10 + 10 | |
| cell_height = 4 * 10 + 10 | |
| total_width = cell_width * 18 | |
| total_height = cell_height * 18 | |
| def paint_grid( c, y, x, m ): | |
| for i in range( 4 ): | |
| for j in range( 4 ): | |
| c.rectangle( cell_width * x + 5 + j * 10, | |
| cell_height * y + 5 + i * 10, | |
| 10, 10 ) | |
| if m[i][j] == 1: | |
| c.fill() | |
| else: | |
| c.stroke() | |
| def paint(): | |
| surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, total_width, total_height ) | |
| c = cairo.Context(surface) | |
| c.set_source_rgb(1,1,1) | |
| c.paint() | |
| c.set_source_rgb(0,0,0) | |
| c.set_line_width(1) | |
| x = 0 | |
| y = 0 | |
| def callback(m): | |
| nonlocal x | |
| nonlocal y | |
| paint_grid( c, y, x, m ) | |
| x += 1 | |
| if x >= 18: | |
| x = 0 | |
| y += 1 | |
| dfs( 0, 0, start, callback ) | |
| surface.write_to_png( "whiteboard.png" ) | |
| paint() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment