Skip to content

Instantly share code, notes, and snippets.

@Mic92
Created February 28, 2019 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mic92/b3ae36be2dc09db47767eff699981101 to your computer and use it in GitHub Desktop.
Save Mic92/b3ae36be2dc09db47767eff699981101 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import itertools
import os
import sys
from typing import *
if sys.version_info < (3, 6):
print("Program requires least Python 3.6", file=sys.stderr)
sys.exit(1)
try:
import numpy as np
except ImportError:
print("numpy required to run this program!", file=sys.stderr)
sys.exit(1)
try:
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use("GTK3Agg")
HAS_MATPLOTLIB = True
except:
HAS_MATPLOTLIB = False
class Image:
def __init__(self, id: int, orientation: str, tags: List[str]):
self.id = id
self.orientation = orientation
self.tags = tags
def __repr__(self) -> str:
return f"Image<%d, %s, %s>" % (self.id, self.orientation, " ".join(self.tags))
# 3 5 1 6
# TTTTT
# TMMMT
# TTTTT
def parse_input(name: str) -> List[Image]:
with open(name) as f:
lines = f.readlines()
photos = int(lines[0])
images: List[Image] = []
for id, line in enumerate(lines[1:]):
fields = line.strip().split(" ")
orientation = fields[0]
tags = fields[2:]
images.append(Image(id, orientation, tags))
return images
def write_result(filename: str, slides: List[List[int]]) -> None:
with open(filename, "w") as f:
f.write("%d\n" % len(slides))
for row in slides:
for col in row:
f.write("%d" % col)
f.write("\n")
def draw_matrix(matrix: np.array, annotation: np.array) -> None:
if not HAS_MATPLOTLIB:
return
fig, ax = plt.subplots()
bar = ax.matshow(matrix)
for (i, j), z in np.ndenumerate(annotation):
ax.text(
j,
i,
"{:0.1f}".format(z),
ha="center",
va="center",
bbox=dict(boxstyle="round", facecolor="white", edgecolor="0.3"),
)
plt.colorbar(bar, ax=ax)
plt.show()
class Slide:
def __init__(self, a: Image, b: Image):
self.a = a
self.b = b
def __hash__(self):
return hash((self.a, self.b))
def __eq__(self, other) -> bool:
return self.a.id == other.a.id and self.b.id == other.b.id
def __repr__(self) -> str:
return f"Slide<(%s, %s)>" % (self.a, self.b)
def algorithm(input: List[Image]) -> List[List[int]]:
verticals = list(filter(lambda h: h.orientation == "V", input))
perms: Set[Slide] = set()
for perm in itertools.permutations(verticals, 2):
if perm[0].id > perm[1].id:
s = Slide(perm[0], perm[1])
else:
s = Slide(perm[1], perm[0])
perms.add(s)
breakpoint()
return []
def main():
if len(sys.argv) < 3:
print("USAGE: %s input output" % sys.argv[0], file=sys.stderr)
sys.exit(1)
input_filename, output_filename = sys.argv[1], sys.argv[2]
input = parse_input(input_filename)
# draw_matrix(input.matrix, input.matrix)
output = algorithm(input)
write_result(output_filename, output)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment