Xah Talk Show 2024-12-02 Ep598, Wolfram Language, Advent of Code 2024, Day 2

By Xah Lee. Date: .
vidthumb 1jRTEtpjbFg
vidthumb 1jRTEtpjbFg
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" ] ] ;

Function[{x}, Count[ x, True ]] @
Map[ Function[{x},
And[
Or[ OrderedQ @ x, OrderedQ[ Reverse @ x ] ],
And @@ Map[ Function[{x}, 0 < x <= 3] , Map[Abs , Differences @ x]]
]
] , xinput]

(* answer for toy input is 2 *)
(* answer for personal input is 606 *)

part 2

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 *)
advent of code Wolfram 2024-12-02 XPsrF
advent of code Wolfram 2024-12-02 XPsrF