49 lines
1.1 KiB
D
Executable file
49 lines
1.1 KiB
D
Executable file
#!/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);
|
|
}
|
|
|