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"); + } +} +