Added day 6.

This commit is contained in:
Andrew Lalis 2021-12-06 10:26:03 +01:00
parent fb08d89d85
commit cf52f4996e
4 changed files with 32 additions and 2 deletions

View File

@ -1,7 +1,7 @@
import std.stdio; import std.stdio;
import day5.part1; import day6.part1;
void main() { void main() {
hydrothermalVents(); lanternFish();
} }

1
source/day6/input.txt Normal file
View File

@ -0,0 +1 @@
1,2,1,1,1,1,1,1,2,1,3,1,1,1,1,3,1,1,1,5,1,1,1,4,5,1,1,1,3,4,1,1,1,1,1,1,1,5,1,4,1,1,1,1,1,1,1,5,1,3,1,3,1,1,1,5,1,1,1,1,1,5,4,1,2,4,4,1,1,1,1,1,5,1,1,1,1,1,5,4,3,1,1,1,1,1,1,1,5,1,3,1,4,1,1,3,1,1,1,1,1,1,2,1,4,1,3,1,1,1,1,1,5,1,1,1,2,1,1,1,1,2,1,1,1,1,4,1,3,1,1,1,1,1,1,1,1,5,1,1,4,1,1,1,1,1,3,1,3,3,1,1,1,2,1,1,1,1,1,1,1,1,1,5,1,1,1,1,5,1,1,1,1,2,1,1,1,4,1,1,1,2,3,1,1,1,1,1,1,1,1,2,1,1,1,2,3,1,2,1,1,5,4,1,1,2,1,1,1,3,1,4,1,1,1,1,3,1,2,5,1,1,1,5,1,1,1,1,1,4,1,1,4,1,1,1,2,2,2,2,4,3,1,1,3,1,1,1,1,1,1,2,2,1,1,4,2,1,4,1,1,1,1,1,5,1,1,4,2,1,1,2,5,4,2,1,1,1,1,4,2,3,5,2,1,5,1,3,1,1,5,1,1,4,5,1,1,1,1,4

28
source/day6/part1.d Normal file
View File

@ -0,0 +1,28 @@
module day6.part1;
import std.file;
import std.stdio;
import std.math;
import std.string;
import std.algorithm;
import std.conv;
import std.array;
import std.parallelism;
ulong computeGrowth(ulong initial, ulong t) {
return (t <= initial) ? 1 : computeGrowth(6, t - initial - 1) + computeGrowth(8, t - initial - 1);
}
void lanternFish() {
auto f = File("source/day6/input.txt", "r");
ulong t = 256;
ulong[] fish = f.readln().strip().split(",").map!(s => s.to!ulong).array;
ulong total = 0;
// WARNING!!! Only use this on a PC with >= 24 threads.
writefln("Using %d threads.", taskPool.size());
foreach (idx, i; taskPool.parallel(fish, 1)) {
total += computeGrowth(i, t);
writefln("Computed growth for fish %d", idx);
}
writefln("%d", total);
}

View File

@ -0,0 +1 @@
3,4,3,1,2