ALTERYX INSPIRE | Join us this May for for a multi-day virtual analytics + data science experience like no other! Register Now
The Alteryx Community will be temporarily unavailable for a time due to scheduled maintenance on Thursday, April 22nd. Please plan accordingly.
alteryx Community

General Discussions

Discuss a wide range of topics! Questions about the Alteryx Platform should be directed to the appropriate Product discussion forum.

Advent of Code 2020 - BaseA Style (Day 22)

14 - Magnetar

17 - Castor

Back to the familiar, if somewhat finicky, realm of substring parsing to play a macro free game for part I.  Like the answer to the question of Life, the Universe and Everything, part II will require Deep Thought.

Spoiler
Part I

Part II requires a generic recursive algorithm that would be fairly easy in a traditional programming language, but is complex(for me at least) to implement in Alteryx.  I have a inkling of how I could accomplish it, involving keeping track of parents and recursion level, but I won't be able to flush it out completely today

Dan

12 - Quasar

Here is my solution for part 1. I took the iterative approach.

Spoiler
workflow

iterative Macro

11 - Bolide

I toyed with the idea of replacing the macro for a combination of tools for the first part, but thought "let's wait for part 2"...

Spoiler

Part II rules are surprisingly simple, provided you read them well (including:
...(the quantity of cards copied is equal to the number on the card they drew to trigger the sub-game)...
and
..(again so that the winner's card is above the other card)...
)
I missed them for a while and spent a long time trying to understand why the macro was not working...

Part II now works with the "sample" dataset. But 100 000 iterations later it has not reached the end for the real input, which makes me think there may be something wrong with the macro, or, it's going to take a looong while to get it there.

The input is converted into:

Deeper levels of recursion are kept as additional rows:

It's awfully inefficient and the workflow is utterly cluttered right now. But not sure how much gain would be obtained from cleaning it properly.
Alternatively, it may be worth rethinking the strategy altogether 😅

Sample data:
Spoiler

12 - Quasar

Took a bit longer than I would have liked to solve part 1 with first an error of mixing numbers and strings so not getting the desired output in the example dataset, and then when running the full dataset being out by one which was just to do with how I dealt the last cards in the pack, but managed to fix it.

It's nice being back into it as I just didn't really have the time last week to participate.

Spoiler

Macro

Chris
Check out my collaboration with fellow ACE Joshua Burkhow at AlterTricks.com
16 - Nebula

Ok, so Part 2 is horrific... but done in BaseA

Spoiler

My approach for Part 2 is to turn the recursion into iteration and use generate rows.
- I encode the 1-50 as ASCII characters (value plus 48 so 1=1, 2=2,...9=9,10=:,...)
- First 2 words of the string are state if player 1 and 2 in the current game (or subgame)
- After word 2, the previous state of the current game is stored as (P1#P2) separated by spaces
- This is checked to see if P1 wins the game, if so the current game is ended and the change made to parent game (or iteration terminates) and current game is removed (replace up to first ` ! `)
- Otherwise, if need to recurse then a new pair of P1 and P2 are added followed by a ` ! ` which marks the end of the current game state
- If someone wins a game as other player runs out, then as above with P1 happens
- If someone wins a round then the state is added to list and word 1 and word 2 are manipulated to be the new value

The horrific formula is below:

``````IF REGEX_CountMatches(C," ") = 0 THEN
"*"
ELSEIF Contains(REGEX_Replace(C, " ! .*\$", ""),GetWord(C,0) + "#" + GetWord(C,1)) THEN
// Player 1 win by termination
iif(Contains(C," ! "),
Substring(GetWord(REGEX_Replace(C, "^[^!]+ ! ", ""), 0), 1)
+ Left(GetWord(REGEX_Replace(C, "^[^!]+ ! ", ""), 0), 1)
+ Left(GetWord(REGEX_Replace(C, "^[^!]+ ! ", ""), 1), 1)
+ " " + Substring(GetWord(REGEX_Replace(C, "^[^!]+ ! ", ""), 1), 1)
+ " " + GetWord(REGEX_Replace(C, "^[^!]+ ! ", ""), 0) + "#" + GetWord(REGEX_Replace(C, "^[^!]+ ! ", ""), 1)
+ Regex_Replace(C, "^[^!]+ ! [^ ]+ [^ ]+ ?", " "),
Substring(GetWord(C,0),1) + Left(GetWord(C,0),1) + Left(GetWord(C,1),1))
ELSEIF CharToInt(GetWord(C,0)) - 48 < Length(GetWord(C,0)) and CharToInt(GetWord(C,1))-48 < Length(GetWord(C,1)) THEN
// Do I need to recurse
Substring(GetWord(C,0),1,CharToInt(GetWord(C,0)) - 48) + " " + Substring(GetWord(C,1),1,CharToInt(GetWord(C,1))-48) + " ! "+ C
ELSEIF CharToInt(GetWord(C,0)) > CharToInt(GetWord(C,1)) AND Length(GetWord(C,1)) = 1 THEN
// Player 1 win
iif(Contains(C," ! "),
Substring(GetWord(REGEX_Replace(C, "^[^!]+ ! ", ""), 0), 1)
+ Left(GetWord(REGEX_Replace(C, "^[^!]+ ! ", ""), 0), 1)
+ Left(GetWord(REGEX_Replace(C, "^[^!]+ ! ", ""), 1), 1)
+ " " + Substring(GetWord(REGEX_Replace(C, "^[^!]+ ! ", ""), 1), 1)
+ " " + GetWord(REGEX_Replace(C, "^[^!]+ ! ", ""), 0) + "#" + GetWord(REGEX_Replace(C, "^[^!]+ ! ", ""), 1)
+ Regex_Replace(C, "^[^!]+ ! [^ ]+ [^ ]+ ?", " "),
Substring(GetWord(C,0),1) + Left(GetWord(C,0),1) + Left(GetWord(C,1),1))
ELSEIF CharToInt(GetWord(C,0)) < CharToInt(GetWord(C,1)) AND Length(GetWord(C,0)) = 1 THEN
// Player 2 win
iif(Contains(C," ! "),
Substring(GetWord(REGEX_Replace(C, "^[^!]+ ! ", ""), 0), 1)
+ " " + Substring(GetWord(REGEX_Replace(C, "^[^!]+ ! ", ""), 1), 1)
+ Left(GetWord(REGEX_Replace(C, "^[^!]+ ! ", ""), 1), 1)
+ Left(GetWord(REGEX_Replace(C, "^[^!]+ ! ", ""), 0), 1)
+ " " + GetWord(REGEX_Replace(C, "^[^!]+ ! ", ""), 0) + "#" + GetWord(REGEX_Replace(C, "^[^!]+ ! ", ""), 1)
+ Regex_Replace(C, "^[^!]+ ! [^ ]+ [^ ]+ ?", " "),
Substring(GetWord(C,1),1) + Left(GetWord(C,1),1) + Left(GetWord(C,0),1))
ELSE
// Play a turn
iif(CharToInt(GetWord(C,0))>CharToInt(GetWord(C,1)),
Substring(GetWord(C,0),1) + Left(GetWord(C,0),1) + Left(GetWord(C,1),1) + " " +Substring(GetWord(C,1),1),
Substring(GetWord(C,0),1) + " " +Substring(GetWord(C,1),1)+ Left(GetWord(C,1),1) + Left(GetWord(C,0),1))

+ " " + GetWord(C,0) + "#" + GetWord(C,1)

// Keep Old
+ Regex_Replace(C,"^[^ ]+ [^ ]+ ?", " ")
ENDIF``````

14 - Magnetar

Day22!

I cleared Part 1 only.

Spoiler

Iterative Macro

AS anonymous user #1105310