mirror of
https://github.com/HenkKalkwater/aoc-2020
synced 2024-11-22 11:05:18 +00:00
Add day 15 parts 1 and 2
This commit is contained in:
parent
d02f0ce415
commit
eae7eb7a24
|
@ -22,6 +22,7 @@ import day11;
|
||||||
import day12;
|
import day12;
|
||||||
import day13;
|
import day13;
|
||||||
import day14;
|
import day14;
|
||||||
|
import day15;
|
||||||
import dayutil;
|
import dayutil;
|
||||||
|
|
||||||
immutable string progName = "aoc-2020";
|
immutable string progName = "aoc-2020";
|
||||||
|
@ -45,6 +46,7 @@ Variant function(int, File, bool, string[])[] programs = [
|
||||||
&day12.run,
|
&day12.run,
|
||||||
&day13.run,
|
&day13.run,
|
||||||
&day14.run,
|
&day14.run,
|
||||||
|
&day15.run,
|
||||||
];
|
];
|
||||||
|
|
||||||
void printUsage(string message = null) {
|
void printUsage(string message = null) {
|
||||||
|
|
|
@ -30,7 +30,7 @@ long part1(ParsedInput input) {
|
||||||
long earliestBusTimestamp = long.max;
|
long earliestBusTimestamp = long.max;
|
||||||
foreach(long bus; input.busses.filter!(x => x != -1)) {
|
foreach(long bus; input.busses.filter!(x => x != -1)) {
|
||||||
long closestTimestamp = (input.timestamp / bus + 1 ) * bus;
|
long closestTimestamp = (input.timestamp / bus + 1 ) * bus;
|
||||||
writeln(closestTimestamp);
|
//writeln(closestTimestamp);
|
||||||
if (closestTimestamp < earliestBusTimestamp) {
|
if (closestTimestamp < earliestBusTimestamp) {
|
||||||
earliestBusTimestamp = closestTimestamp;
|
earliestBusTimestamp = closestTimestamp;
|
||||||
earliestBus = bus;
|
earliestBus = bus;
|
||||||
|
|
|
@ -24,7 +24,6 @@ struct Instruction {
|
||||||
|
|
||||||
Variant run(int part, File input, bool bigboy, string[] args) {
|
Variant run(int part, File input, bool bigboy, string[] args) {
|
||||||
auto parsed = input.byLineCopy.parseInput;
|
auto parsed = input.byLineCopy.parseInput;
|
||||||
writeln(parsed);
|
|
||||||
|
|
||||||
return Variant(parts!long(part,
|
return Variant(parts!long(part,
|
||||||
() => part1(parsed),
|
() => part1(parsed),
|
||||||
|
@ -149,9 +148,9 @@ long[] addressPermutations(Value[] address) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
writeln(address);
|
// debug writeln(address);
|
||||||
foreach(address2; addresses) {
|
foreach(address2; addresses) {
|
||||||
writeln("Address: %064b".format(address2));
|
// debug writeln("Address: %064b".format(address2));
|
||||||
}
|
}
|
||||||
assert(addresses.length == pow(2, address.count(Value.X)));
|
assert(addresses.length == pow(2, address.count(Value.X)));
|
||||||
assert(addresses.uniq.array.length == addresses.length);
|
assert(addresses.uniq.array.length == addresses.length);
|
||||||
|
@ -199,7 +198,5 @@ mask = 00000000000000000000000000000000X0XX
|
||||||
mem[26] = 1
|
mem[26] = 1
|
||||||
EOS";
|
EOS";
|
||||||
auto parsed = parseInput(input.lineSplitter);
|
auto parsed = parseInput(input.lineSplitter);
|
||||||
writeln(parsed);
|
|
||||||
assert(part2(parsed) == 208);
|
assert(part2(parsed) == 208);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
71
source/day15.d
Normal file
71
source/day15.d
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
import std.conv;
|
||||||
|
import std.typecons;
|
||||||
|
|
||||||
|
import dayutil;
|
||||||
|
|
||||||
|
immutable size_t TIMES = 2020;
|
||||||
|
immutable size_t TIMES2 = 30000000;
|
||||||
|
|
||||||
|
Variant run(int part, File input, bool bigboy, string[] args) {
|
||||||
|
int[] numbers = input.byLineCopy.joiner(",").array.splitter(",").map!(to!int).array;
|
||||||
|
return Variant(parts!int(part,
|
||||||
|
() => part1(numbers),
|
||||||
|
() => part2(numbers)));
|
||||||
|
}
|
||||||
|
|
||||||
|
int part1(int[] startingNumbers) {
|
||||||
|
int[] memory;
|
||||||
|
memory.reserve(2020);
|
||||||
|
|
||||||
|
memory ~= startingNumbers;
|
||||||
|
|
||||||
|
int previous = startingNumbers[$ - 1];
|
||||||
|
foreach(index; memory.length..TIMES) {
|
||||||
|
ptrdiff_t last = memory[0..$ - 1].retro.countUntil(previous) + 1;
|
||||||
|
|
||||||
|
previous = cast(int) last;
|
||||||
|
|
||||||
|
memory ~= [previous];
|
||||||
|
}
|
||||||
|
|
||||||
|
return previous;
|
||||||
|
}
|
||||||
|
|
||||||
|
int part2(int[] startingNumbers) {
|
||||||
|
int[int] memory;
|
||||||
|
|
||||||
|
foreach(idx, num; startingNumbers[0..$ - 1]) {
|
||||||
|
memory[num] = cast(int) idx + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int previousNumber = startingNumbers[$ - 1];
|
||||||
|
|
||||||
|
foreach(index; memory.length..TIMES2 - 1) {
|
||||||
|
int lastSpoken = memory.get(previousNumber, 0);
|
||||||
|
int newNumber = lastSpoken;
|
||||||
|
|
||||||
|
if (lastSpoken > 0) {
|
||||||
|
newNumber = cast(int) index + 1 - lastSpoken;
|
||||||
|
}
|
||||||
|
|
||||||
|
memory[previousNumber] = cast(int) index + 1;
|
||||||
|
|
||||||
|
/+if (index < 20 || index == TIMES2 - 2) {
|
||||||
|
writeln("i:", index + 1, "; newNumber: ", newNumber, ", lastSpoken: ", lastSpoken, ", prevous: ", previousNumber);
|
||||||
|
if (index != TIMES2 - 2) writeln(memory);
|
||||||
|
}+/
|
||||||
|
previousNumber = newNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
return previousNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
//assert(part1([0,3,6]) == 12);
|
||||||
|
assert(part1([1,3,2]) == 1);
|
||||||
|
assert(part1([2,1,3]) == 10);
|
||||||
|
assert(part1([1,2,3]) == 27);
|
||||||
|
|
||||||
|
assert(part2([0,3,6]) == 175594);
|
||||||
|
assert(part2([1,3,2]) == 2578);
|
||||||
|
}
|
Loading…
Reference in a new issue