Created
February 17, 2017 17:27
-
-
Save zakaluka/102a10708c05ba0a0dfabe601c708c84 to your computer and use it in GitHub Desktop.
Blog-AOCD1P1 pieces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | |
{ loc with pt = npt } | |
| East -> | |
let npt = { x = loc.pt.x + dist; y = loc.pt.y } | |
{ loc with pt = npt } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let startPt = { | |
x = 0 | |
y = 0 | |
} | |
let startLoc = { | |
dir = North | |
pt = startPt | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let move way dist = (turn way) >> (advance dist) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
) | |
startLoc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let toTurn = function | |
| 'R' -> R | |
| 'L' -> L | |
| _ -> failwith "Unknown turn type" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let day1part1 = partOne "R2, L5, L4, L5, R4, R1, L4, R5, R3, R1, L1, L1, R4, L4, L1, R4, L4, R4, L3, R5, R4, R1, R3, L1, L1, R1, L2, R5, L4, L3, R1, L2, L2, R192, L3, R5, R48, R5, L2, R76, R4, R2, R1, L1, L5, L1, R185, L5, L1, R5, L4, R1, R3, L4, L3, R1, L5, R4, L4, R4, R5, L3, L1, L2, L4, L3, L4, R2, R2, L3, L5, R2, R5, L1, R1, L3, L5, L3, R4, L4, R3, L1, R5, L3, R2, R4, R2, L1, R3, L1, L3, L5, R4, R5, R2, R2, L5, L3, L1, L1, L5, L2, L3, R3, R3, L3, L4, L5, R2, L1, R1, R3, R4, L2, R1, L1, R3, R3, L4, L2, R5, R5, L1, R4, L5, L5, R1, L5, R4, R2, L1, L4, R1, L1, L1, L5, R3, R4, L2, R1, R2, R1, R1, R3, L5, R1, R4" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
| North -> { loc with dir = East } | |
| East -> { loc with dir = South } | |
| South -> { loc with dir = West } | |
| West -> { loc with dir = North } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Direction = | |
| North | |
| South | |
| East | |
| West | |
type TurnTo = | |
| R | |
| L | |
type Point = { | |
x : int | |
y : int | |
} | |
type Location = { | |
dir : Direction | |
pt : Point | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment