-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.fsx
67 lines (57 loc) · 1.54 KB
/
day2.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#r "./packages/Fuchu/lib/Fuchu.dll"
module BathroomKeypad =
open System
let private keypad =
[
" 1 "
" 234 "
"56789"
" ABC "
" D "
] |> array2D
let private centrePoint = (0,2)
let private move c (x,y) =
match c with
| 'U' -> (x,y-1)
| 'D' -> (x,y+1)
| 'L' -> (x-1,y)
| 'R' -> (x+1,y)
| _ -> failwith "Up, down, left, right"
let private getKey (x,y) = keypad.[y,x]
let private isValid = function
| _,y when y < 0 || y >= 5 ->
false
| x,_ when x < 0 || x >= 5 ->
false
| xy -> getKey xy <> ' '
let rec private findDigit xy line =
match line with
| c::tail ->
let nxy = move c xy
findDigit (if isValid nxy then nxy else xy) tail
| [] -> xy
let private toCharList (s:string) = [for c in s.Trim() -> c]
let getAccessCode input =
input
|> Seq.map toCharList
|> Seq.filter (not << List.isEmpty)
|> Seq.scan findDigit centrePoint
|> Seq.tail
|> Seq.map getKey
|> Seq.toArray
|> String
open Fuchu
let input =
[
"ULL "
"RRDDD"
"LURDL"
"UUUUD"
""
]
testCase "bathroom keypad" (fun _ -> Assert.Equal("", "5DB3", BathroomKeypad.getAccessCode input))
|> run
|> ignore
System.IO.File.ReadAllLines("day2-input.txt")
|> BathroomKeypad.getAccessCode
|> printfn "The bathroom code is %s"