Xah Talk Show 2024-12-04 Ep601, Wolfram Language, Advent of Code 2024, Day 4, Part 2

By Xah Lee. Date: .
harbin girl hugging wolf xmas 2024-12-04
harbin girl hugging wolf xmas 2024-12-04

🎄

part 2

xah talk show 2024-12-04 ep601 DC3kt
xah talk show 2024-12-04 ep601 DC3kt
(* scratch pad with explanation *)

xinput =
"MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX"

xinput = ReadString @ "c:/Users/xah/web/xahlee_info/comp/advent/advent_of_code_2024_4_input.txt";

(*
problem, count all words MAS that forms a diagonal cross.
word can go from bottom diagnoal to top.

e.g.

M.S
.A.
M.S

Each cross, count as 1 occurrence.
Problem is, find total count of such cross..

 *)

(*
naive approach.
Identify all occurrence of A that is not on the edge of the matrix.
Then, for each, check its corners.
If both of the 2 diagonal corners are M and S, order doesn't matter, then it's valid.
*)

(* 
more fancy approach.
generate all 3 by 3 submatrix.
then just check each.
this would be very inefficient.
also, unable to find such function during livestream.
 *)

xmatrix = Map[ Characters , StringSplit[ xinput ]]

{rowCount, colCount} = Dimensions @ xmatrix

(*
{{M, M, M, S, X, X, M, A, S, M},
{M, S, A, M, X, M, S, M, S, A},
{A, M, X, S, X, M, A, A, M, M},
{M, S, A, M, A, S, M, S, M, X},
{X, M, A, S, A, M, X, A, M, M},
{X, X, A, M, M, X, X, A, M, A},
{S, M, S, M, S, A, S, X, S, S},
{S, A, X, A, M, A, S, A, A, A},
{M, A, M, M, M, X, M, M, M, M},
{M, X, M, X, A, X, M, A, S, X}}
 *)

(* positions of occurrence of A *)
xpositions = Position[ xmatrix, "A", {-1} ]

(*
{{1, 8}, {2, 3}, {2, 10}, {3, 1}, {3, 7}, {3, 8}, {4, 3}, {4, 5}, {5, 3}, {5, 5}, {5, 8}, {6, 3}, {6, 8}, {6, 10}, {7, 6}, {8, 2}, {8, 4}, {8, 6}, {8, 8}, {8, 9}, {8, 10}, {9, 2}, {10, 5}, {10, 8}}
 *)

(* remove the A that are on the edge of matrix *)
xpositions = Cases[ xpositions, {Except[1|rowCount] , Except[1|colCount] } ]

(* {{2, 3}, {3, 7}, {3, 8}, {4, 3}, {4, 5}, {5, 3}, {5, 5}, {5, 8}, {6, 3}, {6, 8}, {7, 6}, {8, 2}, {8, 4}, {8, 6}, {8, 8}, {8, 9}, {9, 2}} *)

Function[{x}, Count[ x, True ]] @
Map[
Function[{xpos},
(
(Extract[ xmatrix , (xpos + {1,1}) ] === "M"
&&
Extract[ xmatrix , (xpos + {-1,-1}) ] === "S")
||
(Extract[ xmatrix , (xpos + {1,1}) ] === "S"
&&
Extract[ xmatrix , (xpos + {-1,-1}) ] === "M")
)
&&
(
(Extract[ xmatrix , (xpos + {1,-1}) ] === "M"
&&
Extract[ xmatrix , (xpos + {-1,1}) ] === "S")
||
(Extract[ xmatrix , (xpos + {1,-1}) ] === "S"
&&
Extract[ xmatrix , (xpos + {-1,1}) ] === "M")
)

],
xpositions]
(* answer for toy input 9 *)

(* answer for toy input 1936 *)

One-Line Solution

xinput =
"MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX"

xinput = Import @ "http://xahlee.info/comp/advent/advent_of_code_2024_4_input.txt";

(* answer for toy input 9 *)
(* answer for personal input 1936 *)

xmatrix = Map[ Characters , StringSplit[ xinput ]]

Count[
Cases[ Position[ xmatrix, "A", {-1} ], {Except[1|(Length @ xmatrix)] , Except[1| (Length @ xmatrix[[1]]) ] } ] /. {ii_Integer, jj_Integer} :>
(((( {1, 1} == "M" && {-1,-1} == "S") || ( {1, 1} == "S" && {-1,-1} == "M")) && (( {1,-1} == "M" && {-1, 1} == "S") || ( {1,-1} == "S" && {-1, 1} == "M"))) /. {{x_, y_} :> Extract[ xmatrix, ({x,y} + {ii,jj}) ]}) ,
True ]