Xah Talk Show 2022-12-13 Advent of Code Day 2, in WolframLang, emacs lisp, JavaScript
Problem Description
problem: given a string of lines, like this:
A Y B X C Z
- write a function f. f returns a integer.
- where,
- A means 1
- B means 2
- C means 3
- X means 1
- Y means 2
- Z means 3
and we have the following relation, where the right side win:
- A ≺ B (B win)
- B ≺ C
- C ≺ A
- X ≺ Y
- Y ≺ Z
- Z ≺ A
- lose = 0
- draw = 3
- win = 6
- for example,
- f(A,Y) → 1+2+6 = 9
- basically, for each line, the function compute 3 numbers, and sum them.
- algorithm:
- convert the letters to numbers. a to 1, b to 2, c to 3, and same for x y z. add them.
- then, compare the modular arithemitc module to 2, if the diff is 1, then add 6, if d diff is 2, then add 3, if 0, add 0.
third column, is the result of y - x 1 1 draw , 0 1 2 win, 1 1 3 lose, 2 2 1 lose, -1 2 2 draw, 0 2 3 win, 1 3 1 win, -2 3 2 lose, -1 3 3 draw, 0 if outcome -1, → 0 if outcome 0, → 3 if outcome 1, → 6 Table[ { {x, y}, {Mod[x, 2], Mod[y, 2]}, {Mod[x, 2] - Mod[y, 2]} }, {x, 0,2}, {y, 0,2} ] 0 % 2 = 0 1 % 2 = 1 2 % 2 = 0 0 % 2 = 0 1 % 2 = 1 2 % 2 = 0
Clear[xinput, fComputeLineScore, x6, xanswer ] fComputeLineScore[{x_, y_}] := y + 1 + Mod[ ( Mod[y + 3-x, 3] ) + 1, 3] 3 xinput = " A Y B X C Z"; x6 = Map[ fComputeLineScore, ToExpression[ Partition[ StringSplit[ StringReplace[ xinput, { "A" -> "0", "B" -> "1", "C" -> "2", "X" -> "0", "Y" -> "1", "Z" -> "2" } ] ], 2 ] ] ] xanswer = Total @ x6
Table[ {{x, y }, {Mod[ x, 3 ], Mod[ y, 3 ]}}, {x, 0, 2}, {y, 0, 2} ] Table[ {x, y, ( Mod[y+3-x, 3] ) }, {x, 0, 2}, {y, 0, 2} ] Table[ {x, y, Mod[ ( Mod[y + 3-x, 3] ) + 1, 3] }, {x, 0, 2}, {y, 0, 2} ]//TableForm