Skip to content

Instantly share code, notes, and snippets.

@einblicker
einblicker / gist:3115842
Created July 15, 2012 08:12
POJ 3009 Curling 2.0
exception Done of int
let loopEnd = ref false
while not !loopEnd do
let [| width; height |] = System.Console.ReadLine().Split([|' '|]) |> Array.map int
if height = 0 && width = 0 then loopEnd := true
else
let maze =
[| for x = 0 to height - 1 do
yield System.Console.ReadLine().Split([|' '|]) |> Array.map int |]
let start = ref (0, 0)
@einblicker
einblicker / gist:3115976
Created July 15, 2012 08:55
POJ 3669 Meteor Shower
module Util =
let fordir x y f =
for (dx, dy) in [ (-1, 0); (0, -1); (1, 0); (0, 1) ] do
f (x+dx) (y+dy)
open Util
let size = System.Console.ReadLine() |> int
let inputs = [|
for x = 0 to size - 1 do
@einblicker
einblicker / gist:3116215
Created July 15, 2012 10:37
AOJ 0121 Seven Puzzle
module Util =
let toStr x = Array.fold (fun x y -> x + string y) "" x
let fordir (arr : int[]) x f =
for (i, d) in List.mapi (fun a b -> (a, b)) [ 1; -1; 4; -4 ] do
if not(x%4=0 && i=1) && not(x%4=3 && i=0) && x+d<8 && x+d>=0 then
let arr = arr.Clone() :?> int[]
let t = arr.[x]
arr.[x] <- arr.[x + d]
arr.[x + d] <- t
f arr
@einblicker
einblicker / gist:3117471
Created July 15, 2012 15:32
PKU 2718 Smallest Difference
let size = System.Console.ReadLine() |> int
let input = [|
for i = 0 to size - 1 do
yield System.Console.ReadLine().Split([|' '|]) |> Array.map int
|]
let rec permutations list taken =
seq { if Set.count taken = List.length list then yield [] else
for l in list do
if not (Set.contains l taken) then
@einblicker
einblicker / gist:3117567
Created July 15, 2012 15:57
POJ 3187 Backward Digit Sums
let [| n; sum |] = System.Console.ReadLine().Split([|' '|]) |> Array.map int
let rec permutations list taken =
seq { if Set.count taken = List.length list then yield [] else
for l in list do
if not (Set.contains l taken) then
for perm in permutations list (Set.add l taken) do
yield l::perm }
let step xs = seq {
@einblicker
einblicker / gist:3135433
Created July 18, 2012 10:23
System Fのインタプリタ(未完成)
type ty =
| TyVar of int * int
| TyArr of ty * ty
| TyAll of string * ty
| TySome of string * ty
type binding =
| NameBind
| VarBind of ty
| TyVarBind
@einblicker
einblicker / gist:3136147
Created July 18, 2012 13:13
レーベンシュタイン距離
let levenshteinDistance (str1 : string) (str2 : string) =
let d = Array.init (str1.Length+1) (fun _ -> Array.init (str2.Length+1) (fun _ -> 0))
let mutable cost = 0
for i1 = 0 to str1.Length do
d.[i1].[0] <- i1
for i2 = 0 to str2.Length do
d.[0].[i2] <- i2
for i1 = 1 to str1.Length do
for i2 = 1 to str2.Length do
if str1.[i1-1] = str2.[i2-1] then
@einblicker
einblicker / ねじれヒープ.fs
Created July 23, 2012 16:42
the fun of programming chap1
type Tree<'T when 'T : comparison> = Null | Fork of 'T * Tree<'T> * Tree<'T>
let isEmpty = function
| Null -> true
| _ -> false
let minElem (Fork(x,_,_)) = x
let rec merge a b =
@einblicker
einblicker / NeuralNetwork.fs
Created July 26, 2012 14:00
neural network
open System
type Class = Virus | Noise
type Datum = { Image : float[]; Class : Class }
type NeuralNetwork(inputSize, hiddenSize, outputSize, patternSize, maxT, eps, alpha, beta, W0, high, low) =
let mutable eta = 0.005
let inputLayer = Array.init (inputSize+1) (fun _ -> 0.0)
let hiddenLayer = Array.init (hiddenSize+1) (fun _ -> 0.0)
@einblicker
einblicker / gist:3187433
Created July 27, 2012 11:07
MCMCっぽいやつ
(* 何かがおかしい。要検証。 *)
module MCMC =
let random = new Random()
let select x = x + (random.NextDouble() - 0.5) * 10.0
let prob x = 1.0 / (1.0 + System.Math.Exp(-x))
let nextStatus x =
let xt = select x
let p = prob xt / prob x