Skip to content

Instantly share code, notes, and snippets.

import Control.Monad.State
import System.Random
import Data.List
import System.Environment
rollDie :: State StdGen Int
rollDie = state $ randomR (1, 6) -- randomR (1, 6) :: StdGen -> (Int, StdGen)
rollNDice :: Int -> State StdGen [Int]
rollNDice x | x < 1 = return []
from collections import defaultdict
from copy import deepcopy
from functools import partial
def pre_solve(key_funcs, N):
kfl = len(key_funcs)
players = [defaultdict(set) for _ in xrange(kfl)]
rplayers = [{} for _ in xrange(kfl)]
from collections import defaultdict
from copy import deepcopy
daphne = defaultdict(set)
rdaphne = {}
maxx = defaultdict(set)
rmaxx = {}
mindy = defaultdict(set)
rmindy = {}
""" The problem was solved on 1 core in 543m35.913s.
The best score was: 3354674163673461017691780032809373762464467910656
The best dice was: up: 6, down: 3, top: 1, bottom: 6, left: 8, right: 7
The dice started with 1 on top and 3 on up
The route associated with the score and the dice is:
[(1, 1), (2, 1), (2, 2), (3, 2), (3, 3), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (2, 10), (1, 10), (1, 11), ( 2, 11), (3, 11), (4, 11), (5, 11), (6, 11), (7, 11), (8, 11), (9, 11), (9, 10), (9, 9), (9, 8), (9, 7), (9, 6), (9, 5), (8, 5), (8, 4), (7, 4), (6, 4), (6, 5), (6, 6), (6, 7), (7, 7), (7, 8), (7, 9), (6, 9), (5, 9), (4, 9), (4, 8), (4, 7), (4, 6), (3, 6), (3, 5), (4, 5), (5, 5), (5, 4), (4, 4), (4, 3), (4, 2), (5, 2), (6, 2), (7, 2), (8, 2), (9, 2), (9, 3), (10, 3), (10, 2), (10, 1), (11, 1), (11, 2), (11, 3), (11, 4), (10, 4), (10, 5), (11, 5), (11, 6), (11, 7), (11, 8), (11, 9), (11, 10), (11, 11), (11, 12), (12, 12)]
"""
from itertools import product
from random import shuffle
@jakab922
jakab922 / knight_pos.hs
Last active February 5, 2016 14:40
The in_n function is important which calculates the reachable squares reachable in n steps on the chessboard(quite inefficient though)
import Data.List (nub)
class Monad m => MonadPlus m where
mzero :: m a
mplus :: m a -> m a -> m a
instance MonadPlus [] where
mzero = []
mplus = (++)
import re
PUT_PATTERN = re.compile(r'PUT (?P<what>[0-9]+) INSIDE (?P<to>[0-9]+)')
LOOSE_PATTERN = re.compile(r'SET (?P<which>[0-9]+) LOOSE')
SWAP_PATTERN = re.compile(r'SWAP (?P<one>[0-9]+) WITH (?P<other>[0-9]+)')
class Bag(object):
def __init__(self, n, parent=None):
self.n = n
@jakab922
jakab922 / freelancers_dream.py
Last active December 10, 2015 10:59
Solution for Freelancer's dream on codeforces(http://codeforces.com/contest/606/problem/E)
""" The problem is an extremely easy linear programming problem.
The only issue is that there is no simplex solver in the
standard library and scipy is not available on codeforces.
"""
from scipy.optimize import linprog
n, p, q = map(int, raw_input().strip().split())
A = [[], []]
b = [-p, -q]
c = [1 for _ in xrange(n)]
from collections import defaultdict
from sys import argv
def to_dict(word):
ret = defaultdict(int)
for letter in word:
ret[letter] += 1
return ret
""" Computing the number of permutations on n points
no fix points in 3 ways. """
from functools import wraps
from math import factorial, e, floor
from sys import argv
CACHE = {}
def cache(f):
@wraps(f)
checkers = [
lambda nums: False,
lambda nums: True,
lambda nums: nums[-1] % 2 == 0,
lambda nums: sum(nums) % 3 == 0,
lambda nums: (10 * nums[-2] + nums[-1]) % 4 == 0,
lambda nums: nums[-1] in [0, 5],
lambda nums: sum(nums) % 3 == 0 and nums[-1] % 2 == 0,
lambda nums: sum([10 ** i * nums[-(i + 1)] for i in xrange(len(nums))]) % 7 == 0,
lambda nums: sum([10 ** i * nums[-(i + 1)] for i in xrange(3)]) % 8 == 0,