summary refs log tree commit diff stats
path: root/day2.fsx
blob: 26fc704950f31cb3fc7548656cf042c9488feb2d (plain) (blame)
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
open System.IO

let keypad1 (pos: int * int): string = 
    let (x, y) = pos
    string (x + 3 * y + 1)

let keypad2 (pos: int * int): string =
    let pad = [
        "--1--"
        "-234-"
        "56789"
        "-ABC-"
        "--D--"
    ]
    let (row, col) = pos
    string (pad[col][row])

let final_pos1 (start: int * int) (line: string) =
    let move (pos: int*int) (dPos: char) = 
        let (x, y) = pos
        match dPos with
            | 'U' -> (x, y-1 |> max 0 |> min 2)
            | 'D' -> (x, y+1 |> max 0 |> min 2)
            | 'L' -> (x-1 |> max 0 |> min 2, y)
            | 'R' -> (x+1 |> max 0 |> min 2, y)
            | _ -> (-500, -500)
    List.fold move start (Seq.toList line)

let final_pos2 (start: int*int) (line: string) =
    let move (pos: int*int) (dPos: char) =
        let (x, y) = pos
        let newPos =
            match dPos with
                | 'U' -> (x, y-1 |> max 0 |> min 4)
                | 'D' -> (x, y+1 |> max 0 |> min 4)
                | 'L' -> (x-1 |> max 0 |> min 4, y)
                | 'R' -> (x+1 |> max 0 |> min 4, y)
                | _ -> (-500, -500)
        if keypad2 newPos = "-" then pos else newPos
    List.fold move start (Seq.toList line)

let lines = "day2.txt" |> File.ReadLines |> Seq.toList

let generate_code (instructions: list<string>) (posfunc: int*int -> string -> int*int) (keyfunc: int*int -> string) (start: int*int): list<string> =
    let positions = List.scan posfunc start instructions
    List.map keyfunc positions[1..]

// part 1
generate_code lines final_pos1 keypad1 (1, 1) |> String.concat "" |> printfn "%s"
// part 2
generate_code lines final_pos2 keypad2 (0, 2) |> String.concat "" |> printfn "%s"