Last active
June 16, 2016 09:38
-
-
Save Highstaker/09913478cdb10977272a115586d3ba2a to your computer and use it in GitHub Desktop.
Daily challenges from https://www.reddit.com/r/dailyprogrammer/
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
# https://www.reddit.com/r/dailyprogrammer/comments/4m3ddb/20160601_challenge_269_intermediate_mirror/ | |
import numpy as np | |
def decrypt(inp): | |
mirrors = np.array(list(map(list ,MIRRORS.split('\n')))) | |
TOP = [chr(i) for i in range(97,110)] | |
RIGHT = [chr(i) for i in range(110,123)] | |
LEFT = [chr(i) for i in range(65,78)] | |
BOTTOM = [chr(i) for i in range(78,91)] | |
result = "" | |
state = "" | |
def init_state(ch): | |
if ch in TOP: | |
return "down", [TOP.index(ch), -1] | |
elif ch in RIGHT: | |
return "left", [13 , RIGHT.index(ch) ] | |
elif ch in LEFT: | |
return "right", [-1, LEFT.index(ch)] | |
elif ch in BOTTOM: | |
return "up", [BOTTOM.index(ch) ,13] | |
for letter in inp: | |
state, coords = init_state(letter) | |
while True: | |
if state == "up": | |
for n, i in enumerate(mirrors.T[coords[0]][coords[1]-13-1::-1]): | |
if i == "\\": | |
state = "left" | |
coords[1] -= n + 1 | |
break | |
elif i == "/": | |
state = "right" | |
coords[1] -= n + 1 | |
break | |
else: | |
result += TOP[coords[0]] | |
break | |
elif state == "down": | |
for n, i in enumerate(mirrors.T[coords[0]][coords[1]+1:]): | |
if i == "\\": | |
state = "right" | |
coords[1] += n + 1 | |
break | |
elif i == "/": | |
state = "left" | |
coords[1] += n + 1 | |
break | |
else: | |
result += BOTTOM[coords[0]] | |
break | |
elif state == "left": | |
for n, i in enumerate(mirrors[coords[1]][coords[0]-13-1::-1]): | |
if i == "\\": | |
state = "up" | |
coords[0] -= n + 1 | |
break | |
elif i == "/": | |
state = "down" | |
coords[0] -= n + 1 | |
break | |
else: | |
result += LEFT[coords[1]] | |
break | |
elif state == "right": | |
for n, i in enumerate(mirrors[coords[1]][coords[0]+1:]): | |
if i == "\\": | |
state = "down" | |
coords[0] += n + 1 | |
break | |
elif i == "/": | |
state = "up" | |
coords[0] += n + 1 | |
break | |
else: | |
result += RIGHT[coords[1]] | |
break | |
return result | |
MIRRORS = r""" \\ /\ | |
\ | |
/ | |
\ \ | |
\ | |
/ / | |
\ / \ | |
\ | |
\/ | |
/ | |
\ | |
\/ | |
/ / """ | |
inp = "TpnQSjdmZdpoohd" | |
result = decrypt(inp) | |
print("inp ", inp) | |
print("result ", result) | |
assert decrypt("TpnQSjdmZdpoohd") == "DailyProgrammer" |
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
#https://www.reddit.com/r/dailyprogrammer/comments/4nga90/20160610_challenge_270_hard_alien_invasion/ | |
INPUTS = [ | |
"""--X----X | |
-----X-- | |
X--X---- | |
--X----- | |
X--X---- | |
XXXX---- | |
--X----- | |
--X---X-""" | |
, | |
"""X--X- | |
----- | |
----- | |
----- | |
---X-""" | |
, | |
"""--X---------X----------------------X---XXX-------- | |
-X----------X-------------------XX------XX--XX--X- | |
---------X---X--X-------XX----------------------X- | |
----------------------X----X---XX---X-------X----X | |
------------------X----------X--------X----------X | |
----XX---X----------------X---X-X----------------- | |
---X----------------------------------X-------X--- | |
-X-------X--XX----------X----X--X--X----------X--- | |
---------X---------------X----------------------X- | |
-------------X------------------------X----------- | |
-X---------------------------XX----------X-------- | |
--X--------------------X-X--------------------X--- | |
X---X-----------X-------------X------------X------ | |
---X-----------------------X-------------------X-- | |
-------X--------------X--X-----X------------------ | |
--------X------X------X----------XXX----X--X---X-X | |
------------------X-------X----X--X--------------- | |
----X----X----------------------------------X----- | |
-----------X-----X--------X--------X-------------- | |
--X------X-------------X--------------------X----- | |
------X----X----------------------X---------XX---- | |
----XX----------X-----------------X---------X-X--- | |
-----X------X------X----X-------XXX-X------------- | |
---X-X-------------------------------------------- | |
-----------------X----------------X--------------- | |
----X-------------X----------------------X--X----- | |
------X---------XX--X---------X--------X---------- | |
XX--------X-----------------X-------------X---X--- | |
-----------X-X--------X---X--------X---X--------X- | |
-------------------X-------XXX---X---------------- | |
-------X-----X-------------------------------X---- | |
----X------X------X--X---------------X------------ | |
--------X--------X--------------X----------------- | |
--------X------------------X-------X--------X---X- | |
X--X--X---X------X----------X--X--X--------------- | |
-----------X--X-------------X-----------------X--- | |
--------------X-------X-----X-----------------X--- | |
-----------------X----------------------X-X--X---- | |
----------------X----------------------------X---- | |
---------------X----------X-----------X----------- | |
---X-X-----------------XX----X-----XX------------X | |
--X-X-------------------X-------------------X--X-- | |
--X------X---------------------------------------- | |
----X-------------------------X---X--------X-----X | |
X--XX----X-------------X---X---------------------- | |
---------X---------------------------------------X | |
X-------X---------X--X-----XX--------------X------ | |
------XX---------X--X---------X------------------- | |
--------X----------X---X--X-----X------X-----X---- | |
----------------------------------------------X---""" | |
, | |
"""--------------X------X-----------X-X---X--X--X----X--------X--------------------X------------------- | |
-------------------X-X--------------------------------X-----------------X---------X-----X----------- | |
------------X------X------------------------X----X--------XXX---------------X---------------------XX | |
-----------------------------X-------------------------------X-----------X-----X------------X--X---- | |
---------------------------X-------------XX------------XX---X-X--X----------X---X---X------X-------- | |
----------------X--X---------------X--------X-X--X---------------X--------------------X------------- | |
--XX------X-X--------X-----------XX-X-----X-------XX--------X-X----------------------------------X-- | |
---------X-------X-------------------X---------XX---------------X----------X-X-X-----------X-------X | |
-X---------------X-X--------X--X---X-----------------------------------------X-X---X-----X-X----XXXX | |
------X----------------------------------X--X----X--X--X--------------X------X---------X------X-X--- | |
-X---X--------X---------------XX----------------X-----------X--X-X--------------X--X-----X---------- | |
X-X----X-----------------X-X---------------------X----------X-------------------X-----------X------- | |
-X-----X---XX------X--X---------X--XX----X----X--------------XXX--X-------------X-------X--X----X--- | |
-----X-------------X-X---------------X---------------------X---------XX----------------------------- | |
X--------------------------------------X-XX---------------X----------------------------------------X | |
-X------------X-------X--X-----------X-------X------X----------X-------------------X------X-X----X-- | |
X-----X--X-X------------------X--------X---------------X--------X----------------------------------- | |
-------------X--------X------------------------X-XX---------------X-XX------------X----------------- | |
---X--------------------X--X--X---------X-------X-------------X----X------------X--X---X------------ | |
--------------------X-----------X--------------------X------------------------------------------X--- | |
-------------X----X----X-------X--------X-X-----X-XX-------------------X-XX----X---------X---X----X- | |
---------------XX--------------X----------------------X---X-------------X-----------X---------X----- | |
---X------------------------X-------------------------X--X-X---------------X----X------------------- | |
-----------------X-----------------X---------X---------X--------------X-------X----------X---------- | |
-------------X--------X--------X--------------------------------X--X-----X----X--------------------- | |
----X----X--X--------X-----X----------------------------X----------X-----------X-X-------X---------- | |
-XX---X----------------X---------X-X-----X-----------------X---X---------X--------------X-XX---X---- | |
-------------X-----XX-------X----X-------X------------X-------X-X----X-----X-X-------------------XX- | |
-----X------------X-----X---------------X----------X----------X--XX-------X-X-----X-----X----------- | |
--------X----X----X------X--------------X-------X----------X---X---X---X--------------X--X---------- | |
---------X-----------------------XX--X-----------X-----------XX--------------X--------------X------- | |
X-----------------------------------X------X-----------------------------------------------XX-----X- | |
-------X---------X-----X-------------X-----X-----------X----X-------------------X-----X------X------ | |
X--------------------------------------------------------------------------------X-----X------------ | |
-------X------------------------X-----X-X--X------X----------XX--X-------------X-------------------X | |
--------------X-X---------X------------X-----------X-------------X----------X-------X--------------- | |
X-X-X-------------X-----------------------X---X---X--X--------XX---X-----------X---------X---------- | |
-----------X--------------XXX----XX------------------------------------X--X--X-X-------X-X---------X | |
-------------------------X-----------------X----------------------XX--X--X------X-------------X----- | |
--X-X---X-X----------------X---X-------X------XX-----------------X---X--X-------------X-X---X------X | |
------X-----X--------X---X----X------------------------X---X----X-------------------X--------------- | |
---------------X-----------X-----X---------X-------------X---------X----X----------X---X--X-X--X---- | |
---------------------X--------X--X-X--------------------------X----------X----------------------X--- | |
X---X---X--X----------X------------X----------X-X------X-------------------X------X------------X---- | |
----X---X---X-X--X-------------------X---------X---X---X------X----------------X------X------------- | |
-X-------X-------X----------------------X----------------X--------X-----X----------------------XX--X | |
---------XX-X---X--X-----------X--X--------------X-------------X--------------X----X---------------- | |
-----------------------X------------X------------------------X------X-----------X-----XX-----X----X- | |
---------------------X---X----XX---------X---------------------------------X--X---X-----------X---X- | |
-----X----X-----X-X-------------X-X-----------X--X-------XX--------------X----X-X----XXX---X-------- | |
-----------------X-X-------------X-----X-------X-XX-------------X----------------X-X----------X----- | |
------------X-X----------X---------------------------------X------X--------X---X---X---------------- | |
--X------------------------X---------X---------------X------X-------X-----XX--------X----X-------X-- | |
---XXX--------------------------X------X-----X-----------X-------------------X--------XX----------X- | |
---------X----X-------X-X-X---------------X-X----X--------------X-----X----------X----------------X- | |
--------X--X-X---------X------------------X---------X---------------------X----------------X-------- | |
-----------------X------------------X----------X---X---XX-----X---X--------------------------------X | |
---------------------X-X----------XX--XX-----X-------X--X-----X---------X-X---------X---X----------- | |
-----X-------------X-X--------X----------X-----------------X-X-----X----------X----X----------X---X- | |
-------------------X-------X--X---X--X--X----------X-----------------------X--------X--X-------X---X | |
----X------X------XX------X---------------------------X--X----------------X---------------X------X-- | |
--X---X------X----------X------------X-X-X-----------X-X---------------X----X------------X---------- | |
-------X--X-----XX----------X-----------------------------------X---------------------------------X- | |
-----X-----------XX-------X-----------------X----X---------XX------X-----X---X------XX---X--X------- | |
---------X----------------X----X-X-----------X------X--------X-X--------------------------X----X---- | |
X----------------------X-----------------X-------X---X--------------X-X-X-XX------X----------X--X-X- | |
X-----X------X-----------------------------X---------------------X----------X-XX------X-X-X-------X- | |
------------X---X--X------X-X---------X-----X---------X-------------------------X-------------X----- | |
---------------------X---X---------X------------------------------------------X-------X-X----------- | |
-----------------X--------XX----------X---X----------------X-----------------------X---------------- | |
-XX-------X-----X--------------------X-------X---X-------------X-------X--X---X-------X-------X----- | |
-----------X--------XX----X---X------X------X---X--------------------------------------X----X------X | |
--------------X------X--XX-------XX-------------X---------X---X---------X------X-------X------------ | |
----XX----------------------------------------X-------X---------X-------------------X--------------X | |
----------------X---X--------X----X--X-------------X---XX---X---------------X----X--X--------------- | |
---------------X------------------X------X--X---------X--------------X---X-----------X--------X----- | |
----------X-------X------------X-X------X----X---------------------X--X----------X----------------X- | |
---------X------X-----------------------------------------------------X---X------X------------------ | |
-----X-----------------X------------------------------X-------X-----X--X-----X-----X---------------- | |
----X---------------------X---------------X------X------------------------------------------X----X-- | |
-------X--X-------X-----------------X-----------XX---X-----------XX------X--------------------XX---- | |
-----X------------------X--------X----X----X-------------------XX-------------------------X-------X- | |
-----XX---------------------------------------X------------X------X------XXX------X---------X------X | |
X-------X------X----------X--X------X-X----------XX-----X------XX-------------------X-----X--------- | |
--X---X----------X-X---X------XX---------------X-----X--X--------X---------------------X----X------- | |
-X-X----------XX---X-X------------------------------XXX---X--X---X---X--X---X-----X-----X------X---- | |
-------X---------------X--X-------------------X-----------X--------X----------------------X--X------ | |
-------------------X-------------------------X-------X-X-----------------X------------X-X----------- | |
-----X-X------X----------------------------X-------------X-----------XX----------------------------- | |
----X----X-------------------------X----XX---------------X--X--X-----X-----X----------------------X- | |
-------------------X--------X-----------X---------X------X-----X-X---------------------------------- | |
--X------------X-----------XXX-------X--------X-------------X------------X----X--------------------- | |
-----------X--------X---------------------------------------------------X-------------X------------- | |
-X----XX--------------------------X--X--------X----X-------X-------X----X-------XX---X------------X- | |
-------X------------------------------X--X------X--X-------X--X------------X---------X-------------- | |
-XX--------X----X-X---X--------------------------X---X-X------------------------------XX------------ | |
----------X-----XX------------------X-X--------X-X-X---X--XX------------X-X-X--X-------------------- | |
X-------X-------XX---X-X--XX--------X---X-------------------------------X--------------------X------ | |
-X-----------------------------------X-----X--------------------------------X-------XX-------------- | |
--X------------------------X-XX----------------X-------X-------X--------------X-----X-X------X---X--""" | |
] | |
import numpy as np | |
for INPUT in INPUTS: | |
FIELD = np.array(list(map(list, INPUT.split('\n')))) | |
FIELD_SIZE = len(FIELD[0]) | |
# print(FIELD)#debug | |
tr_field = FIELD.T | |
# print(tr_field)#debug | |
max_square = 0 | |
for n_row, row in enumerate(FIELD): | |
conseq_counter, conseq_start = 0, 0 | |
for n, elem in enumerate(row): | |
# print(n_row, n)#debug | |
if elem == "-": | |
if not conseq_counter: | |
conseq_start = n | |
for i in row[n:]: | |
if i == "-": | |
conseq_counter += 1 | |
else: | |
# X | |
break | |
# if elem == "X" or n == FIELD_SIZE-1: | |
if conseq_counter > max_square: | |
#start a square growing | |
square_size = 0 | |
for i in range(min(conseq_counter, FIELD_SIZE-n_row)): | |
row_slice = FIELD[n_row+i][conseq_start:conseq_start+i+1] | |
col_slice = tr_field[conseq_start+i][n_row:n_row+i+1] | |
# print(1, n_row, n, conseq_start, conseq_counter, i, row_slice)#debug | |
# print(2, n_row, n, conseq_start, conseq_counter, i, col_slice)#debug | |
if "X" in row_slice or "X" in col_slice: | |
# print("X", square_size)#debug | |
max_square = max(max_square, square_size) | |
# conseq_counter, conseq_start = 0, 0 | |
break | |
else: | |
square_size += 1 | |
else: | |
# print("no obstacles")#debug | |
max_square = max(max_square, square_size) | |
# conseq_counter, conseq_start = 0, 0 | |
conseq_counter, conseq_start = 0, 0 | |
# print("max_square", max_square) | |
print("{1}: {0} dropships!".format(max_square**2, FIELD_SIZE)) |
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
#https://www.reddit.com/r/dailyprogrammer/comments/4o74p3/20160615_challenge_271_intermediate_making_waves/ | |
from struct import pack | |
from math import sin, pi | |
import wave | |
import random | |
RATE=8000 | |
note_length = 0.3 # seconds | |
NOTES="ABCDEFG_GFEDCBA" | |
NOTE_FREQS = { | |
"A" : 220.00, | |
"B" : 246.94, | |
"C" : 261.63, | |
"D" : 293.66, | |
"E" : 329.63, | |
"F" : 349.23, | |
"G" : 392.00, | |
} | |
with wave.open('test_mono.wav', 'w') as wv: | |
wv.setparams((1, 2, RATE, 0, 'NONE', 'not compressed')) | |
for note in NOTES: | |
try: | |
frequency = NOTE_FREQS[note] | |
maxVol=2**15-1 #maximum amplitude | |
except KeyError: | |
maxVol = 0 | |
wvData=bytes() | |
for i in range(0, int(RATE*note_length)): | |
wvData+=pack('h', int(maxVol*sin(i*2*pi*frequency/RATE))) #500Hz | |
wv.writeframes(wvData) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment