This repository has been archived by the owner on Nov 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle17.fs
82 lines (67 loc) · 2.29 KB
/
puzzle17.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
// https://adventofcode.com/2020/day/17
//
#if !INTERACTIVE
module Puzzle17
#else
#load "common.fs"
#endif
open System.IO
open common
let input = read2d (Path.Combine(__SOURCE_DIRECTORY__, "puzzle17.txt"))
type State = | Active | Inactive
type Cube = (int * int * int * int)
let dimension =
input
|> Array2D.mapi (fun i j c -> (i, j, 0, 0), match c with | '#' -> Active | _ -> Inactive)
|> Seq.cast<Cube * State>
|> Map.ofSeq
let evolve adjacent dimension =
let activeKeys = Map.toSeq dimension |> Seq.filter (snd >> (=)Active) |> Seq.map fst |> Array.ofSeq
let activeAdjacent = activeKeys |> Seq.collect adjacent |> Array.ofSeq
let newDimension =
activeAdjacent
|> Seq.groupBy id |> Seq.map (fun (p, s) -> p, Seq.length s)
|> Seq.map (fun (cube, activeAdjCount) ->
let state = dimension |> Map.tryFind cube
match state, activeAdjCount with
| Some Active, 2 -> cube, Active
| _, 3 -> cube, Active
| _, _ -> cube, Inactive)
|> Map.ofSeq
newDimension
module Part1 =
let adjacent (i, j, k, _) =
[k-1..k+1]
|> Seq.allPairs [j-1..j+1]
|> Seq.allPairs [i-1..i+1]
|> Seq.map (fun (i', (j', k')) -> i', j', k', 0)
|> Seq.except [i,j,k,0]
let answer =
dimension
|> evolve adjacent
|> evolve adjacent
|> evolve adjacent
|> evolve adjacent
|> evolve adjacent
|> evolve adjacent
|> Map.toSeq
|> Seq.filter (snd >> (=)Active) |> Seq.length
module Part2 =
let adjacent (i, j, k, z) =
[z-1..z+1]
|> Seq.allPairs [k-1..k+1]
|> Seq.allPairs [j-1..j+1]
|> Seq.allPairs [i-1..i+1]
|> Seq.map (fun (i', (j', (k', z'))) -> i', j', k', z')
|> Seq.except [i,j,k,z]
let answer =
dimension
|> evolve adjacent
|> evolve adjacent
|> evolve adjacent
|> evolve adjacent
|> evolve adjacent
|> evolve adjacent
|> Map.toSeq
|> Seq.filter (snd >> (=)Active) |> Seq.length