Skip to content

Instantly share code, notes, and snippets.

View jasondown's full-sized avatar

Jason Down jasondown

View GitHub Profile
@jasondown
jasondown / factorial_elixir.ex
Created September 2, 2016 04:29
Elixir Factorial - Pattern Matching and Recursion
defmodule MyMath do
def factorial(n), do: factorial(n, 1)
def factorial(0, acc), do: acc
def factorial(n, acc) do
factorial(n - 1, acc * n)
end
@jasondown
jasondown / C4L_default.fsx
Last active December 28, 2018 05:48
Default F# Code
(* Bring data on patient samples from the diagnosis machine to the laboratory with enough molecules
to produce medicine! *)
open System
let projectCount = int(Console.In.ReadLine())
for i in 0 .. projectCount - 1 do
let token = (Console.In.ReadLine()).Split [|' '|]
let a = int(token.[0])
let b = int(token.[1])
let c = int(token.[2])
type Token = string array
// unit -> string
let readInput () = Console.ReadLine()
// unit -> int
let readInt = readInput >> int
// string -> string []
let tokenize (line : string) = line.Split ' '
type Module =
| StartPos
| Diagnosis
| Molecules
| Laboratory
static member Create moduleName =
match moduleName with
| "START_POS" -> Module.StartPos
| "DIAGNOSIS" -> Module.Diagnosis
| "MOLECULES" -> Module.Molecules
type MoleculeType =
| A | B | C | D | E
override x.ToString () =
match x with
| A -> "A"
| B -> "B"
| C -> "C"
| D -> "D"
| E -> "E"
type MoleculeStorage =
{ Counts : Map<MoleculeType, int> }
static member Create (token : Token) =
{ Counts =
Map [ (MoleculeType.A, (int <| token.[0]))
(MoleculeType.B, (int <| token.[1]))
(MoleculeType.C, (int <| token.[2]))
(MoleculeType.D, (int <| token.[3]))
(MoleculeType.E, (int <| token.[4])) ]
}
type HealthPoints = int
type Player =
| Me | Enemy | Cloud
static member Create id =
match id with
| 0 -> Player.Me
| 1 -> Player.Enemy
| -1 -> Player.Cloud
| i -> failwithf "Unknown player id: %i" i
type Id = int
type SampleData =
{ Id : Id
CarriedBy : Player
HealthPoints : HealthPoints
Molecules : MoleculeStorage }
static member Create (token : Token) =
{ Id = (int <| token.[0])
CarriedBy = Player.Create (int <| token.[1])
type GameState =
{ Robots : Robot list
Samples : SampleData list }
// SampleData -> string
let collect:Collect = fun s -> sprintf "CONNECT %i" <| s.Id
// MoleculeType -> string
let gather:Gather = fun m -> sprintf "CONNECT %s" <| m.ToString()
// SampleData -> string
let produce:Produce = fun s -> sprintf "CONNECT %i" s.Id
// Module -> string