From cf52f4996e2e9506848939ef9242da6e6af2b42a Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Mon, 6 Dec 2021 10:26:03 +0100 Subject: [PATCH] Added day 6. --- source/app.d | 4 ++-- source/day6/input.txt | 1 + source/day6/part1.d | 28 ++++++++++++++++++++++++++++ source/day6/test_input.txt | 1 + 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 source/day6/input.txt create mode 100644 source/day6/part1.d create mode 100644 source/day6/test_input.txt diff --git a/source/app.d b/source/app.d index 9d7dd67..ccb63b7 100644 --- a/source/app.d +++ b/source/app.d @@ -1,7 +1,7 @@ import std.stdio; -import day5.part1; +import day6.part1; void main() { - hydrothermalVents(); + lanternFish(); } diff --git a/source/day6/input.txt b/source/day6/input.txt new file mode 100644 index 0000000..229caa9 --- /dev/null +++ b/source/day6/input.txt @@ -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 diff --git a/source/day6/part1.d b/source/day6/part1.d new file mode 100644 index 0000000..309e209 --- /dev/null +++ b/source/day6/part1.d @@ -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); +} \ No newline at end of file diff --git a/source/day6/test_input.txt b/source/day6/test_input.txt new file mode 100644 index 0000000..55129f1 --- /dev/null +++ b/source/day6/test_input.txt @@ -0,0 +1 @@ +3,4,3,1,2