Day 1, Dlang version
This commit is contained in:
parent
ffab9138f4
commit
51868fb59f
1 changed files with 37 additions and 0 deletions
37
1.d
Executable file
37
1.d
Executable file
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue