1
0
Fork 0
mirror of https://github.com/HenkKalkwater/aoc-2020 synced 2024-11-22 11:05:18 +00:00

Added day 9 part 2

This commit is contained in:
Chris Josten 2020-12-09 16:38:42 +01:00
parent 3e2632b2af
commit 404605b092
Signed by: chris
GPG key ID: 1795A594046530AB

View file

@ -11,21 +11,67 @@ Variant run(int part, File input, bool bigboy, string[] args) {
import std.conv; import std.conv;
auto numbers = input.byLineCopy.array.map!(to!uint); auto numbers = input.byLineCopy.array.map!(to!uint);
return Variant(parts!uint(part, return Variant(parts!uint(part,
() => part1(numbers))); () => part1(numbers),
() => part2(numbers)));
} }
T part1(Range, T = ElementType!Range)(Range range, int preambleSize = 25) ptrdiff_t indexOfWeakness(Range)(Range range, int preambleSize = 25)
if (isForwardRange!Range && isNumeric!T) { if (isForwardRange!Range)
foreach(window; range.slide!(No.withPartial)(preambleSize + 1)) { in (range.length > preambleSize)
out (r; r >= preambleSize || r == -1)
do {
foreach(i, window; range.slide!(No.withPartial)(preambleSize + 1).enumerate) {
if(!window[0..preambleSize].any!(e => window.canFind!(x => e + x == window[preambleSize]))) { if(!window[0..preambleSize].any!(e => window.canFind!(x => e + x == window[preambleSize]))) {
return window[preambleSize]; return preambleSize + i;
} }
} }
return -1; return -1;
} }
T part1(Range, T = ElementType!Range)(Range range, int preambleSize = 25)
if (isForwardRange!Range)
in (range.length > preambleSize) {
auto index = indexOfWeakness(range, preambleSize);
assert(index >= 0);
return range[index];
}
unittest { unittest {
int[] numbers = [35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299, int[] numbers = [35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299,
277, 309, 576]; 277, 309, 576];
assert(part1(numbers, 5) == 127); assert(part1(numbers, 5) == 127);
} }
T part2(Range, T = ElementType!Range)(Range range, int preambleSize = 25)
if (isForwardRange!Range)
in (range.length > preambleSize)
do {
T num = part1(range, preambleSize);
for (size_t offset = 0; offset <= range.length; offset++) {
T min = T.max;
T max = T.min;
size_t offset_offset = 0;
T sum = 0;
for(; sum < num && offset + offset_offset < range.length; sum += range[offset + offset_offset++]) {
T curNumber = range[offset + offset_offset];
if (curNumber < min) min = curNumber;
if (curNumber > max) max = curNumber;
}
if (sum == num) {
return min + max;
}
}
return -1;
}
unittest {
int[] numbers = [35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299,
277, 309, 576];
assert(part2(numbers, 5) == 62);
}