Colorize output%

This commit is contained in:
Dominic Zimmer 2021-07-22 12:28:39 +02:00
parent fc3c3417e4
commit 0ca36c2f93
5 changed files with 71 additions and 18 deletions

View File

@ -4,6 +4,27 @@ from typing import Union, Iterator
from random import choices, choice from random import choices, choice
from math import prod from math import prod
ansii_red = "\u001b[31;1m"
ansii_red_bg = "\u001b[41;1m"
ansii_green = "\u001b[32;1m"
ansii_green_bg = "\u001b[42;1m"
ansii_yellow = "\u001b[33;1m"
ansii_yellow_bg = "\u001b[43;1m"
ansii_blue = "\u001b[34;1m"
ansii_blue_bg = "\u001b[44;1m"
ansii_magenta = "\u001b[35;1m"
ansii_magenta_bg = "\u001b[45;1m"
ansii_reset = "\u001b[0m"
thermo_colors = [
[ ansii_red, ansii_red_bg ],
[ ansii_yellow, ansii_yellow_bg ],
[ ansii_blue, ansii_blue_bg ],
[ ansii_magenta, ansii_magenta_bg ]
]
checker_colors_even = [ ansii_green_bg, ansii_green_bg ]
checker_colors_odd = [ ansii_reset ]
@dataclass(frozen=True, repr=False) @dataclass(frozen=True, repr=False)
class Cell: class Cell:
row: int row: int
@ -28,18 +49,18 @@ class SolvedCell(FilledCell):
@dataclass(frozen=True, repr=False) @dataclass(frozen=True, repr=False)
class EmptyCell(Cell): class EmptyCell(Cell):
def __repr__(self): def __repr__(self):
return "." parity = ((self.row // 3) + (self.col // 3)) % 2 == 0
color = checker_colors_odd[0] if parity else checker_colors_even[0]
return f"{color}.{ansii_reset}"
@dataclass(frozen=True, repr=False) @dataclass(frozen=True, repr=False)
class UninitializedCell(): class Thermo(Cell):
def __repr__(self):
return "."
@dataclass(frozen=True, repr=False)
class Thermo:
value: int value: int
color: int
def __repr__(self): def __repr__(self):
return f"{self.value}" parity = ((self.row // 3) + (self.col // 3)) % 2
color = thermo_colors[self.color][parity]
return f"{color}{self.value}{ansii_reset}"
@dataclass(frozen=True) @dataclass(frozen=True)
class Link: class Link:
@ -50,7 +71,7 @@ def pp(c: Cell):
if isinstance(c, GivenCell): if isinstance(c, GivenCell):
return f"{c.value!s}" return f"{c.value!s}"
if isinstance(c, SolvedCell): if isinstance(c, SolvedCell):
return f"\u001b[32;1m{str(c.value)}\u001b[0m" return f"{ansii_green}{str(c.value)}{ansii_reset}"
if isinstance(c, EmptyCell): if isinstance(c, EmptyCell):
return " " return " "
return "_" return "_"
@ -168,11 +189,8 @@ for value in range(8,0, -1):
extended_paths : list[Path] = [ new_path + path for path in paths[neighbor]] extended_paths : list[Path] = [ new_path + path for path in paths[neighbor]]
all_paths = [new_path] + extended_paths all_paths = [new_path] + extended_paths
paths[cell].extend(all_paths) paths[cell].extend(all_paths)
# for path in paths[cell]:
# print(f"{cell} has path {pP(path)}")
all_paths : list[Path] = [ path for cell in all_cells(solution) for path in paths[cell] ] # all_paths : list[Path] = [ path for cell in all_cells(solution) for path in paths[cell] ]
print(len(all_paths))
def get_cells(path: Path) -> Iterator[FilledCell]: def get_cells(path: Path) -> Iterator[FilledCell]:
yield path[0].fr yield path[0].fr
@ -194,7 +212,7 @@ def ilike(path: Path) -> bool:
max_one_solved = len(given_cells) < 2 max_one_solved = len(given_cells) < 2
return max_one_solved return max_one_solved
hints : list[list[Thermo | Cell]]= empty_grid() hints : list[list[Thermo | Cell]] = empty_grid()
used_cells: set[FilledCell] = set() used_cells: set[FilledCell] = set()
num_paths = 4 num_paths = 4
while num_paths > 0: while num_paths > 0:
@ -206,12 +224,11 @@ while num_paths > 0:
for index, cell in enumerate(cells): for index, cell in enumerate(cells):
used_cells.add(cell) used_cells.add(cell)
row, col = cell.row, cell.col row, col = cell.row, cell.col
hints[row][col] = Thermo(index) hints[row][col] = Thermo(row, col, index, num_paths - 1)
num_paths -= 1 num_paths -= 1
break break
#out = [ " ".join(list(map(str, row))) for row in hints ]
strs = [ " ".join([str(entry) for entry in row]) for row in hints] strs = [ " ".join([str(entry) for entry in row]) for row in hints]
print("\n".join(strs)) print("\n".join(strs))

9
sudokus/15.in Normal file
View File

@ -0,0 +1,9 @@
004301007
730090000
008040050
000900070
403080106
060004000
020060500
000050023
800203400

9
sudokus/16.in Normal file
View File

@ -0,0 +1,9 @@
000043000
004000803
010807050
803060109
020000080
109080502
050406010
901000300
000950000

9
sudokus/17.in Normal file
View File

@ -0,0 +1,9 @@
001000005
000376200
200000060
905800020
000615000
070002506
090000004
004581000
800000700

9
sudokus/18.in Normal file
View File

@ -0,0 +1,9 @@
040300009
100000006
003021000
030100200
009008050
010040080
000610400
500000001
900002030