Skip to content

Commit 644de61

Browse files
committed
2023 Day 16
1 parent d17bfdf commit 644de61

File tree

4 files changed

+210
-9
lines changed

4 files changed

+210
-9
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"request": "launch",
1717
"program": "${workspaceFolder}/2023/bin/Debug/net8.0/2023.dll",
1818
"args": [
19-
"13"
19+
"16"
2020
],
2121
"cwd": "${workspaceFolder}/2023",
2222
"stopAtEntry": false,

2023/16.fs

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,89 @@ open AdventOfCode
44
open FSharpPlus
55
open FParsec
66

7-
let parser = pint32
7+
let parser = ParseUtils.grid (anyOf ".|\\/-")
88

9-
let solve1 input = 0
9+
type Dir =
10+
| Up
11+
| Down
12+
| Left
13+
| Right
1014

11-
let solve2 input = 0
15+
let dirToDelta =
16+
function
17+
| Up -> 0, -1
18+
| Down -> 0, 1
19+
| Left -> -1, 0
20+
| Right -> 1, 0
21+
22+
let cellToDirs dir cell =
23+
match (dir, cell) with
24+
| _, '.' -> Seq.singleton dir
25+
26+
| _, '|' when dir = Up || dir = Down -> Seq.singleton dir
27+
| _, '|' -> [ Up; Down ]
28+
29+
| _, '-' when dir = Left || dir = Right -> Seq.singleton dir
30+
| _, '-' -> [ Left; Right ]
31+
32+
| Right, '/' -> Seq.singleton Up
33+
| Up, '/' -> Seq.singleton Right
34+
| Left, '/' -> Seq.singleton Down
35+
| Down, '/' -> Seq.singleton Left
36+
37+
| Right, '\\' -> Seq.singleton Down
38+
| Down, '\\' -> Seq.singleton Right
39+
| Up, '\\' -> Seq.singleton Left
40+
| Left, '\\' -> Seq.singleton Up
41+
42+
| _ -> failwith "Not exhaustive match"
43+
44+
let countEnergised grid start =
45+
let bounds = (Array.length (Array.item 0 grid), Array.length grid)
46+
47+
let fNeighbors (p, dir) =
48+
cellToDirs dir (Array.item2dp p grid)
49+
|> Seq.map (fun dir -> (Tuple2.add (dirToDelta dir) p, dir), 1)
50+
|> Seq.filter (fun ((p, _), _) -> Tuple2.le (0, 0) p && Tuple2.lt p bounds)
51+
52+
Graph.flood fNeighbors start |> Seq.map (fst >> fst) |> Seq.distinct |> Seq.length
53+
54+
let solve1 input = countEnergised input ((0, 0), Right)
55+
56+
let solve2 input =
57+
let allStarts =
58+
Seq.concat [ seq { 0 .. Array.length (Array.item 0 input) - 1 } |> Seq.map (fun x -> (x, 0), Down)
59+
seq { 0 .. Array.length (Array.item 0 input) - 1 }
60+
|> Seq.map (fun x -> (x, Array.length input - 1), Up)
61+
seq { 0 .. Array.length input - 1 } |> Seq.map (fun y -> (0, y), Right)
62+
seq { 0 .. Array.length input - 1 }
63+
|> Seq.map (fun y -> (Array.length (Array.item 0 input) - 1, y), Left) ]
64+
65+
allStarts |> Seq.map (countEnergised input) |> Seq.max
1266

1367
let solution = makeSolution () parser solve1 solve2
1468

1569
module Tests =
1670
open Xunit
1771
open FsUnit.Xunit
1872

19-
let input = [| "" |]
73+
let input =
74+
[| @".|...\...."
75+
@"|.-.\....."
76+
@".....|-..."
77+
@"........|."
78+
@".........."
79+
@".........\"
80+
@"..../.\\.."
81+
@".-.-/..|.."
82+
@".|....-|.\"
83+
@"..//.|...."
84+
@"" |]
2085

2186
[<Fact>]
2287
let ``Example part 1`` () =
23-
testPart1 solution input |> should equal 0
88+
testPart1 solution input |> should equal 46
2489

25-
// [<Fact>]
26-
// let ``Example part 2`` () =
27-
// testPart2 solution input |> should equal 0
90+
[<Fact>]
91+
let ``Example part 2`` () =
92+
testPart2 solution input |> should equal 51

2023/in/16.in

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
....-.....-.......|..--.......\....\..................................................../...........\.........
83+
...........................-.\..\......................................\......|.......-........-.....|........
84+
..-.......\............................../...................|..-.....................-......./..\.......-.-..
85+
....|.....-./.................\......./...........|.............................-....|........-............-..
86+
.....-...............|...........-.......|/..\-.............|-|........\...........|./...../..................
87+
|...................-../../.....|./..................|........|....\..../.....|....................../......./
88+
...............-./\.......|.......|./....................-....\...................-.......................|...
89+
.../..........\...........|.....-..../..-..-.......................|.......|.....-./......................|...
90+
......-..\..\........................................|...-................................|.............|.....
91+
...........|............-\.........../..\....../.....\....../...........|...........|.../............\........
92+
|....-..........-.|...........-..-..................-..\......\.......--............|.................|\\.....
93+
.........../|.-..........|...\.|./......../...........\...........\/....-..........\...................../....
94+
.........................-...../.....-./.-............................\...........-...............\.\/....../.
95+
..../........-...................../............../....\............................./............../.../.....
96+
..................-/...-......................|....................\......\..../.........-......./............
97+
....../....................-....\...-.-.....//...|.....................\.........\............\.-....-../.....
98+
......-.....\......|.|........................|.................-............../......./......................
99+
....../......../.........\..................|...|..................................|..-..........|....\.......
100+
..........\.../......................\.......\/......................-....|.........................../.\.....
101+
.........-..............-.|...../../............................\../...............-.............\......../...
102+
...\............\......................\.\......\........\............\...-.....|./...-.............|....|.-..
103+
/........................../.-..\|.-........../.-............./../.........-.../..............................
104+
|....|.\.....-..|....|....../........|.|...........\-......................|......|..........................|
105+
.......................\...........................................\..........|.............-.../.............
106+
.\.........|.............|................................\..................|||........\...-|................
107+
..........\...............\.....................-.....|....................-..........\|..................|.|.
108+
...|...........\.|......|/.-../....../.................../......\.........................|\.\..............\.
109+
..........................................................\......................./.....--..........|...-..\..
110+
........-/...-................/........\..........................\..../........................\........-....

Common/Graph.fs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,29 @@ let connectedComponent
121121
Seq.toList visited
122122

123123
doSearch ()
124+
125+
let flood
126+
(fNeighbors: 'vertex -> ('vertex * int) seq)
127+
(start: 'vertex )
128+
=
129+
let visited = Dictionary<'vertex, int32>()
130+
let fringe = PriorityQueue<'vertex, int32>()
131+
132+
visited.Add(start, 0) |> ignore
133+
fringe.Enqueue(start, 0)
134+
135+
let rec doSearch () =
136+
match fringe.TryDequeue () with
137+
| true, v, c ->
138+
139+
for (n, cc) in fNeighbors v do
140+
match visited.TryAdd(n, c + cc) with
141+
| true -> fringe.Enqueue(n, c + cc)
142+
| false -> ()
143+
144+
doSearch()
145+
| false, _, _->
146+
Seq.map (fun (p: KeyValuePair<'vertex, int32>) -> p.Key, p.Value) visited |> Seq.toList
147+
148+
doSearch ()
149+

0 commit comments

Comments
 (0)