-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day5.cs
166 lines (148 loc) · 5.46 KB
/
Day5.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
namespace AoC2023;
public class Day5 : IDay<IEnumerable<string>, long>
{
public static long SolvePart1(IEnumerable<string> input)
{
var min = long.MaxValue;
var inputArr = input.ToArray();
var seedsTypes = inputArr[0].Split(": ")[1].Split(' ').Select(long.Parse);
var (sourceToDestination, states) = BuildMapAndStatesList(inputArr);
foreach (var seedType in seedsTypes)
{
var currentState = "seed";
var currentType = seedType;
long nextType = 0;
for (int i = 0; i < states.Count - 1; i++)
{
var nextState = states[i + 1];
var typeRanges = sourceToDestination[$"{currentState}-to-{nextState}"]!;
var existingSourceRange = typeRanges.FirstOrDefault(
tr => currentType >= tr.Source && currentType <= tr.Source + tr.Count
);
if (existingSourceRange != default)
{
nextType =
existingSourceRange.Destination + currentType - existingSourceRange.Source;
}
else
{
nextType = currentType;
}
currentState = nextState;
currentType = nextType;
}
if (nextType < min)
{
min = nextType;
}
}
return min;
}
// 🤕!
public static long SolvePart2(IEnumerable<string> input)
{
var inputArr = input.ToArray();
List<List<MapEntry>> maps = [];
var seedRanges = inputArr[0].Split(": ")[1].Split(' ').Select(long.Parse);
var inputSeeds = new Stack<LongRange>(
seedRanges.Chunk(2).Select(pair => new LongRange(pair[0], pair[0] + pair[1]))
);
for (int i = 2; i < inputArr.Length; i++)
{
var line = inputArr[i];
if (string.IsNullOrWhiteSpace(line))
{
continue;
}
if (line.EndsWith(':'))
{
maps.Add([]);
continue;
}
var dataStr = line.Split(' ');
var destination = long.Parse(dataStr[0]);
var source = long.Parse(dataStr[1]);
var count = long.Parse(dataStr[2]);
maps.Last().Add(new MapEntry(source, destination, count));
}
foreach (var item in maps)
{
Stack<LongRange> newSeeds = [];
while (inputSeeds.Count != 0)
{
var sr = inputSeeds.Pop();
var finished = true;
foreach (var range in item)
{
var leftOverlap = Math.Max(sr.Low, range.Source);
var rightOverlap = Math.Min(sr.High, range.Source + range.Length);
if (leftOverlap < rightOverlap)
{
newSeeds.Push(
new LongRange(
leftOverlap - range.Source + range.Destination,
rightOverlap - range.Source + range.Destination
)
);
if (leftOverlap > sr.Low)
{
inputSeeds.Push(new LongRange(sr.Low, leftOverlap));
}
if (sr.High > rightOverlap)
{
inputSeeds.Push(new LongRange(rightOverlap, sr.High));
}
finished = false;
break;
}
}
if (finished == true)
{
newSeeds.Push(new LongRange(sr.Low, sr.High));
}
}
inputSeeds = newSeeds;
}
return inputSeeds.Min(r => r.Low);
}
public record struct MapEntry(long Source, long Destination, long Length);
public record struct LongRange(long Low, long High);
public record struct MapRule(long Source, long Destination, long Count);
private static Tuple<Dictionary<string, List<MapRule>>, List<string>> BuildMapAndStatesList(
string[] inputArr
)
{
List<string> states = ["seed"];
var sourceToDestination = new Dictionary<string, List<MapRule>>();
var sourceToDestinationStr = "";
for (int i = 2; i < inputArr.Length; i++)
{
var line = inputArr[i];
if (string.IsNullOrEmpty(line))
{
sourceToDestinationStr = "";
continue;
}
if (line.EndsWith(':'))
{
sourceToDestinationStr = line.Split(' ')[0];
states.Add(sourceToDestinationStr.Split('-')[2]);
continue;
}
var dataStr = line.Split(' ');
var destination = long.Parse(dataStr[0]);
var source = long.Parse(dataStr[1]);
var count = long.Parse(dataStr[2]);
var key = new MapRule(source, destination, count);
if (sourceToDestination.TryGetValue(sourceToDestinationStr, out var existingList))
{
existingList.Add(key);
}
else
{
sourceToDestination[sourceToDestinationStr] = [key];
}
}
return Tuple.Create(sourceToDestination, states);
}
}