1
0
Fork 0
mirror of https://github.com/HenkKalkwater/aoc-2020 synced 2024-11-22 19:15:18 +00:00
aoc-2020/source/day13.d

108 lines
2.5 KiB
D
Raw Normal View History

2020-12-14 02:12:15 +00:00
import std.algorithm;
import std.typecons;
2020-12-13 10:39:50 +00:00
import dayutil;
struct ParsedInput {
2020-12-14 02:12:15 +00:00
long timestamp;
long[] busses;
2020-12-13 10:39:50 +00:00
}
Variant run(int part, File input, bool bigboy, string[] args) {
ParsedInput pInput = input.byLineCopy.array.parseInput();
2020-12-13 20:41:28 +00:00
Variant result = parts!long(part,
() => part1(pInput),
2020-12-14 02:12:15 +00:00
() => part2(pInput));
2020-12-13 10:39:50 +00:00
return result;
}
ParsedInput parseInput(Range)(Range range) if (isInputRange!Range && isSomeString!(ElementType!Range)) {
import std.conv;
ParsedInput input;
input.timestamp = to!int(range[0]);
2020-12-14 02:12:15 +00:00
input.busses = range[1].splitter(',').map!(c => c == "x" ? -1 : c.to!long).array;
2020-12-13 10:39:50 +00:00
return input;
}
2020-12-14 02:12:15 +00:00
long part1(ParsedInput input) {
long earliestBus = 0;
long earliestBusTimestamp = long.max;
foreach(long bus; input.busses.filter!(x => x != -1)) {
long closestTimestamp = (input.timestamp / bus + 1 ) * bus;
2020-12-13 10:39:50 +00:00
writeln(closestTimestamp);
if (closestTimestamp < earliestBusTimestamp) {
earliestBusTimestamp = closestTimestamp;
earliestBus = bus;
}
}
2020-12-13 20:41:28 +00:00
// debug writefln("Bus %d at %d", earliestBus, earliestBusTimestamp);
2020-12-13 10:39:50 +00:00
return earliestBus * (earliestBusTimestamp - input.timestamp);
}
2020-12-13 20:41:28 +00:00
long part2(ParsedInput input) {
2020-12-14 02:12:15 +00:00
/+long[] remainders;
long[] divisors;
// Too much, but we're not short on memory
remainders.reserve(input.busses.length);
divisors.reserve(input.busses.length);
foreach (remainder, divisor; input.busses) {
if (divisor < 0) continue;
remainders ~= [remainder % divisor];
divisors ~= [divisor];
}
2020-12-13 20:41:28 +00:00
2020-12-14 02:12:15 +00:00
writeln(remainders);
writeln(divisors);
2020-12-13 20:41:28 +00:00
2020-12-14 02:12:15 +00:00
long N = divisors.fold!((a,b) => a * b);
writeln(N);
long[] Ns; Ns.length = divisors.length;
long[] Xs; Xs.length = divisors.length;
long[] BNXs; BNXs.length = divisors.length;
2020-12-13 20:41:28 +00:00
2020-12-14 02:12:15 +00:00
foreach(i, divisor; divisors) {
Ns[i] = N / divisor;
}
2020-12-13 20:41:28 +00:00
2020-12-14 02:12:15 +00:00
foreach(ref Xi, Ni, Di; lockstep(Xs, Ns, divisors)) {
long a = Ni % Di;
long b;
for(b = 0; (a * b) % Di != 1; b++) {}
Xi = b;
}
writeln(Xs);
2020-12-13 20:41:28 +00:00
2020-12-14 02:12:15 +00:00
foreach(ref BNXi, Ri, Ni, Xi; lockstep(BNXs, remainders, Ns, Xs)) {
BNXi = Ri * Ni * Xi;
}
long result = BNXs.sum() % N;+/
Tuple!(size_t, "index", long, "value")[] busses = input.busses.enumerate.filter!((e) => e.value != -1).array;
2020-12-13 20:41:28 +00:00
2020-12-14 02:12:15 +00:00
long time = 0;
long step = busses[0].value;
2020-12-13 20:41:28 +00:00
2020-12-14 02:12:15 +00:00
foreach(bus; busses[1..$]) {
for(; (time + bus.index) % bus.value != 0; time += step) {}
step *= bus.value;
2020-12-13 20:41:28 +00:00
}
2020-12-14 02:12:15 +00:00
return time;
2020-12-13 20:41:28 +00:00
}
2020-12-13 10:39:50 +00:00
unittest {
string[] input = [
"939",
"7,13,x,x,59,x,31,19"
];
ParsedInput pInput = parseInput(input);
assert(part1(pInput) == 295);
2020-12-14 02:12:15 +00:00
ParsedInput p2;
p2.busses = [-1, 7, -1, 5, -1, -1, 8];
//assert(part2(p2) == 78);
2020-12-13 20:41:28 +00:00
assert(part2(pInput) == 1068781);
2020-12-13 10:39:50 +00:00
}