From 84f97bc5c0de458656626ecb810c73eaabf16bb5 Mon Sep 17 00:00:00 2001 From: Roman Hergenreder Date: Mon, 9 Dec 2019 15:09:46 +0100 Subject: [PATCH] Day 09 solved --- Day 09/decode.py | 94 +++++++++++++++++++++++++++++++++++++++++++++ Day 09/decoded.png | Bin 0 -> 1022 bytes Day 09/qr.png | Bin 0 -> 527 bytes 3 files changed, 94 insertions(+) create mode 100644 Day 09/decode.py create mode 100644 Day 09/decoded.png create mode 100644 Day 09/qr.png diff --git a/Day 09/decode.py b/Day 09/decode.py new file mode 100644 index 0000000..759ab34 --- /dev/null +++ b/Day 09/decode.py @@ -0,0 +1,94 @@ +#!/usr/bin/python + +from PIL import Image +import numpy + +im = Image.open("qr.png") +input = im.load() + +# QR Version 4 +size = 33 +pixelSize = im.size[0] // size + +# rule 30 +codes = { + "111": 0, + "110": 0, + "101": 0, + "100": 1, + "011": 1, + "010": 1, + "001": 1, + "000": 0 +} + + +def translatePos(pos): + x = pos[0] * pixelSize + pixelSize // 2 + y = pos[1] * pixelSize + pixelSize // 2 + return (x, y) + +def fillPixel(pos, bit, pix): + color = (0,0,0) if bit == 1 else (255,255,255) + for x in range(5): + for y in range(5): + pix[pos[0]*pixelSize+x,pos[1]*pixelSize+y] = color + +def readPos(pos, pix, size): + if pos[0] < 0 or pos[0] >= size[0] or pos[1] < 0 or pos[1] >= size[1]: + return 0 + + translated = translatePos(pos) + return 0 if pix[translated] == 255 else 1 + +def readBufPos(pos, buf): + x = pos[0] + y = pos[1] + + if x < 0 or x >= buf.shape[0] or y < 0 or y >= buf.shape[1]: + return 0 + + return int(buf[x,y]) + +def rule30decode(x, y, buf): + bits = "".join([str(readBufPos(p, buf)) for p in [(x - 1, y - 1), (x, y - 1), (x + 1, y - 1)]]) + return codes[bits] + + +# Used to save rule30 pyramid for visualization +def bufToImage(buf, filename, pixelSize = 5): + imgSize = (buf.shape[0] * pixelSize, buf.shape[1] * pixelSize) + outImage = Image.new('RGB', imgSize, 255) + outPixel = outImage.load() + for x in range(0, buf.shape[0]): + for y in range(0, buf.shape[1]): + color = (0,0,0) if int(buf[x,y]) == 1 else (255,255,255) + for i in range(pixelSize): + for j in range(pixelSize): + outPixel[x*pixelSize+i,y*pixelSize+j] = color + + outImage.save(filename) + +# 33 + 33/2 + 2 +bufSize = 51 +buf = numpy.zeros(shape=(bufSize,bufSize)) +buf[buf.shape[0]//2,0] = 1 + +for y in range(1,buf.shape[1]): + for x in range(0,buf.shape[0]): + buf[x,y] = rule30decode(x,y,buf) + +# bufToImage(buf, "rule30.png") + +offsetX = (bufSize-33)//2-1 +offsetY = 0 + +outImage = Image.new('RGB', im.size, 255) +outPix = outImage.load() +for x in range(size): + for y in range(size): + bit1 = readPos((x,y), input, im.size) + bit2 = int(buf[offsetX+x,offsetY+y]) + bit = bit1 ^ bit2 + fillPixel((x,y), bit, outPix) +outImage.save("decoded.png") diff --git a/Day 09/decoded.png b/Day 09/decoded.png new file mode 100644 index 0000000000000000000000000000000000000000..75f699dadc3115200a3f8ff9bf164d265329373f GIT binary patch literal 1022 zcmeAS@N?(olHy`uVBq!ia0vp^OF@`}2}o}8T-D3Kz>kZs!WPE6F>ALZDGFHFA%|h z_PSGL@UkYeupZ<1$;@EoFJ>S0-S=&RPV&MJC3T6Du9l}SxX|?RtkZp+FCgMtP41gT zkDN5}Pl+7mGTwAyZsVcV+>$*|1AnCz&Rw-&?k(Fl+B3g?71TU}uxq_f_U6Cs+>+TV zuT6JN%QH%e2s4Aa@WpIbop|oR9i2PlEuGbRrMx#ey@@}J&>DV!=9=K8t2dftkujAmiJe!U%FJ%9nL#wAt9&_`VJ4-L> zM;OBO9$!zteeG^$=4bL{68G||32zMJC-Z~j-8d`Dr8j@ZA zvg0pac%=B4@0VUkid_zhRi$gU6{jStUvcu*o#SQc%()T~b3wlsntR(P#~ZoC7vJ6Z z?qD@iq_rBgZsdGEbE#422A|}6!HdJ4*CPeVso!ifmwuZgwZ$$>>CA&Y;ZJ zx02RD@jvdiUMT1OZsK1mhltaoU(W=m)v9j(+ia9SHBslQF+8@9mY=DPf3dn)bXk6@ zXDZwI^$RaTqO^N9FdRMIA4+a%&pDko?{@p&qKnW(>6&w|TE3QbS%BIN^Rj*Uo5C|v z5lLL;^z~ogRK@*F?WD8UNuP4)g(t_2&qU7KINO_TuKj;^pH$?-pmU2S!NS<^di)xL td@jSV0O{Ay1*>>pXJ>$uI56eN$9&ouq8s-84KQCbc)I$ztaD0e0sspB0~r7S literal 0 HcmV?d00001 diff --git a/Day 09/qr.png b/Day 09/qr.png new file mode 100644 index 0000000000000000000000000000000000000000..eb1d5fe27616940afb254c42b8f5032b2d7a87fa GIT binary patch literal 527 zcmV+q0`UEbP)Sy@r6~TR4Z7NzZwTr7r zvK{F2*fwId(r_ypNK&1slF}|{PuJSdTpYmXvGuHhN?WATF4#`g#7?S3nc7mN7IpZ1 zn!E482lwnJ?qAaQZ5yw7Vr@GYSJCQH!wDJ_cS#Q#ZhMlfH0aD!$&+nBhu6?a66Z92 z&+8|xAF;q+s8jA?%x4O-@Z;($x+eySj`W?eODzMay%|w&r=;hZn$mfTD2!t zmA2G*D(P7(WDRUf*E;v&JlPLo<=5@xC9HIA zR(o!7;y