-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay_5.cs
134 lines (107 loc) · 5.03 KB
/
Day_5.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
namespace AdventOfCode_2023
{
public class Day_5 : Day
{
public class MapLine
{
public long DestinationStart { get; set; }
public long SourceStart { get; set; }
public long RangeLength { get; set; }
}
private List<long> getNeededSeeds()
{
return inputs[0].Split(":")[1].Trim().Split(" ").Select(s => long.Parse(s)).ToList();
}
private List<MapLine> buildMap(string mapName)
{
List<MapLine> map = new List<MapLine>();
int currentLine = 0;
while (!inputs[currentLine].StartsWith($"{mapName} map:"))
currentLine++;
currentLine++;
while (currentLine < inputs.Length && !string.IsNullOrWhiteSpace(inputs[currentLine]))
{
string[] datas = inputs[currentLine].Split(" ");
MapLine mapLine = new MapLine
{
DestinationStart = long.Parse(datas[0]),
SourceStart = long.Parse(datas[1]),
RangeLength = long.Parse(datas[2])
};
map.Add(mapLine);
currentLine++;
}
return map.OrderByDescending(m => m.SourceStart).ToList();
}
private long searchValueInMap(long searchedValue, List<MapLine> map)
{
long res = searchedValue;
foreach (MapLine line in map)
{
if (searchedValue >= line.SourceStart && searchedValue < line.SourceStart + line.RangeLength)
{
return line.DestinationStart + (searchedValue - line.SourceStart);
}
}
return res;
}
public override long FirstPart()
{
long lowestLocation = long.MaxValue;
List<long> neededSeeds = getNeededSeeds();
List<MapLine> seedToSoilMap = buildMap("seed-to-soil");
List<MapLine> soilToFertilizerMap = buildMap("soil-to-fertilizer");
List<MapLine> fertilizerToWaterMap = buildMap("fertilizer-to-water");
List<MapLine> waterToLightMap = buildMap("water-to-light");
List<MapLine> lightToTemperatureMap = buildMap("light-to-temperature");
List<MapLine> temperatureToHumidityMap = buildMap("temperature-to-humidity");
List<MapLine> humidityToLocationMap = buildMap("humidity-to-location");
foreach (var seed in neededSeeds)
{
long soil = searchValueInMap(seed, seedToSoilMap);
long fertilizer = searchValueInMap(soil, soilToFertilizerMap);
long water = searchValueInMap(fertilizer, fertilizerToWaterMap);
long light = searchValueInMap(water, waterToLightMap);
long temperature = searchValueInMap(light, lightToTemperatureMap);
long humidity = searchValueInMap(temperature, temperatureToHumidityMap);
long location = searchValueInMap(humidity, humidityToLocationMap);
if (location < lowestLocation)
lowestLocation = location;
}
return lowestLocation;
}
// still too slow
public override long SecondPart()
{
return -1;
long lowestLocation = long.MaxValue;
List<long> neededSeeds = getNeededSeeds();
List<MapLine> seedToSoilMap = buildMap("seed-to-soil");
List<MapLine> soilToFertilizerMap = buildMap("soil-to-fertilizer");
List<MapLine> fertilizerToWaterMap = buildMap("fertilizer-to-water");
List<MapLine> waterToLightMap = buildMap("water-to-light");
List<MapLine> lightToTemperatureMap = buildMap("light-to-temperature");
List<MapLine> temperatureToHumidityMap = buildMap("temperature-to-humidity");
List<MapLine> humidityToLocationMap = buildMap("humidity-to-location");
for (int i = 0; i < neededSeeds.Count; i++)
{
long rangeStart = neededSeeds[i];
i++;
long rangeLength = neededSeeds[i];
for (long seed = rangeStart; seed < rangeStart + rangeLength; seed++)
{
long soil = searchValueInMap(seed, seedToSoilMap);
long fertilizer = searchValueInMap(soil, soilToFertilizerMap);
long water = searchValueInMap(fertilizer, fertilizerToWaterMap);
long light = searchValueInMap(water, waterToLightMap);
long temperature = searchValueInMap(light, lightToTemperatureMap);
long humidity = searchValueInMap(temperature, temperatureToHumidityMap);
long location = searchValueInMap(humidity, humidityToLocationMap);
if (location < lowestLocation)
lowestLocation = location;
}
}
return lowestLocation;
}
}
}