-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day5.cs
153 lines (134 loc) · 3.84 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
using System.Text.RegularExpressions;
namespace AdventOfCode2023
{
internal class Day5
{
public static (int, int) FindBlock(string[] lines, string header)
{
int startBlockIndex = 0;
int endBlockIndex = 0;
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].Contains(header))
{
startBlockIndex = i + 1;
}
if ((startBlockIndex != 0 && string.IsNullOrEmpty(lines[i])) || (i == lines.Length - 1))
{
endBlockIndex = i - 1;
break;
}
}
return (startBlockIndex, endBlockIndex);
}
public static List<(int, int)> GetAllBlocks(string[] lines)
{
List<(int, int)> blocks = new()
{
FindBlock(lines, "seed-to-soil map:"),
FindBlock(lines, "soil-to-fertilizer map:"),
FindBlock(lines, "fertilizer-to-water map:"),
FindBlock(lines, "water-to-light map:"),
FindBlock(lines, "light-to-temperature map:"),
FindBlock(lines, "temperature-to-humidity map:"),
FindBlock(lines, "humidity-to-location map:")
};
return blocks;
}
public static void RunPuzzle1()
{
string[] lines = File.ReadAllLines(@"Day5-Input.txt");
Match seedMatch = Regex.Match(lines[0], "^seeds: (.*)");
string seedLine = seedMatch.Groups[1].Value;
List<long> seeds = new();
Regex regex = new Regex(@"\d+");
foreach (Match match in regex.Matches(seedLine))
{
seeds.Add(long.Parse(match.Value));
}
List<(int, int)> blocks = GetAllBlocks(lines);
long lowestLocation = long.MaxValue;
foreach (long seed in seeds)
{
long nextNumber = seed;
foreach ((int, int) block in blocks)
{
for (int x = block.Item1; x <= block.Item2; x++)
{
string[] nrs = lines[x].Split(' ');
long destStartRange = long.Parse(nrs[0]);
long srcStartRange = long.Parse(nrs[1]);
long rangeLength = long.Parse(nrs[2]);
long maxSrcRange = srcStartRange + rangeLength;
if (nextNumber >= srcStartRange && nextNumber <= maxSrcRange)
{
nextNumber = nextNumber - srcStartRange + destStartRange;
break;
}
}
}
if (nextNumber < lowestLocation)
{
lowestLocation = nextNumber;
}
}
Console.WriteLine("Answer Puzzle 1: " + lowestLocation);
}
public static void RunPuzzle2()
{
string[] lines = File.ReadAllLines(@"Day5-Input.txt");
Match seedMatch = Regex.Match(lines[0], "^seeds: (.*)");
string seedLine = seedMatch.Groups[1].Value;
List<long> seeds = new();
List<long> seedRange = new();
Regex regex = new Regex(@"\d+");
bool isSeed = true;
foreach (Match match in regex.Matches(seedLine))
{
if (isSeed)
{
seeds.Add(long.Parse(match.Value));
isSeed = false;
}
else
{
seedRange.Add(long.Parse(match.Value));
isSeed = true;
}
}
List<(int, int)> blocks = GetAllBlocks(lines);
long lowestLocation = long.MaxValue;
//foreach (long seed in seeds)
//{
// long minSeedRange = seed;
// long maxSeedRange = seed + seedRange.Last();
// for (long i = seed; i < maxSeedRange; i++)
// {
// long nextNumber = i;
// foreach ((int, int) block in blocks)
// {
// for (int x = block.Item1; x <= block.Item2; x++)
// {
// string[] nrs = lines[x].Split(' ');
// long destStartRange = long.Parse(nrs[0]);
// long srcStartRange = long.Parse(nrs[1]);
// long rangeLength = long.Parse(nrs[2]);
// long maxSrcRange = srcStartRange + rangeLength;
// if (nextNumber >= srcStartRange && nextNumber <= maxSrcRange)
// {
// nextNumber = nextNumber - srcStartRange + destStartRange;
// break;
// }
// }
// }
// if (nextNumber < lowestLocation)
// {
// lowestLocation = nextNumber;
// Console.WriteLine("Seed: {0}, Lowest Location: {1}", seed, lowestLocation);
// }
// }
//}
Console.WriteLine("Answer Puzzle 2: " + lowestLocation);
}
}
}