-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.fs
68 lines (55 loc) · 1.75 KB
/
Program.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
66
67
68
module Day04
open System
open System.IO
type Room = { Name: string; SectorID: int; Checksum: string }
let parseRoom (line: string) =
let room = line.Substring(0, line.Length - 7)
let checksum = line.Substring(line.Length - 6, 5)
let nameIdSeparator = line.LastIndexOf('-')
let name = room.Substring(0, nameIdSeparator)
let id = room.Substring(nameIdSeparator + 1, room.Length - nameIdSeparator - 1) |> int
{ Name = name; SectorID = id; Checksum = checksum }
let calculateChecksum =
Seq.filter Char.IsLetter
>> Seq.countBy id
>> Seq.sortBy (fun (c, _) -> c)
>> Seq.sortByDescending (fun (_, count) -> count)
>> Seq.map (fun (c, _) -> c)
>> Seq.take 5
>> Seq.toArray
>> String
let matchesChecksum room =
let actualChecksum = calculateChecksum room.Name
room.Checksum = actualChecksum
let decryptName room =
let shift c =
match c with
| '-' -> ' '
| ' ' -> ' '
| 'z' -> 'a'
| _ -> char (int c + 1)
let shiftN c =
{ 1 .. room.SectorID }
|> Seq.fold (fun c' _ -> shift c') c
let decryptedName =
room.Name
|> Seq.map shiftN
|> Seq.toArray
|> String
{ room with Name = decryptedName }
let partOne =
List.filter matchesChecksum
>> List.sumBy (fun room -> room.SectorID)
let partTwo =
List.filter matchesChecksum
>> List.map decryptName
>> List.find (fun room -> room.Name = "northpole object storage")
[<EntryPoint>]
let main argv =
let input =
File.ReadLines("Input.txt")
|> Seq.map parseRoom
|> Seq.toList
partOne input |> printfn "Part one: %d"
partTwo input |> printfn "Part two: %A"
0