Skip to content

Instantly share code, notes, and snippets.

@scoiatael

scoiatael/Part1.fsx

Created Jan 17, 2018
Embed
What would you like to do?
module Dancers
type Circle = char array
type Move = Circle -> Circle
let startingPattern : Circle = Array.ofList ['a'..'p']
let spin (n : int) : Move =
fun circle ->
let len = Array.length circle in
Array.append circle.[(len-n)..(len-1)] circle.[0..(len-n-1)]
let exchange (a : int, b : int) : Move =
fun circle ->
let valA = circle.[a] in
circle.[a] <- circle.[b];
circle.[b] <- valA;
circle
let partner (a : char, b : char) : Move =
fun circle ->
let idxA = Array.findIndex ((=) a) circle in
let idxB = Array.findIndex ((=) b) circle in
exchange (idxA, idxB) circle
let parseMove (input : string) : Move =
let cmd = input.[0] in
let args = input.[1..] in
let pairOfArray [|a; b|] = (a, b) in
let parseExchange = Array.map int >> pairOfArray in
let firstChar (x : string) = x.[0] in
let parsePartner = Array.map firstChar >> pairOfArray
match cmd with
| 's' -> args |> int |> spin
| 'x' -> args.Split '/' |> parseExchange |> exchange
| 'p' -> args.Split '/' |> parsePartner |> partner
let parse (input : string) =
input.Split ','
|> Array.map parseMove
let solve = Array.fold (|>) startingPattern >> System.String
let input = System.IO.File.ReadAllText "input.txt" |> parse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment