day 2
second day, a problem with ever slightly more meat, but still light
my solution could have been more compact, but i already what i wanted my solution to be in my head as soon as i saw the problem, so i went with the flow
click to view my solution
given the sample strategy guide:
A Y
B X
C Z
assuming input
is the above as one big string,
input =
input
|> String.trim()
|> String.split("\n")
|> Enum.map(&String.split(&1, " "))
the usual input trim / split, but this time splitting each line itself by an empty string
[
["A", "Y"],
["B", "X"],
["C", "Z"]
]
leaving us with a list of lists yet again
defmodule RockPaperScissors do
def score(them, you) do
choice_score =
case you do
:rock -> 1
:paper -> 2
:scissors -> 3
end
game_score =
case {you, them} do
{you, you} -> 3
{:rock, :paper} -> 0
{:rock, :scissors} -> 6
{:paper, :scissors} -> 0
{:paper, :rock} -> 6
{:scissors, :rock} -> 0
{:scissors, :paper} -> 6
end
choice_score + game_score
end
def pick_then_score(them, wanted_result) do
case {them, wanted_result} do
{x, :draw} -> score(them, x)
{:rock, :win} -> score(them, :paper)
{:rock, :throw} -> score(them, :scissors)
{:paper, :win} -> score(them, :scissors)
{:paper, :throw} -> score(them, :rock)
{:scissors, :win} -> score(them, :rock)
{:scissors, :throw} -> score(them, :paper)
end
end
end
creating a module for the first time this year, to
- calculate score, using
score/2
, which takes in a ‘game’ (choice from us, and choice from enemy), and returns the score from that game we receive
score/2
choice_score =
case you do
:rock -> 1
:paper -> 2
:scissors -> 3
end
using pattern matching, get the score we get from simply picking an option
game_score =
case {you, them} do
{you, you} -> 3
{:rock, :paper} -> 0
{:rock, :scissors} -> 6
{:paper, :scissors} -> 0
{:paper, :rock} -> 6
{:scissors, :rock} -> 0
{:scissors, :paper} -> 6
end
using pattern matching again, to figure out what we actually from winning / losing, using a sneaky pattern matching trick for handling the draw case
choice_score + game_score
then, returning a value
big stuff
pick_then_score/2
case {them, wanted_result} do
{x, :draw} -> score(them, x)
{:rock, :win} -> score(them, :paper)
{:rock, :throw} -> score(them, :scissors)
{:paper, :win} -> score(them, :scissors)
{:paper, :throw} -> score(them, :rock)
{:scissors, :win} -> score(them, :rock)
{:scissors, :throw} -> score(them, :paper)
end
for this util, we take in what we know they picked, and the result we wish the game to end up in, and do yet another pattern match, with some more sneaky logic for the draw case, using score/2
to calculate the score of our very legit game
part 1
assumed_guide = %{
"A" => :rock,
"B" => :paper,
"C" => :scissors,
"X" => :rock,
"Y" => :paper,
"Z" => :scissors
}
for part 1, i assume i have the ‘assumed guide’ from the elf, which maps the input from string -> atom, so the rest of my program can use atoms instead of strings
input
|> Enum.map(fn [them, you] ->
RockPaperScissors.score(
assumed_guide[them],
assumed_guide[you]
)
end)
|> Enum.sum()
# -> 15
map over the already cleaned up input, pluck out the left and right values into variables, and stuff them into score/2
sum that, and boom, answer
part 2
actual_guide = %{
"A" => :rock,
"B" => :paper,
"C" => :scissors,
"X" => :throw,
"Y" => :draw,
"Z" => :win
}
this is a similar map to the one above, but the actual guide from the elf, no silly stuff this time
input
|> Enum.map(fn [them, you] ->
RockPaperScissors.pick_then_score(
actual_guide[them],
actual_guide[you]
)
end)
|> Enum.sum()
# -> 12
same as above, yet again, but we use pick_then_score
, because we are legit rock paper scissor gamers
the full solution can be found [here]
the [MUN Computer Science Society] server / discord continues to be busy, as i write this at 11:00pm on the 2nd:
- 39 people have solved day 1
- 30 have solved day 2
it seems we had another large crowd of late folk up doing the problem again, even those who did the problem later but still pretty early in the morning have been pushed off of the top twenty!
others
[Mudkip/AdventOfCode] python[briannamcdonald/advent-of-code-2022] pythona python solution, with ample usage of dicts to map input to expected output
verbose but it means each part is nice and clean
[krbarter/Advent-Of-Code-2022] pythonthe brute force way, but decently compact
[nint8835/AdventOfCode2022] f# & pythonthe brute force way, but not as decently compact :D
[hamzahap/AdventOfCode2022] sheetsthe functional, and, mathematical way
nice
[TheCrypticCanadian/advent-of-code-2022] python
i wish github could preview excel files in a table because i am simply not spending the time required to pull this down and view
however,keep it up hamzahedit: turns out it is not being done in excel, but actually being done in sheets and being exported to excel such that there can exist files in the repo
fixed :)
[STollenaar/AdventOfCode2022] golangless of a blob this time but still two files, gump why?
the brute force way, but just mapping the lines to the output, which for this problem isn’t a bad strat
[zcvaters/adventofcode2022] swiftclean little go maps and structs again
setup means problem is smol :)
[chadmroberts88/advent-of-code-2022] typescriptswift enums, interesting
them having methods looks cursed i must admit
[devthedevel/advent_of_code] typescriptbrute force, but still compact!
[mathieuboudreau/advent] python notebookwow pulling out type defs that are a union of strings
but usage of
//@ts-ignore
yet, i still cannot question the logic of the wise clouds
for they are so high
[ericthomasca/adventofcode2022] rustsimple, clean, reads nice
nice
[Keenan-Nicholson/AdventOfCode] typescriptbruce force, but :crab:
[CameronSquires/AdventOfCode2022] pythonbrute force
yeah
[RyanBrushett/adventofcode2022] rubybrute force, an
if
soup
[joel1842/advent-of-code-2022] pythonbrute force, but ruby, so its nice <3
pretty compact
[ecumene/advent_of_code] python notebookbrute force, but match statement usage, so nice
[canetoads.ca] javascripta little bit of ai generated art of a man tending to a garden integrated into the notebook, we love to see it
brute force, but spread over many compact cells
even a summary at the end, very introspective
[apreynolds1989/AdventOfCode2022] typescriptjust mapping the input lines to the resulting score, so technically brute force, but probably the most clean brute force here!
quite switch-casey-brute-forcey, but a solution nonetheless!
if you want to have your repo added for me to make a note of / talk about here (or have you repo removed), reach out:
email -> me@jackharrhy.com
discord -> <i>jack arthur null</i>#7539