Xah Talk Show 2024-12-05 Ep602, Wolfram Language, Advent of Code 2024, Day 5

By Xah Lee. Date: .
vidthumb mSU2opCVkOY
vidthumb mSU2opCVkOY

InCorrect Answer, Part 1

(* this is incorrect code.
the code logic for building xorderSeq is incorrect in critical ways.
 *)

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", "|"} ];

(* {{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 = ToExpression @ Map[ Function[{x}, StringSplit[ x, "," ]], StringSplit[ xlines, "\n" ] ];

(* description of the problem.

given a list of pairs of numbers, each pair indicates order, e.g. (aa,bb) means aa comes before bb.

and given a list of lines, each line contains a sequence of numbers.

for each line, check if all its numbers are in order.

for each ordered line,
take the number at the middle, sum it up.
this sum is the answer.

*)

(*
given 2 numbers,
first, check if there is a pair of these numbers in the dict.
order of the pair does not matter.
if the order is the same, then return true.
if the order is not same, then return false.

but what if, one of the number, is not in the dict.

4 cases:

The first number, not in keys,
the first number, not in values.
The second number, not in keys,
the second number, not in values.

but what if, both numbers, are not in the dict, not in key nor value.
this means, the advent of code fucked up.

*)

(*
ok, we need to rewrite the given list of pairs, into
a sequence of ordered numbers.
that way, given any 2 element, we can tell, if they are in order,
by comparing their indexes.

how to do this?

go thru the dict pairs.
given a pair, aa bb,

If both not in the list, add both, in the order aa bb, in beginning or end.
If both are in the list, check if their list order is the same. If not, that means there is a conflict.
If aa is in the list but not bb. Add bb to the list, after aa.
If aa is not in the list but bb is. Add aa to the list, before bb.
*)

(*
OrderedQ[ {75,47,61,53,29},
Function[{x,y}, return True or False]
]
*)

(* HHHH--------------------------------------------------- *)

(* build a list of order sequence, such that items are from all the pairs, and all elements in proper order *)

xorderSeq = {};

(*
example for testing the building xorderSeq

assume the order of elements is integer order.
suppose we got

xpairs =
{
{2,9},
{1,8},
{1,2},
{2,8},
{8,9},
{1,9}
}

these are all possible pairs of 1 2 8 9.
the correct result should be {1,2,8,9}.

by our algo, we get, in steps

{2,9} → {2,9}
{1,8} → {2,9,1,8}
{1,2} → {1,9,2,8}
{2,8} → {1,9,2,8} (this step is start of problem)
{8,9} → {1,8,2,9}
{1,9} → {1,8,2,9} (wrong)

correct is {1,2,8,9}

 *)

MapThread[
Function[{aa,bb},

If[aa === bb, Print ["problem. got a pair is equal."] , Null ];

With[ {
aaExist = MemberQ[ xorderSeq, aa ],
bbExist = MemberQ[ xorderSeq, bb ]
},
Which[
(aaExist && bbExist),
With[ {
 aaIndex = FirstPosition[ xorderSeq, aa ][[1]],
 bbIndex = FirstPosition[ xorderSeq, bb ][[1]]},

Print [ "both aa: ", aa, " bb ", bb,  "  already exist in order seq.", xorderSeq] ;

If[ aaIndex <= bbIndex , Null,
Print [ "got a pair that exist but conflict in existing order: ", {aa, bb, xorderSeq}] ;
{xorderSeq[[aaIndex]], xorderSeq[[bbIndex]] } = {bb,aa} ]
 ],
(! aaExist && ! bbExist), AppendTo[ xorderSeq, aa ]; AppendTo[ xorderSeq, bb ],
(aaExist && ! bbExist), AppendTo[ xorderSeq, bb ],
(! aaExist && bbExist), PrependTo[ xorderSeq, aa ]
]
]
] ,
Transpose @ xpairs];

(* xorderSeq *)
(* {97, 75, 47, 61, 53, 29, 13} *)

(* DeleteDuplicates @ Flatten[ xpairs ] *)

xposindex = PositionIndex[ xorderSeq ];

xorderPred = Function[{aa,bb}, Lookup[ xposindex, aa][[1]] < Lookup[ xposindex, bb][[1]]];

xorderPred = Function[{aa,bb},
If[
KeyExistsQ[ xposindex, aa ] && KeyExistsQ[ xposindex, bb ] ,
(Lookup[ xposindex, aa][[1]] < Lookup[ xposindex, bb][[1]]),
False
]
];

(*
Map[
Function[{xline},
OrderedQ[ xline, Function[{aa,bb}, Lookup[ xposindex, aa][[1]] <= Lookup[ xposindex, bb][[1]] ] ] ],
xlines]
*)
(* {True, True, True, False, False, False} *)

(* answer for toy input is 143 *)
(* answer for personal input is 10100 *)

xanswer =
Total @
Map[

Function[{xx},

If[ EvenQ @ Length @ xx, Print @  "problem, line length even", Null];

If[ OrderedQ[ xx, xorderPred ] ,
xx[[Ceiling[ (Length @ xx) / 2 ]]] ,
0]
 ],
xlines];

(* status.
answer is correct for toy input, but not personal input.
after close to 4 hours coding.
will finish later.
 *)

xposindex

xanswer
xtodo

Mini Problem, Generat a Ordered List from List of Ordered Pairs

xpairs =
{
{2,9},
{1,8},
{1,2},
{2,8},
{8,9},
{1,9}
}

xorderedlist = DeleteDuplicates @ Flatten @ xpairs

Map[
Function[{x}, expr]
, xpairs]

{2,9} → {2,9}
{1,8} → {2,9,1,8}
{1,2} → {1,9,2,8}
{2,8} → {1,9,2,8} (this step is start of problem)
{8,9} → {1,8,2,9}
{1,9} → {1,8,2,9} (wrong)

correct is {1,2,8,9}

Correct Answer, Part 1

xtodo
(* this is incorrect code.
the code logic for building xorderSeq is incorrect in critical ways.
 *)

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" ] ];

(* build a list of order sequence, such that items are from all the pairs, and all elements in proper order *)

xorderSeq = {};

MapThread[
Function[{aa,bb},

If[aa === bb, Print ["problem. got a pair is equal."] , Null ];

With[ {
aaExist = MemberQ[ xorderSeq, aa ],
bbExist = MemberQ[ xorderSeq, bb ]
},
Which[
(aaExist && bbExist),
With[ {
 aaIndex = FirstPosition[ xorderSeq, aa ][[1]],
 bbIndex = FirstPosition[ xorderSeq, bb ][[1]]},

Print [ "both aa: ", aa, " bb ", bb,  "  already exist in order seq.", xorderSeq] ;

If[ aaIndex <= bbIndex , Null,
Print [ "got a pair that exist but conflict in existing order: ", {aa, bb, xorderSeq}] ;
{xorderSeq[[aaIndex]], xorderSeq[[bbIndex]] } = {bb,aa} ]
 ],
(! aaExist && ! bbExist), AppendTo[ xorderSeq, aa ]; AppendTo[ xorderSeq, bb ],
(aaExist && ! bbExist), AppendTo[ xorderSeq, bb ],
(! aaExist && bbExist), PrependTo[ xorderSeq, aa ]
]
]
] ,
Transpose @ xpairs];

(* xorderSeq *)
(* {97, 75, 47, 61, 53, 29, 13} *)

(* DeleteDuplicates @ Flatten[ xpairs ] *)

xposindex = PositionIndex[ xorderSeq ];

xorderPred = Function[{aa,bb}, Lookup[ xposindex, aa][[1]] < Lookup[ xposindex, bb][[1]]];

xorderPred = Function[{aa,bb},
If[
KeyExistsQ[ xposindex, aa ] && KeyExistsQ[ xposindex, bb ] ,
(Lookup[ xposindex, aa][[1]] < Lookup[ xposindex, bb][[1]]),
False
]
];

(*
Map[
Function[{xline},
OrderedQ[ xline, Function[{aa,bb}, Lookup[ xposindex, aa][[1]] <= Lookup[ xposindex, bb][[1]] ] ] ],
xlines]
*)
(* {True, True, True, False, False, False} *)

(* answer for toy input is 143 *)
(* answer for personal input is 10100 *)

xanswer =
Total @
Map[

Function[{xx},

If[ EvenQ @ Length @ xx, Print @  "problem, line length even", Null];

If[ OrderedQ[ xx, xorderPred ] ,
xx[[Ceiling[ (Length @ xx) / 2 ]]] ,
0]
 ],
xlines];

(* status.
answer is correct for toy input, but not personal input.
after close to 4 hours coding.
will finish later.
 *)

xposindex

xanswer

part 2