From 51868fb59ffffa1ee421b5c417b013624370e0bd78fd623f97e5edd8b4a28a12 Mon Sep 17 00:00:00 2001 From: nazrin Date: Sun, 1 Dec 2024 17:21:13 +0000 Subject: [PATCH] Day 1, Dlang version --- 1.d | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100755 1.d diff --git a/1.d b/1.d new file mode 100755 index 0000000..3e4e537 --- /dev/null +++ b/1.d @@ -0,0 +1,37 @@ +#!/bin/env rdmd +module aoc24_1; + +import std.stdio, std.conv, std.algorithm, std.range, std.math, std.parallelism, std.string; + +void main(){ + long[] left, right; + long[long] rightCount; + + foreach(line; stdin.byLine){ + long l = line.parse!long; + line = line.strip(); + long r = line.parse!long; + + left ~= l; + right ~= r; + + if(r !in rightCount) + rightCount[r] = 0; + rightCount[r]++; + } + + foreach(x; iota(0, 2).parallel){ + if(x == 0) + left.sort(); + else + right.sort(); + } + + foreach(x; iota(0, 2).parallel){ + if(x == 0) + writeln("Part one ", zip(left, right).map!(x => abs(x[0] - x[1])).reduce!"a+b"); + else + writeln("Part two ", left.map!(l => rightCount.get(l, 0) * l).reduce!"a+b"); + } +} +