xinput =
"7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9";
xinput = Import[ "http://xahlee.info/comp/advent/advent_of_code_2024_2_input.txt" ];
xinput = ToExpression @ Map[ StringSplit , StringSplit[ xinput, "\n" ] ] ;
(* returns True or False *)
fCheckRow =
Function[{xlist},
(OrderedQ @ xlist || OrderedQ @ Reverse @ xlist ) &&
(And @@ Map[(( 0 < # <= 3 ) &) , Abs /@ (Differences @ xlist)])
];
(*
ANALYSIS.
check the diff list has no 0.
then check if it's all positive, or all negative.
if so, the row is good.
*)(*
BRUTE FORCE OUR WAY.
write a function ff that check if the row pass.
then map ff on a list of rows, each one is with one item removed.
if, any of this pass, then it is pass.
*)(* fCheckRow[{82,80,79,76,73,70,67,67776}] *)(* return True or False *)
rowMaybeRemoveOneItem = ((fCheckRow[#] || (Or @@ (fCheckRow /@ (Subsets[# , { Length @ # -1} ])))) &);
Count[ Map[ rowMaybeRemoveOneItem , xinput] , True ]
(* answer for toy input is 4 *)(* answer for personal input is 644 *)