Skip to content

Instantly share code, notes, and snippets.

View milang's full-sized avatar

Milan Gardian milang

  • Calgary, Alberta, Canada
  • 06:35 (UTC -06:00)
View GitHub Profile
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Input data
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
let parse filename offset =
File.ReadAllLines(filename)
|> Seq.mapi
(fun rowIndex line ->
let y = offset - rowIndex // read rows from top to bottom (from positives to negatives)
line
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Input data
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
let rotateLeft (value: string) =
match value.Length with
| 5 -> String.Join("", [| value.[1]; value.[4]; '/'; value.[0]; value.[3] |])
| 11 -> String.Join("", [| value.[2]; value.[6]; value.[10]; '/'; value.[1]; value.[5]; value.[9]; '/'; value.[0]; value.[4]; value.[8] |])
| _ -> raise (InvalidOperationException "Unrecognized value")
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Input data
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
type Vector = (int64 * int64 * int64)
type Particle = {
position: Vector
velocity: Vector
acceleration: Vector
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Input data
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
let parse filename =
File.ReadAllLines(filename)
|> Seq.map (fun line -> line |> Array.ofSeq)
|> Array.ofSeq
let data = parse "queries/Advent/Advent2017-day19.txt"
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Input data
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
type State = {
exec: int;
registers: Map<char, int64>;
lastSound: int64;
receiving: bool;
sendsCount: int;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Problem A
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
type SpinlockState = { position: int; counter: int; list: int list }
let rec insert position value list =
if position = 0 then value::list
else
match list with
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Input data
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
let findProgram name programs = programs |> Array.findIndex ((=) name)
let spin count programs =
let offset = count % (Array.length programs)
if offset = 0 then programs
else
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Problem A
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
let generate factor seed =
seed
|> Seq.unfold (
fun previous ->
let result = (previous * factor) % 2147483647L
Some (result,result))
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Previous assignment: KnotHash
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
let rec reverse start length array =
if length < 2 then array
else
let other = (start + length - 1) % (Array.length array)
let tmp = array.[start]
array.[start] <- array.[other]
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Input data
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
type Layer = { index: int; depth: int; period: int }
let parse filename =
File.ReadAllLines(filename)
|> Seq.map (fun line ->
let parts = line.Split(':')