Skip to content

Instantly share code, notes, and snippets.

(* FizzBuzz *)
let main input =
let f x = if x % 3 = 0 then "Fizz" else ""
let b x = if x % 5 = 0 then "Buzz" else ""
let ans = (f input) + (b input)
if ans = "" then input.ToString() else ans
@zakaluka
zakaluka / Blog-AOCD1P1-Advance.fs
Created February 17, 2017 17:27
Blog-AOCD1P1 pieces
let advance dist loc =
match loc.dir with
| North ->
let npt = { x = loc.pt.x; y = loc.pt.y + dist }
{ loc with pt = npt }
| South ->
let npt = { x = loc.pt.x; y = loc.pt.y - dist }
{ loc with pt = npt }
| West ->
let npt = { x = loc.pt.x - dist; y = loc.pt.y }
type Direction =
| North
| South
| East
| West
type TurnTo =
| R
| L
let startPt = {
x = 0
y = 0
}
let startLoc = {
dir = North
pt = startPt
}
let toTurn = function
| 'R' -> R
| 'L' -> L
| _ -> failwith "Unknown turn type"
let turn way loc =
match way with
| L ->
match loc.dir with
| North -> { loc with dir = West }
| West -> { loc with dir = South }
| South -> { loc with dir = East }
| East -> { loc with dir = North }
| R ->
match loc.dir with
let advance dist loc =
match loc.dir with
| North ->
let npt = { x = loc.pt.x; y = loc.pt.y + dist }
{ loc with pt = npt }
| South ->
let npt = { x = loc.pt.x; y = loc.pt.y - dist }
{ loc with pt = npt }
| West ->
let npt = { x = loc.pt.x - dist; y = loc.pt.y }
let move way dist = (turn way) >> (advance dist)
let moveAll strList =
Regex.Matches (strList, @"[LR]\d+")
|> Seq.cast
|> Seq.fold
(fun oldloc (instr : System.Text.RegularExpressions.Match) ->
move
(instr.Value.[0] |> toTurn)
(instr.Value.Substring 1 |> int)
oldloc
)
let distance stloc endloc = (abs (stloc.x - endloc.x)) + (abs (stloc.y - endloc.y))
let partOne strList =
strList |> moveAll |> fun x -> distance startPt x.pt