Skip to content

Instantly share code, notes, and snippets.

@fbwright
fbwright / backpack.py
Created June 23, 2015 11:55
[2015-06-17] Challenge #219 [Hard] The Cave of Prosperity
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#An attempt at finding a solution to the challenge #219 of /r/DailyProgrammer
#(http://www.reddit.com/r/dailyprogrammer/comments/3aewlg/)
#I am using a genetic algorithm to find an (usually approximate) solution.
from __future__ import print_function, division
import genetic_algorithm as ga
import sys, random, argparse
if sys.version_info.major < 3:
input = raw_input
@fbwright
fbwright / cod_fis.py
Last active August 29, 2015 14:22
Calcolo del codice fiscale
N,L="0123456789","ABCDEFGHIJKLMNOPQRSTUVWXYZ";A=N+L;D="! %')-/135! %')-/135\"$24+#&(,.0*6987";P=" !\"#$%&'() !\"#$%&'()*+,-./0123456789";p=lambda l,i:l.split("\t")[i].strip()
def G(s,f=0):
C,V="",""
for c in s.upper():
if c in "AEIOU":V+=c
else:C+=c
C=[C,C[0]+C[2:]][f and len(C)>3];return(C+V+"X"*3)[:3]
C=lambda n,c,s,g,m,a,C:(lambda c:c+L[sum(ord(([D,P][i%2])[A.index(x)])-32 for i,x in enumerate(c))%26])(G(c)+G(n,1)+str(a)[-2:]+"ABCDEHLMPRST"[m-1]+"%02d"%(g+40*s)+{p(l,1):p(l,0)for l in open("belfiore.txt").read().splitlines()}[C.upper()])
@fbwright
fbwright / pcalc.nim
Created May 30, 2015 15:42
Calculator in Nimrod with RDP
#pCalc - Nimrod version7
import strutils, math, tables, logging
type
EProgramError = object of E_Base
EStackError = object of EProgramError
EStackUnderflowError = object of EStackError
ESyntaxError = object of EProgramError
ERuntimeError = object of EProgramError
EDivisionByZeroError = object of ERuntimeError
TSymbol = enum
@fbwright
fbwright / compiler.py
Created May 30, 2015 15:35
Recursive descent parser and interpreter/(Python|Forth) compiler
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, division
import sys, re
if sys.version_info.major < 3:
input = raw_input
#I wonder... how difficult would be writing a parser that takes well-formed
#BNF in input and outputs a recursive descent parser based on that grammar?
#I'd have to recognize, name and put into an enum tokens ('...'), create a
#dictionary of identifiers (<...>), and each identifier will be assigned
@fbwright
fbwright / vm_gen.py
Created May 30, 2015 15:21
VM generator
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, division
from contextlib import contextmanager
import sys
if sys.version_info.major < 3:
input = raw_input
class CodeGenerator(object):
def __init__(self, tab=" "):
@fbwright
fbwright / sasm.py
Created May 30, 2015 14:48
Simple assembler for a simple VM
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, division
import sys
from time import sleep
if sys.version_info.major < 3:
input = raw_input
#THIS! IS! A! FUCKING! MESS!
@fbwright
fbwright / manga.py
Created May 21, 2015 18:06
Manga downloader
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#by fbWright
from __future__ import print_function, division
import sys, bs4, requests, os, os.path, json
from urllib.parse import urlparse, urljoin
from unidecode import unidecode
if sys.version_info.major < 3:
input = raw_input
@fbwright
fbwright / brainfuck.py
Created March 28, 2015 22:36
Some of my golfed code
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Finally finished! 2014-07-26T16:25, 768 bytes - with interactive prompt
# and reading from file
#TODO: exception-checking, maybe continuation
#Brainfuck in python
#Brainfuck instructions:
# > increment the data pointer (to point to the next cell to the right).
# < decrement the data pointer (to point to the next cell to the left).
# + increment (increase by one) the byte at the data pointer.
@fbwright
fbwright / novel_zip.py
Created March 28, 2015 17:43
Novel compressor
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, division
import sys, time
if sys.version_info.major < 3:
input = raw_input
def compress(data):
freq = {}
for line in data.splitlines():
@fbwright
fbwright / frogue.fs
Last active August 29, 2015 14:15
frogue.fs
( frogue.fs 2015-02-23T09.05 by fbwright )
( A [simple] roguelike in gforth; move the @, collide with # )
( And [comments excluded] fits into a block [1000/1024 B] )
variable &i 64 constant W 16 constant H create _M W H * chars
allot 6 constant MS 32 constant MM create _O MS MM * chars allot
: U 0 -1 ; : D 0 1 ; : R 1 0 ; : L -1 0 ; : # W * + _M + ; : &
MS * _O + ; : b? H 1- mod 0= swap W 1- mod 0= or ; : p? c@ '# <>
; : #d 2dup at-xy # c@ emit ; : #n 2dup # -rot b? if '# else bl
then swap c! ; : &xy@ & 1+ dup c@ swap 1+ c@ ; : &xy! & 1+ rot
over c! 1+ c! ; : &d dup &xy@ at-xy & c@ emit ; : &n &i @ & swap