Skip to content

Instantly share code, notes, and snippets.

@FeepingCreature
Created December 2, 2021 15:26
Show Gist options
  • Save FeepingCreature/4b18ce81d604882a4827e4805d2db353 to your computer and use it in GitHub Desktop.
Save FeepingCreature/4b18ce81d604882a4827e4805d2db353 to your computer and use it in GitHub Desktop.
module day2;
macro import neat.macros.listcomprehension;
macro import neat.macros.assert;
import std.stdio;
import std.file;
import std.string;
alias command = (
:forward, int |
:down, int |
:up, int);
command parse(string line) {
auto parts = line.split(" ");
if (parts[0] == "forward") return (:forward, parts[1].atoi);
else if (parts[0] == "down") return (:down, parts[1].atoi);
else if (parts[0] == "up") return (:up, parts[1].atoi);
else assert(false);
}
void main() {
auto lines = "day2.txt".readText.split("\n");
auto commands = [line.parse for line in lines where line.length];
// todo reduce/fold
mut int progress = 0;
mut int depth = 0;
for (command in commands) command.case {
(:forward, int fwd): progress += fwd;
(:down, int down): depth += down;
(:up, int up): depth -= up;
}
print("progress $progress, depth $depth, result $(progress * depth)");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment