Skip to content

Instantly share code, notes, and snippets.

View Blog-FizzBuzz.fs
(* 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
View Blog-AOCD1P1-Advance.fs
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 }
View Blog-AOCD1P1-Types2.fs
type Direction =
| North
| South
| East
| West
type TurnTo =
| R
| L
View Blog-AOCD1P1-BaseValues2.fs
let startPt = {
x = 0
y = 0
}
let startLoc = {
dir = North
pt = startPt
}
View Blog-AOCD1P1-ParseFile2.fs
let toTurn = function
| 'R' -> R
| 'L' -> L
| _ -> failwith "Unknown turn type"
View Blog-AOCD1P1-Turn2.fs
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
View Blog-AOCD1P1-Advance2.fs
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 }
View Blog-AOCD1P1-Move2.fs
let move way dist = (turn way) >> (advance dist)
View Blog-AOCD1P1-MoveAll2.fs
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
)
View Blog-AOCD1P1-Distance2.fs
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