Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View shanecharles's full-sized avatar

Shane Charles shanecharles

View GitHub Profile
#r "packages/FParsec/lib/net40-client/FParsecCS.dll"
#r "packages/FParsec/lib/net40-client/FParsec.dll"
open FParsec
type Operation =
| Spin of int
| Exchange of int * int
| Partner of char * char
type ParserState = unit
@shanecharles
shanecharles / aoc2017-day16-parsers.fs
Last active August 6, 2019 08:37
FParsec code to parse Advent of Code day 16 input to an Operation choice.
#r "packages/FParsec/lib/net40-client/FParsecCS.dll"
#r "packages/FParsec/lib/net40-client/FParsec.dll"
open FParsec
type Operation =
| Spin of int
| Exchange of int * int
| Partner of char * char
type ParserState = unit
type Operation =
| Spin of int
| Exchange of int * int
| Partner of char * char
let memoryValues =
seq {
yield 1
let rec loop i (ring : int []) =
seq {
yield! ring |> Seq.ofArray
let nring = generateRing i ring
yield! loop (i+1) nring
}
yield! loop 2 [|1;2;4;5;10;11;23;25|]
let generateRing ringId previousRing =
let rowLength = ringId * 2
let ringSize = rowLength * 4
let ring = Array.zeroCreate ringSize
let data = (rowLength, previousRing, ring)
for i in 0 .. (ringSize - 1) do
let value =
match (data, i) with
| FirstItem v
let wrapGet (arr : int []) = function
| x when x < 0 -> arr.[arr.Length + x]
| x -> arr.[x]
let generalIndexOffset rowLength location = 2 * (location / rowLength) + 1
let (|LastItem|_|) ((_, previousRing : int [], currentRing : int []), i) =
if i = currentRing.Length - 1
then Some (currentRing.[i - 1] + currentRing.[0] + previousRing.[previousRing.Length - 1])
let part1 location =
let rowOffset ringId limit =
match (location - limit) % (ringId * 2) with
| 0 -> ringId
| x when x - ringId < 0 -> (ringId - x) % ringId
| x -> x % ringId
location |> function
| x when x <= 1 -> 0
| x ->
@shanecharles
shanecharles / aoc-2017-day3-part1-ring.fs
Last active December 6, 2017 01:30
Initial ring generation for advent of code 2017 day 3 part 1.
let rings =
seq {
yield (0,1)
yield! Seq.unfold
(fun (i, previous) ->
let ringId = i + 1
let m = (ringId, (ringId * 2 * 4) + previous)
Some (m,m)) (0,1) }
@shanecharles
shanecharles / ThereAndBackProperty.fsx
Last active October 25, 2017 01:38
There and back properties of MSDN encryption/decryption example.
#I "packages/FsCheck/lib/net452/"
#r "FsCheck.dll"
#I "artifacts/"
#r "ThereAndBack.dll"
open FsCheck
open System
let ``Check there and back of DateTime encryption`` (input : DateTime) =
@shanecharles
shanecharles / partial-application-to-higher-order.fsx
Created July 23, 2017 13:26
F# script file of refactoring a curried function into a higher order function.
let datapoints = [1 .. 400000]
let keys = [1; -4; 45000; 2501; 56000]
// Enable some evaluation statistics. Execute #time again to disable.
#time
let singleKeyLookup fn = fn datapoints 67 |> ignore
let multiKeyLookup (fn : int seq -> int -> bool) =
keys |> List.map (fn datapoints)