Xah Talk Show 2024-10-26 Ep591, Wolfram Language, Advent of Code 2023, Day 14 Part 2, take 2
xinput =
"O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....";
xmatrix = Map[ Characters , StringSplit[ xinput ]];
xrowcount = Length[ xmatrix ];
xcolumncount = Length[ xmatrix[[1]] ];
moveNorth = Function[{xx},
Transpose @ ReplaceRepeated[ Transpose @ xx,
{head___,Pattern[dots, Longest[ ".".. ] ],Pattern[balls, Longest[ "O".. ] ],tail___} -> {head,balls, dots, tail}
] ];
moveWest =
Function[{xx},
ReplaceRepeated[ xx,
{head___,Pattern[dots, Longest[ ".".. ] ],Pattern[balls, Longest[ "O".. ] ],tail___} -> {head,balls, dots, tail}
] ];
moveSouth =
Function[{xx}, Transpose @ ReplaceRepeated[ Transpose @ xx,
{head___, Pattern[balls, Longest[ "O".. ] ], Pattern[dots, Longest[ ".".. ] ], tail___} -> {head,dots,balls,tail}
] ];
moveEast =
Function[{xx}, ReplaceRepeated[ xx,
{head___, Pattern[balls, Longest[ "O".. ] ], Pattern[dots, Longest[ ".".. ] ], tail___} -> {head,dots,balls,tail}
] ];
f1cycle = Function[{yy}, yy // moveNorth // moveWest // moveSouth // moveEast];
xallmoved = Nest[ f1cycle, xmatrix, 100 ]
Total @ MapIndexed[ Function[{xval, xindex},
Count[ xval, "O" ] * (xrowcount - xindex[[1]] +1) ],
xallmoved ]
numberOfCycles = 30;
listOfMoves =
Flatten @
Table[ {
HoldForm @ moveNorth,
HoldForm @ moveWest,
HoldForm @ moveSouth,
HoldForm @ moveEast}, {numberOfCycles}];
boardhistory =
Table[
Fold[
Function[{arg, ff}, ReleaseHold @ ff[arg] ],
xmatrix ,
Take[ listOfMoves, x ]
] ,
{x, 1, 4 * numberOfCycles}];
Map[ MatrixForm, boardhistory];
Manipulate[ boardhistory[[n]] // MatrixForm , {n, 1, numberOfCycles 4, 1} ]
Manipulate[
boardhistory[[n]] // MatrixForm , {n, 1, numberOfCycles 4, 1} ]
Manipulate[
boardhistory[[n]] // MatrixForm , {n, 1, numberOfCycles 4, 4} ]
xFindNestPeriod = ResourceFunction["FindNestPeriod"];
xPeriodity = xFindNestPeriod[f1cycle , xmatrix, 1000]
xFinalBoardConfig =
Nest[
f1cycle ,
xmatrix,
Mod[ 10^9 , xPeriodity ]
]
Total @ MapIndexed[ Function[{xval, xindex},
Count[ xval, "O" ] * (xrowcount - xindex[[1]] +1) ],
xFinalBoardConfig ]
FindNestPeriod[f_, init_, maxStepsToTry_ : 100000] :=
Block[{triedsteps = 0},
With[{pointoncycle =
NestWhile[{triedsteps++; f[First[#]], f[f[Last[#]]]} &, {init,
f[init]}, UnsameQ @@ # &, 1, maxStepsToTry]},
If[triedsteps == maxStepsToTry,
Failure["NoRepeatFound", Association["MaxSteps" -> triedsteps]],
Block[{cyclelength = 1},
NestWhile[(cyclelength++; f[#]) &,
f[pointoncycle], # =!= pointoncycle &]; cyclelength]]]]
Xah Talk Show, Advent of Code 2023, Day 14, Wolfram Language
- Xah Talk Show 2024-10-18 Ep588, Advent of Code 2023, Day 14, Wolfram Language
- Xah Talk Show 2024-10-22 Ep590, Advent of Code 2023, Day 14 Part 2, Wolfram Language
- Xah Talk Show 2024-10-26 Ep591, Wolfram Language, Advent of Code 2023, Day 14 Part 2, take 2
- Xah Talk Show 2024-10-27 Ep592, Wolfram Language, Advent of Code 2023, Day 14 Part 2, Take 3