This file contains 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 re | |
import sys | |
from PIL import Image | |
with open("data.txt") as f: | |
lines = f.readlines() | |
"""u = -3 * x + 95 * y; z = -0.12652202789462033 + 0.006530883329643569 * i | |
v = -65 * x + 5 * y; z = -0.16588235294117648 + 0.04352941176470588 * i""" | |
PAT = re.compile(r"(u|v) = ([0-9-]+) \* x \+ ([0-9-]+) \* y; z = ([0-9-.]+) \+ ([0-9-.]+) \* i") | |
class Case(): | |
def __init__(self, var, x, y, zx, zy): | |
self.var = var | |
self.x = x | |
self.y = y | |
self.zx = zx | |
self.zy = zy | |
def __str__(self): | |
return f"{self.var} = {self.x} * x + {self.y} * y; z = {self.zx} + {self.zy} * i" | |
def solve(self): | |
if self.var == "u": | |
return self.solve_u() | |
else: | |
return self.solve_v() | |
def solve_u(self): | |
a, b = self.x, self.y | |
x, y = self.zx, self.zy | |
return (a * x + b * y, a * y - b * x) | |
def solve_v(self): | |
a, b = self.x, self.y | |
x, y = self.zx, self.zy | |
return (b * x - a * y, a * x + b * y) | |
cases = [] | |
for l in lines: | |
m = PAT.match(l) | |
if not m: | |
raise Exception(f"Didn't match {l}") | |
cases.append(Case(m.group(1), int(m.group(2)), int(m.group(3)), float(m.group(4)), float(m.group(5)))) | |
solns = [case.solve() for case in cases] | |
pts = [(round(x), round(y)) for x, y in solns] | |
w = max(pt[0] for pt in pts) + 1 | |
h = max(pt[1] for pt in pts) + 1 | |
im = Image.new(mode="1", size=(w, h)) | |
for pt in pts: | |
im.putpixel(pt, 1) | |
im.save("flag.png", format="png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment