This commit is contained in:
nazrin 2024-12-02 10:58:54 +00:00
parent 51868fb59f
commit ac2d6076fc

49
2.d Executable file
View file

@ -0,0 +1,49 @@
#!/bin/env rdmd
module aoc24_2;
import std;
auto absDiff(T)(T a, T b) if(isSigned!T){
return abs(a - b);
}
bool isWithinRange(T)(T n, T low, T high){
return n >= low && n <= high;
}
struct Skipper(T){ // Just smile and wave, boys. Smile and wave
T range;
long iter = -1;
bool empty(){
return iter == range.length;
}
void popFront(){
iter++;
}
auto front(){
if(iter == -1)
return tuple(iter, range.array);
return tuple(iter, joiner([range[0 .. iter], range[iter+1 .. $]]).array);
}
}
void main(){
size_t totalSafe, totalMostlySafe;
foreach(line; stdin.byLine){
auto nums = line.split(' ').map!(to!int);
foreach(l, lnums; Skipper!(typeof(nums))(nums)){
bool isIncreasing = lnums.isStrictlyMonotonic!"b > a";
bool isDecreasing = lnums.isStrictlyMonotonic!"a > b";
bool differLittle = lnums.slide(2).all!(x => absDiff(x[0], x[1]).isWithinRange(1, 3));
if((isIncreasing || isDecreasing) && differLittle){
if(l == -1)
totalSafe++;
totalMostlySafe++;
break;
}
}
}
writeln("Part one ", totalSafe);
writeln("Part two ", totalMostlySafe);
}