From f4b4845e3853fb12c6ffc7d88d820de405d9a928 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Thu, 2 Dec 2021 14:29:16 +0100 Subject: [PATCH] Added day 2. --- source/app.d | 3 +- source/day2/input.txt | 1000 +++++++++++++++++++++++++++++++++++++++++ source/day2/part1.d | 44 ++ 3 files changed, 1046 insertions(+), 1 deletion(-) create mode 100644 source/day2/input.txt create mode 100644 source/day2/part1.d diff --git a/source/app.d b/source/app.d index b03f9df..f872ae5 100644 --- a/source/app.d +++ b/source/app.d @@ -2,7 +2,8 @@ import std.stdio; import day1.part1; import day1.part2; +import day2.part1; void main() { - slidingSum(); + dive2(); } diff --git a/source/day2/input.txt b/source/day2/input.txt new file mode 100644 index 0000000..171b7da --- /dev/null +++ b/source/day2/input.txt @@ -0,0 +1,1000 @@ +forward 6 +down 8 +down 5 +down 9 +forward 2 +down 5 +down 5 +forward 1 +forward 7 +down 8 +up 2 +down 4 +up 8 +down 8 +forward 3 +forward 4 +down 1 +forward 5 +up 7 +down 7 +down 8 +forward 2 +up 3 +forward 1 +forward 6 +forward 9 +forward 7 +forward 8 +forward 2 +forward 3 +up 2 +up 8 +down 1 +forward 7 +down 7 +down 2 +forward 6 +down 1 +forward 5 +down 3 +forward 6 +down 7 +up 1 +up 3 +forward 7 +forward 6 +forward 8 +down 4 +down 2 +up 5 +down 2 +forward 2 +up 5 +forward 6 +down 3 +down 1 +down 5 +forward 6 +up 6 +down 7 +down 8 +down 2 +forward 3 +down 5 +down 4 +forward 7 +forward 9 +up 9 +up 8 +up 4 +forward 8 +forward 5 +down 4 +up 2 +forward 9 +up 5 +down 5 +up 9 +forward 2 +forward 3 +down 6 +down 8 +forward 8 +up 5 +down 5 +forward 7 +forward 6 +forward 8 +up 3 +forward 3 +forward 1 +up 8 +down 8 +down 2 +down 4 +up 7 +up 2 +up 9 +up 4 +forward 6 +down 8 +down 1 +forward 6 +forward 6 +down 4 +down 2 +up 7 +down 9 +down 9 +up 2 +up 7 +down 4 +down 2 +forward 1 +down 1 +up 5 +up 5 +forward 9 +up 3 +down 7 +forward 7 +down 4 +down 8 +up 1 +down 4 +down 7 +forward 5 +up 9 +forward 5 +forward 1 +forward 8 +forward 6 +forward 5 +forward 1 +down 4 +down 6 +forward 5 +forward 2 +forward 3 +down 1 +up 2 +up 9 +forward 4 +up 8 +down 7 +down 8 +up 7 +down 2 +forward 7 +up 1 +forward 5 +forward 1 +forward 8 +forward 1 +up 8 +down 6 +down 7 +forward 2 +down 8 +down 8 +forward 8 +up 8 +down 6 +down 7 +down 4 +down 7 +forward 6 +up 3 +forward 3 +down 2 +down 8 +down 3 +down 9 +forward 9 +forward 7 +down 6 +down 4 +forward 6 +down 2 +down 7 +up 7 +up 8 +forward 2 +forward 8 +down 3 +up 2 +forward 9 +down 2 +up 3 +down 1 +down 1 +down 4 +down 8 +up 2 +up 8 +forward 2 +forward 1 +up 1 +forward 7 +down 8 +down 1 +down 7 +up 3 +down 3 +forward 8 +forward 2 +forward 7 +down 2 +up 9 +up 3 +up 5 +down 4 +up 3 +forward 4 +up 5 +down 9 +down 9 +forward 2 +forward 2 +down 2 +down 8 +down 3 +down 5 +forward 6 +down 6 +up 5 +down 2 +down 4 +down 9 +down 3 +forward 7 +down 1 +forward 1 +down 4 +up 1 +down 9 +forward 5 +up 2 +down 3 +forward 8 +forward 9 +up 9 +down 2 +forward 8 +down 4 +down 5 +forward 6 +forward 5 +forward 4 +down 6 +down 9 +down 2 +forward 9 +down 4 +up 8 +up 9 +up 2 +up 5 +up 5 +forward 9 +up 1 +forward 6 +forward 7 +forward 8 +forward 9 +up 2 +forward 3 +forward 4 +forward 6 +forward 9 +up 5 +up 5 +down 3 +forward 1 +forward 3 +forward 2 +forward 3 +forward 6 +forward 7 +down 4 +down 2 +down 1 +forward 2 +down 5 +forward 3 +forward 6 +down 8 +down 9 +forward 4 +forward 6 +down 6 +down 6 +forward 3 +down 6 +down 8 +down 1 +forward 7 +forward 9 +down 2 +down 5 +forward 1 +forward 3 +down 2 +forward 1 +down 8 +down 1 +forward 4 +down 8 +forward 5 +forward 1 +down 7 +down 7 +forward 3 +forward 1 +forward 6 +forward 7 +forward 5 +up 1 +forward 2 +down 9 +forward 3 +up 1 +forward 2 +down 1 +down 6 +down 3 +forward 7 +down 5 +down 4 +down 1 +forward 9 +forward 9 +down 5 +forward 7 +forward 3 +forward 5 +down 1 +forward 6 +down 8 +up 2 +forward 6 +down 3 +forward 2 +forward 9 +forward 4 +down 1 +down 3 +forward 9 +forward 3 +forward 8 +forward 9 +up 3 +up 1 +forward 1 +forward 2 +down 8 +down 9 +down 2 +down 1 +down 3 +down 2 +forward 9 +forward 7 +down 5 +forward 1 +forward 6 +forward 3 +forward 9 +down 2 +forward 8 +down 5 +down 1 +forward 5 +forward 3 +down 6 +forward 6 +down 8 +forward 2 +up 5 +forward 1 +down 2 +down 6 +forward 9 +forward 7 +down 1 +down 3 +down 6 +up 3 +down 4 +forward 8 +forward 1 +forward 7 +down 2 +down 5 +down 9 +forward 6 +down 5 +forward 5 +up 1 +down 5 +forward 8 +up 9 +forward 2 +down 6 +forward 2 +forward 7 +up 2 +down 9 +down 7 +up 7 +down 6 +up 5 +forward 1 +down 8 +forward 8 +forward 1 +forward 7 +down 9 +down 6 +forward 3 +down 6 +down 1 +down 1 +down 1 +down 3 +down 7 +down 7 +down 3 +down 5 +forward 4 +down 4 +forward 7 +forward 5 +down 9 +down 9 +forward 7 +down 3 +down 9 +down 4 +forward 3 +down 7 +down 2 +forward 2 +down 6 +forward 9 +forward 9 +forward 5 +up 4 +down 7 +down 2 +up 9 +up 4 +forward 8 +forward 1 +down 8 +up 5 +down 4 +down 3 +forward 2 +down 7 +down 2 +down 1 +down 9 +forward 7 +forward 7 +up 8 +up 4 +down 3 +down 8 +forward 6 +forward 5 +forward 5 +forward 5 +down 3 +down 8 +forward 4 +forward 7 +forward 1 +up 3 +up 9 +down 6 +up 4 +down 7 +forward 8 +forward 4 +forward 3 +up 8 +up 3 +down 3 +forward 6 +down 2 +forward 7 +forward 4 +forward 8 +down 3 +down 9 +down 9 +down 2 +forward 8 +up 4 +down 3 +forward 8 +forward 5 +forward 7 +down 6 +up 9 +forward 3 +down 2 +forward 5 +forward 2 +down 7 +forward 6 +forward 2 +up 9 +down 1 +down 1 +forward 4 +up 1 +forward 9 +down 3 +down 4 +down 2 +forward 3 +forward 3 +forward 3 +up 7 +up 8 +down 5 +forward 1 +forward 7 +up 9 +up 3 +down 3 +down 8 +forward 6 +up 5 +up 5 +forward 4 +down 2 +down 8 +down 1 +forward 6 +down 3 +forward 3 +forward 6 +forward 1 +up 3 +up 1 +down 5 +down 2 +down 7 +down 1 +forward 9 +down 4 +down 8 +forward 9 +forward 7 +forward 8 +down 1 +down 2 +up 7 +down 5 +down 2 +down 1 +up 4 +up 8 +up 7 +down 4 +forward 3 +down 2 +down 2 +forward 5 +forward 4 +down 8 +up 4 +forward 4 +up 1 +down 3 +down 9 +down 9 +down 3 +up 8 +forward 1 +forward 6 +down 6 +down 2 +forward 8 +down 3 +forward 8 +forward 2 +forward 9 +up 3 +forward 6 +down 5 +forward 6 +forward 2 +up 7 +down 9 +forward 2 +up 2 +forward 7 +down 1 +down 5 +down 6 +forward 8 +down 6 +forward 4 +forward 1 +forward 3 +forward 4 +up 4 +forward 4 +down 4 +forward 2 +forward 5 +forward 2 +forward 5 +down 9 +up 2 +up 1 +down 2 +up 4 +up 5 +forward 2 +down 3 +down 9 +forward 3 +down 8 +down 9 +forward 5 +down 3 +forward 5 +down 3 +up 8 +forward 7 +forward 1 +down 2 +down 7 +forward 3 +down 8 +forward 9 +down 4 +down 1 +down 7 +down 4 +up 5 +forward 1 +down 4 +forward 1 +forward 8 +up 1 +up 5 +up 2 +up 2 +down 4 +down 7 +forward 2 +down 8 +up 8 +down 9 +down 3 +down 6 +down 3 +down 1 +forward 7 +up 8 +forward 5 +up 5 +down 8 +down 1 +down 8 +down 6 +down 5 +forward 2 +up 5 +down 6 +forward 9 +up 6 +down 5 +down 7 +up 9 +down 1 +forward 4 +up 6 +forward 2 +down 5 +down 5 +forward 2 +up 6 +forward 1 +down 8 +forward 4 +up 8 +down 3 +forward 8 +down 8 +forward 5 +down 6 +down 3 +forward 1 +down 4 +down 8 +up 1 +down 1 +down 2 +up 9 +forward 2 +forward 3 +down 7 +down 2 +forward 7 +up 8 +down 2 +down 8 +down 9 +up 1 +down 5 +down 5 +down 4 +down 8 +down 9 +up 5 +forward 2 +down 4 +down 3 +down 2 +forward 5 +forward 8 +down 8 +down 1 +forward 9 +down 5 +forward 5 +down 2 +up 3 +up 9 +down 1 +down 9 +forward 7 +up 7 +forward 3 +up 6 +forward 8 +down 2 +down 1 +down 7 +forward 5 +down 8 +down 4 +forward 7 +forward 4 +down 6 +forward 9 +down 3 +forward 2 +down 3 +down 1 +down 1 +up 1 +up 3 +down 6 +forward 3 +up 9 +down 4 +up 2 +down 3 +up 1 +down 8 +down 5 +forward 7 +forward 2 +forward 9 +down 8 +down 5 +down 6 +up 3 +forward 2 +up 8 +down 4 +forward 7 +down 8 +down 6 +down 4 +forward 7 +up 9 +down 4 +forward 2 +forward 5 +down 3 +up 6 +up 6 +down 2 +down 4 +forward 8 +forward 5 +forward 3 +forward 5 +down 5 +down 5 +down 6 +forward 3 +forward 7 +forward 1 +down 8 +down 5 +forward 7 +up 7 +down 9 +down 9 +down 9 +up 6 +down 2 +down 3 +forward 1 +up 7 +up 8 +forward 5 +down 1 +down 3 +down 3 +forward 5 +down 7 +down 1 +up 2 +down 2 +down 3 +forward 7 +down 9 +forward 6 +down 5 +forward 2 +down 5 +forward 6 +up 3 +down 8 +up 2 +forward 5 +forward 1 +forward 5 +forward 8 +forward 6 +forward 9 +forward 6 +up 6 +up 5 +down 8 +down 3 +down 5 +down 2 +forward 9 +forward 8 +down 1 +up 1 +up 6 +down 6 +forward 4 +down 3 +forward 6 +forward 1 +up 5 +down 6 +up 9 +down 7 +down 2 +down 9 +down 5 +forward 5 +up 2 +forward 8 +down 2 +down 8 +forward 6 +down 4 +forward 8 +down 7 +down 8 +down 1 +forward 3 +down 6 +down 9 +down 3 +forward 3 +down 8 +forward 8 +down 7 +forward 6 +forward 8 +down 8 +up 7 +down 1 +forward 2 +forward 3 +down 5 +up 8 +down 3 +down 4 +down 7 +forward 9 +forward 7 +forward 1 +down 3 +forward 9 +down 8 +forward 2 +down 2 +down 9 +down 4 +down 3 +up 6 +up 9 +down 3 +down 2 +forward 5 +down 3 +down 2 +down 8 +forward 6 +forward 5 +up 4 +forward 9 +forward 8 +forward 9 +down 2 +forward 2 +up 6 +forward 1 +down 5 +forward 2 +down 8 +up 2 +up 3 +down 3 +up 2 +up 1 +up 5 +forward 1 +forward 2 +down 8 +up 3 +down 9 +forward 7 +up 5 +down 4 +down 4 +up 3 +forward 2 +up 5 +down 4 +down 4 +up 5 +forward 8 +down 8 +down 6 +forward 7 +down 1 +down 3 +down 1 +forward 3 +down 5 +down 3 +forward 3 +up 2 +forward 2 +down 9 +up 8 +forward 8 +up 8 +forward 1 +forward 9 +forward 3 +down 8 +down 3 +forward 8 +forward 4 +down 2 +forward 2 +down 2 +down 5 +down 7 +down 5 +forward 8 +up 3 +forward 1 +down 1 +forward 3 +down 9 +forward 2 +forward 2 +forward 7 +down 7 +down 2 +forward 9 +up 5 +up 7 +forward 8 +forward 1 +down 7 +down 8 +down 3 +forward 6 diff --git a/source/day2/part1.d b/source/day2/part1.d new file mode 100644 index 0000000..fc0caad --- /dev/null +++ b/source/day2/part1.d @@ -0,0 +1,44 @@ +module day2.part1; + +import std.file; +import std.stdio; +import std.string; +import std.conv; +import std.typecons; + +Tuple!(string, int) parseOp(char[] op) { + auto parts = op.split(); + int x = parts[1].to!int; + string d = parts[0].to!string; + return tuple(d, x); +} + +void dive() { + File f = File("source/day2/input.txt", "r"); + int depth = 0; + int position = 0; + foreach (line; f.byLine) { + auto op = parseOp(line); + if (op[0] == "up") depth -= op[1]; + if (op[0] == "down") depth += op[1]; + if (op[0] == "forward") position += op[1]; + } + writefln("Final depth: %d, Final position: %d, Product: %d", depth, position, depth * position); +} + +void dive2() { + File f = File("source/day2/input.txt", "r"); + int aim = 0; + int depth = 0; + int position = 0; + foreach (line; f.byLine) { + auto op = parseOp(line); + if (op[0] == "up") aim -= op[1]; + if (op[0] == "down") aim += op[1]; + if (op[0] == "forward") { + position += op[1]; + depth += aim * op[1]; + } + } + writefln("Final depth: %d, Final position: %d, Product: %d", depth, position, depth * position); +}