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

Brute forcing does not work

This commit is contained in:
Chris Josten 2020-12-13 21:41:28 +01:00
parent fb9815b6ec
commit f013067d26
Signed by: chris
GPG key ID: 1795A594046530AB

View file

@ -7,8 +7,9 @@ struct ParsedInput {
Variant run(int part, File input, bool bigboy, string[] args) {
ParsedInput pInput = input.byLineCopy.array.parseInput();
Variant result = parts!int(part,
() => part1(pInput));
Variant result = parts!long(part,
() => part1(pInput),
() => part2(pInput, 100000000000000));
return result;
}
@ -17,17 +18,15 @@ Variant run(int part, File input, bool bigboy, string[] args) {
ParsedInput input;
input.timestamp = to!int(range[0]);
input.busses = range[1].splitter(',').filter!(c => c != "x").map!(to!int).array;
input.busses = range[1].splitter(',').map!(c => c == "x" ? -1 : c.to!int).array;
writeln(input);
return input;
}
int part1(ParsedInput input) {
import std.math;
int earliestBus = 0;
int earliestBusTimestamp = int.max;
foreach(int bus; input.busses) {
foreach(int bus; input.busses.filter!(x => x != -1)) {
int closestTimestamp = (input.timestamp / bus + 1 ) * bus;
writeln(closestTimestamp);
if (closestTimestamp < earliestBusTimestamp) {
@ -35,10 +34,45 @@ int part1(ParsedInput input) {
earliestBus = bus;
}
}
writefln("Bus %d at %d", earliestBus, earliestBusTimestamp);
// debug writefln("Bus %d at %d", earliestBus, earliestBusTimestamp);
return earliestBus * (earliestBusTimestamp - input.timestamp);
}
long part2(ParsedInput input) {
long result = 0;
long startI = startFrom == 0 ? 0 : startFrom / input.busses[0];
for (long i = startI; result == 0; i++) {
long offset = i * input.busses[0];
bool fail = false;
foreach(j, subsequentBus; input.busses[1..$].enumerate(1)) {
if (subsequentBus == -1) continue;
if ((offset + j) % subsequentBus != 0) {
fail = true;
break;
}
}
if (!fail) {
result = offset;
}
debug {
if (i % 10000 == 0) {
writef("\rAt %d", offset);
stdout.flush();
}
}
}
debug writeln();
return result;
}
unittest {
string[] input = [
"939",
@ -48,4 +82,5 @@ unittest {
ParsedInput pInput = parseInput(input);
assert(part1(pInput) == 295);
assert(part2(pInput) == 1068781);
}