Discussion thread for day 2 of the Advent of Code - https://adventofcode.com/2022/day/2
If you have not read the children's book "The Legend of Rock Paper Scissors", I HIGHLY recommend it as a complement to today's challenge.
"Are you not entertained?!?"
I struggled most with understanding the rules. Really need to read twice build once instead of build 5 times read once.
When in doubt mapping files solve everything
I made some manual assignments instead of writing a long formula. The tradeoff paid off in speed given there were a fixed number of outcomes. Not my most dynamic solution...
My Solution for AOC Day-2
Cheers and happy analyzing!
@clmc9601 Very nicely done 😁. I guess it shows we should do some thinking before we start dragging the tools onto Canvas to solve the problem 😂.
Today ... the missing sleep really came into play ...
I also played tool golf and actually found a pretty smart way to do the score formula without nesting 6 IFs:
I'm embarrassed with how long I took, so here's my refactored version
Originally parsed out the letters and then built complicated IF statements which with my 5am brain meant I had typos and just couldn't see it, so ended up getting some paper out and writing the logic down in a matrix.
I even messed up this!
My solution. There are no IF statements. But my workflow has many hand-made tables.This is similar to @clmc9601 's approach, but not as elegant as hers.
If-else Era it is! 😃
Day 2 done ! ✅
If you need to work on your conditional statements game, here is the training !
I went for the nested IF statements ! Because where would be the fun otherwise ?
Mathematical formula for part 1 :
Mod(CharToInt([Player]) - CharToInt([Opponent]) - 19, 3) * 3 + CharToInt([Player]) - 87
Mathematical formula for part 2 :
(CharToInt([Player]) - 88) * 3 + Mod(ABS(-CharToInt([Player])-CharToInt([Opponent]) + 151), 3) + 1
Day 2 done! This one took way longer than I care to admit... Which is what happens when you don't read the question properly and miss the part about draws, so assign your move based upon your opponent!
I organized the pattern and I found that it is simple. So I remade the workflow as using formula tool.
Formula
ToNumber( Switch(Right([Input],1),"0", "X","1", "Y","2", "Z","3" )) + ToNumber( IF [Input] IN ("C X","A Y","B Z") THEN "6" ELSEIF [Input] IN ("A X","B Y","C Z") THEN "3" ELSE "0" ENDIF )​
Formula for Part 2
ToNumber( Switch(Right([Input],1),"0", "X","0", "Y","3", "Z","6" )) + ToNumber( IF [Input] IN ("A X","C Y","B Z") THEN "3" ELSEIF [Input] IN ("C X","B Y","A Z") THEN "2" ELSE "1" ENDIF )​
A bit copy & paste on the formula, otherwise so far so good.
Nothing clever about today's solution
just write every option...
If statements not too untidy. @grossal does this count as 2 tools for tool golf?
Did the giant If Else If
When I solved way past when I should have been sleeping, I created lookup tables for the different values and scores. Here is my tool golf solution. 3 tools:
Such a fun way to get the brain going in the morning!
If Statement for the win! There are more clever ways to do this but this got the job done relatively fast for me:
Fairly simple once I'd actually read the second question.
Turns out that's quite important.
So, I created a mapping table as follows:
this allowed me to simply join the first and second columns. Then for the first answer I simply built an IF to calculate the points then sum them:(if [Outcome]= "Win" then 6 elseif [Outcome]="Draw" then 3 else 0 endif)+ (if [2]="Y" then 2 elseif [2]="X" then 1 else 3 endif).The second part was a couple of IFs to determine the required play given the Required Outcome, then the associated points.Noice.
Reply number 1: This is the raw solution - refactor to follow.
this is part of the practice of Red-Green-Refactor (TDD)
So here's the first post - my messy solution (Red & Green) - refactor to follow.
Not super happy with my solve on this one - it feels clunky. I might give this some thought and come back to it when I'm not trying to move as fast!
Really fun looking at all the responses. I think @grossal and @DavidP trying to go for tool golf is entertaining too!
Here's mine:
@DavidP it doesn't count. As discussed in the WhatsApp group earlier: For tool golf, both answers have to be provided in a single cell output. Therefore you'd need two Summarize tools :-)
Lots of find replacing going on here, but gets the job done haha. I opted to build out the selection / results and points mapping in tables rather then messing with long if statements. I could have reduced the tool count if I combined the points mapping into the results mapping, but would have taken more manual effort than I cared for.
@AkimasaKajitani I love the simplicity of the formula you came up with! well done
I know there was a prettier way to do this, but this was my Day 2 solution:
My Day 2 solution. I wonder if there is a more elegant way than using the fixed logic table for Scissors, Paper, Stone...
Not elegant, but done. Applies to much in life.
Solved a second time using Python #SnakingMyWayThroughChallenges