-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay19.fs
66 lines (50 loc) · 1.93 KB
/
Day19.fs
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
module Day25
// problem page
// http://adventofcode.com/2017/day/19
open System.IO
let processFile (filePath : string) =
seq {
use fileReader = new StreamReader(filePath)
while not fileReader.EndOfStream do
let line = fileReader.ReadLine()
yield line
}
type Direction =
| Right
| Up
| Left
| Down
let move (x, y) direction =
match direction with
| Right -> (x + 1, y)
| Up -> (x, y - 1)
| Left -> (x - 1, y)
| Down -> (x, y + 1)
let explode (s:string) =
[for c in s -> c]
let problemFileName = @"Data\19.txt"
let field = problemFileName
|> processFile
|> Seq.map (explode >> List.toArray)
|> Seq.toArray
let rec moveThroughTubes (field : char[][]) direction letters steps position =
let getCurSymbol position =
let x, y = fst position, snd position
if (x < 0 || x >= field.[0].Length || y < 0 || y >= field.Length) then ' '
else field.[y].[x]
let findNewDirection =
match direction with
| Up | Down -> if move position Left |> getCurSymbol <> ' ' then Left else Right
| Left | Right -> if move position Up |> getCurSymbol <> ' ' then Up else Down
match getCurSymbol position with
| ' ' -> letters, steps //exit
| '|'| '-' -> move position direction |> moveThroughTubes field direction letters (steps + 1)
| '+' -> let direction = findNewDirection
move position direction |> moveThroughTubes field direction letters (steps + 1)
| letter -> move position direction |> moveThroughTubes field direction (letter::letters) (steps + 1)
let enter = 0, field.[0] |> Array.findIndex (fun x -> x = '|')
let result = moveThroughTubes field Down [] 0 (snd enter, fst enter)
let letters = fst result
|> List.rev
|> Array.ofList |> System.String
let steps = snd result