Xah Talk Show 2024-12-17 Ep603, Wolfram Language, Advent of Code 2024, Day 5. Take 2

(* how to get position of element *) Position[ {1,2,3,4,5}, 4|5, {1} ] (* {{4}, {5}} *) (* in our case, 4 or 5, but this way cannot tell which occured first *) Position[ {1,2,3,5,4}, 4|5, {1} ] (* {{4}, {5}} *) (* HHHH--------------------------------------------------- *) (* showing how order works *) Order[ 3,3 ] (* 0 *) Order[ 3,4 ] (* 1 *) (* HHHH--------------------------------------------------- *) (* syntax shortcut for apply *) (ff @@ xx) === Apply[ ff , xx ] (* True *)
(* HHHH--------------------------------------------------- *) (* change an element *) xx = {1,2,3}; xx[[1]] = bb; xx (* cannot be done if list is not a var *) {1,2,3}[[1]] = 99 (* better way, functional way, to change element *) ReplacePart[ {1,2,3}, 1 -> 99 ] (* {99, 2, 3} *)
(* in a function, you cannot change the parameter value *) (* this is error *) Function[{x}, x = 3][4] (* if you really want to change param value, create local var *) Function[{x}, Module[{y = 3}, y] ][4]
xinput = "47|53 97|13 97|61 97|47 75|29 61|13 75|53 29|13 97|29 53|29 61|53 97|53 61|29 47|13 75|47 97|75 47|61 75|61 47|29 75|13 53|13 75,47,61,53,29 97,61,53,29,13 75,29,13 75,97,47,61,53 61,13,29 97,13,75,29,47"; (* xinput = ReadString @ "c:/Users/xah/web/xahlee_info/comp/advent/advent_of_code_2024_5_input.txt"; *) (* xinput = Import @ "http://xahlee.info/comp/advent/advent_of_code_2024_5_input.txt"; *) {xpairs, xlines} = StringSplit[ xinput, "\n\n" ]; xpairs = ToExpression @ Function[{x}, Partition[ x, 2 ]] @ StringSplit[ xpairs, {"\n", "|"} ]; xlines = ToExpression @ Map[ Function[{x}, StringSplit[ x, "," ]], StringSplit[ xlines, "\n" ] ]; (* xpairs *) (* {{47, 53}, {97, 13}, {97, 61}, {97, 47}, {75, 29}, {61, 13}, {75, 53}, {29, 13}, {97, 29}, {53, 29}, {61, 53}, {97, 53}, {61, 29}, {47, 13}, {75, 47}, {97, 75}, {47, 61}, {75, 61}, {47, 29}, {75, 13}, {53, 13}} *) (* xlines *) (* {{75, 47, 61, 53, 29}, {97, 61, 53, 29, 13}, {75, 29, 13}, {75, 97, 47, 61, 53}, {61, 13, 29}, {97, 13, 75, 29, 47}} *) (* build a list of order sequence, such that items are from all the pairs, and all elements in proper order *) xuniqNumbers = DeleteDuplicates @ Flatten @ xpairs; (* {47, 53, 97, 13, 61, 75, 29} *) (* go thru each pair, if they are in the wrong order in xuniqNumbers, swap their position. do this again, until, none are in wrong order. *) (* HHHH--------------------------------------------------- *) (* example, of, swaping 2 items in a list, according to a given pair. *) xuniqNumbers = {47, 53, 97, 13, 61, 75, 29}; xpairs = {{47, 53}, {97, 13}, {97, 61}, {97, 47}, {75, 29}, {61, 13}, {75, 53}, {29, 13}, {97, 29}, {53, 29}, {61, 53}, {97, 53}, {61, 29}, {47, 13}, {75, 47}, {97, 75}, {47, 61}, {75, 61}, {47, 29}, {75, 13}, {53, 13}}; fReorderPair = Function[{xlist, apair}, Module[ {aa, bb, posA,posB}, {aa, bb} = apair; Print["aa and bb are:", {aa , bb}, "list is ", xlist]; posA = First @ FirstPosition[ xlist, aa, Null ,{1} ]; posB = First @ FirstPosition[ xlist, bb, Null ,{1} ]; If[ (Order[posA, posB] ) === -1, (* Print["aa and bb are:", {aa , bb}, "positions are", {posA, posB}]; *) ReplacePart[ xlist, {posA -> bb, posB -> aa} ] , xlist] ] ]; fReorderPair[xuniqNumbers, {75,53}] fReorderPair[xuniqNumbers, {47,53}] fReorderPair[xuniqNumbers, {97, 61}] (* HHHH--------------------------------------------------- *) (* the correct ordered sequence *) xorderSeq = Fold[ fReorderPair , xuniqNumbers , xpairs ] ; (* {97, 75, 47, 61, 53, 29, 13} *)