Skip to content

Instantly share code, notes, and snippets.

View lwiecek's full-sized avatar

Łukasz Więcek lwiecek

View GitHub Profile
@lwiecek
lwiecek / Mind Games : 123 Dice Solver.py
Last active August 29, 2015 13:55
I was stuck on 123 dice in Mind Games so created this solver :-)
from collections import defaultdict
from itertools import product
board = "321321313" \
"123312222" \
"321213131" \
"312231123" \
"213112321" \
"231323123" \
"132231231" \
@lwiecek
lwiecek / livescore.py
Created February 7, 2014 00:29
Livescore results
import requests
from lxml.html.soupparser import fromstring
import csv
r = requests.get('http://livescore.com')
root = fromstring(r.text)
home_teams = root.xpath('//td[@class="fh"]/text()')
away_teams = root.xpath('//td[@class="fa"]/text()')
@lwiecek
lwiecek / puzzle.py
Last active August 29, 2015 14:04
Word puzzle - DFS with generator
#!/usr/local/bin/python3
N = 4
DICTIONARY = set([
'A',
'AB',
'ABC',
'ABCDEDCB',
'ABCDEFAFEDCDEDCB'
])
import random
class Game(object):
N = 3
def __init__(self, ai=None):
self.board = []
for i in range(Game.N):
self.board.append([' '] * Game.N)
self.current_player = 'X'
@lwiecek
lwiecek / day6.py
Created December 19, 2015 20:35
adventofcode - day6
import itertools
on = set()
def get_min_max(fragments):
return tuple(int(v) for v in (fragments[0] + ',' + fragments[2]).split(','))
def walk(min_x, min_y, max_x, max_y, func):
for coord in itertools.product(xrange(min_x, max_x+1), xrange(min_y, max_y+1)):
func(coord)
@lwiecek
lwiecek / day7.py
Created December 20, 2015 01:20
day7
from collections import namedtuple
import copy
Gate = namedtuple('Gate', 'op in1 in2 out')
gates = set() # gates that have not been calculated yet
wires = {} # identifier -> value
def get_value(gate_in):
if gate_in is None:
return None
@lwiecek
lwiecek / day8.py
Created December 20, 2015 12:32
day8
total = 0
with open('input8.txt') as f:
for line in f:
line = line.strip()
# part1:
# total += len(line) - len(eval(line))
# part2:
new_repr = '"' + line.replace('\\', '\\\\').replace('"', '\\"') + '"'
total += len(new_repr) - len(line)
print(total)
@lwiecek
lwiecek / day9.py
Created December 20, 2015 23:52
day9
import itertools
from collections import defaultdict
dist = defaultdict(lambda: float('inf'))
cities = set()
with open('input9.txt') as f:
for line in f:
frags = line.strip().split()
src_dest = frags[0], frags[2]
dist[frozenset(src_dest)] = int(frags[4])
@lwiecek
lwiecek / convertUIColor.py
Created July 2, 2018 13:54
Convert UIColor to HTML hex value
import re
def uicolor2hex(s):
""" Convert UIColor to HTML hex value.
I thought invisionapp only supports this format but eventually found the switch in settings.
Leaving it here in case someone needs it.
uicolor2hex('UIColor(red:0.08, green:0.14, blue:0.24, alpha:1)') == '#14233dff'
"""
m = re.match('UIColor\(red:(.*), green:(.*), blue:(.*), alpha:(.*)\)', s)
r, g, b, a = m.groups()
def hex_value(c):
@lwiecek
lwiecek / copy_strings.py
Last active July 25, 2018 14:45
Get strings that look like a copy string in a given directory (recursively)
import os
import sys
import re
def list_files(directory, extension):
result = []
paths = [os.path.join(directory, name) for name in os.listdir(directory)]
dirs = filter(os.path.isdir, paths)
matches_ext = lambda path: os.path.splitext(path)[1] == extension
files_matching_ext = filter(matches_ext, paths)