-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay11.cs
More file actions
60 lines (54 loc) · 1.84 KB
/
Day11.cs
File metadata and controls
60 lines (54 loc) · 1.84 KB
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
using System.Diagnostics;
using System;
using System.Collections.Immutable;
namespace _2025._11
{
public sealed class Day11 : Base
{
private Dictionary<string, string[]> _devices = [];
private Dictionary<(string, bool, bool), long> _cache = [];
public Day11(bool example) : base(example)
{
Day = "11";
}
public override void Reset()
{
_cache.Clear();
}
private long FindAllPaths(string curr, bool visitedDac = true, bool visitedFft = true)
{
(string, bool, bool) key = (curr, visitedDac, visitedFft);
if (_cache.TryGetValue(key, out long visitedPaths))
{
return visitedPaths;
}
if (curr == "out")
{
_cache[key] = visitedDac && visitedFft ? 1L : 0L;
return _cache[key];
}
_cache[key] = _devices[curr]
.Sum(output => output switch
{
"dac" => FindAllPaths(output, true, visitedFft),
"fft" => FindAllPaths(output, visitedDac, true),
_ => FindAllPaths(output, visitedDac, visitedFft)
});
return _cache[key];
}
public override object PartOne()
{
_devices = ReadInput()
.Select(x =>x.Split(' '))
.ToDictionary(x => x[0][..^1], x => x[1..]);
return FindAllPaths("you");
}
public override object PartTwo()
{
_devices = (Example ? File.ReadAllLines(Path.Combine(ClassPath, "example_stage2")) : ReadInput())
.Select(x =>x.Split(' '))
.ToDictionary(x => x[0][..^1], x => x[1..]);
return FindAllPaths("svr", false, false);
}
}
}