-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day07.cs
135 lines (112 loc) · 3.81 KB
/
Day07.cs
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Collections.Generic;
using System.Text;
using AdventOfCode.CSharp.Common;
namespace AdventOfCode.CSharp.Y2020.Solvers;
public class Day07 : ISolver
{
readonly record struct Bag(string Modifier, string Colour);
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
var containsShinyGoldCache = new Dictionary<Bag, bool>();
var totalChildBagsCache = new Dictionary<Bag, int>();
var bagContents = new Dictionary<Bag, List<(int Count, Bag Bag)>>();
var reader = new SpanReader(input);
while (!reader.Done)
{
ParseLine(ref reader, out Bag bag, out bool containsShiny, out List<(int Count, Bag Bag)> contents);
if (containsShiny)
{
containsShinyGoldCache[bag] = true;
}
else if (contents.Count == 0)
{
containsShinyGoldCache[bag] = false;
totalChildBagsCache[bag] = 0;
}
bagContents[bag] = contents;
}
int part1 = 0;
foreach (Bag colour in bagContents.Keys)
{
if (ContainsShinyGold(colour, containsShinyGoldCache, bagContents))
{
part1++;
}
}
solution.SubmitPart1(part1);
int part2 = GetTotalChildBags(new("shiny", "gold"), totalChildBagsCache, bagContents);
solution.SubmitPart2(part2);
}
private static void ParseLine(
ref SpanReader reader,
out Bag bag,
out bool containsShinyGold,
out List<(int Count, Bag Bag)> bagContents)
{
containsShinyGold = false;
bag = new(BytesToString(reader.ReadUntil(' ')), BytesToString(reader.ReadUntil(' ')));
reader.SkipLength("bags contain ".Length);
bagContents = new List<(int Count, Bag Colour)>();
if (reader.Peek() == 'n') // no other bags
{
reader.SkipLength("no other bags.\n".Length);
return;
}
while (true)
{
int count = reader.ReadPosIntUntil(' ');
Bag childBag = new(BytesToString(reader.ReadUntil(' ')), BytesToString(reader.ReadUntil(' ')));
if (childBag is { Modifier: "shiny", Colour: "gold" })
{
containsShinyGold = true;
}
bagContents.Add((count, childBag));
reader.SkipLength(count == 1 ? "bag".Length : "bags".Length);
if (reader.Peek() == '.')
{
reader.SkipLength(".\n".Length);
return;
}
reader.SkipLength(", ".Length);
}
}
private static string BytesToString(ReadOnlySpan<byte> str) => Encoding.ASCII.GetString(str);
private static bool ContainsShinyGold(
Bag bag,
Dictionary<Bag, bool> cache,
Dictionary<Bag, List<(int Count, Bag Bag)>> bagContents)
{
if (cache.TryGetValue(bag, out bool containsShinyGold))
{
return containsShinyGold;
}
foreach ((int _, Bag childBag) in bagContents[bag])
{
if (ContainsShinyGold(childBag, cache, bagContents))
{
cache[bag] = true;
return true;
}
}
cache[bag] = false;
return false;
}
private static int GetTotalChildBags(
Bag bag,
Dictionary<Bag, int> cache,
Dictionary<Bag, List<(int Count, Bag Bag)>> bagContents)
{
if (cache.TryGetValue(bag, out int childCount))
{
return childCount;
}
int total = 0;
foreach ((int count, Bag childBag) in bagContents[bag])
{
total += count * (1 + GetTotalChildBags(childBag, cache, bagContents));
}
cache[bag] = total;
return total;
}
}