Skip to content

Instantly share code, notes, and snippets.

@zakaluka
Created February 17, 2017 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zakaluka/102a10708c05ba0a0dfabe601c708c84 to your computer and use it in GitHub Desktop.
Save zakaluka/102a10708c05ba0a0dfabe601c708c84 to your computer and use it in GitHub Desktop.
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 }
{ loc with pt = npt }
| East ->
let npt = { x = loc.pt.x + dist; y = loc.pt.y }
{ loc with pt = npt }
let startPt = {
x = 0
y = 0
}
let startLoc = {
dir = North
pt = startPt
}
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
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
)
startLoc
let toTurn = function
| 'R' -> R
| 'L' -> L
| _ -> failwith "Unknown turn type"
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"
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 }
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