↤ go back, or return home

jack's advent of code 2022 ramblings

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

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 src/data/aoc/2022/data

a python solution, with ample usage of dicts to map input to expected output

verbose but it means each part is nice and clean

[briannamcdonald/advent-of-code-2022] python src/data/aoc/2022/data

the brute force way, but decently compact

[krbarter/Advent-Of-Code-2022] python src/data/aoc/2022/data

the brute force way, but not as decently compact :D

[nint8835/AdventOfCode2022] f# & python src/data/aoc/2022/data

the functional, and, mathematical way

nice

[hamzahap/AdventOfCode2022] sheets src/data/aoc/2022/data

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 hamzah

edit: 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 :)

[TheCrypticCanadian/advent-of-code-2022] python src/data/aoc/2022/data

less 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

[STollenaar/AdventOfCode2022] golang src/data/aoc/2022/data

clean little go maps and structs again

setup means problem is smol :)

[zcvaters/adventofcode2022] swift src/data/aoc/2022/data

swift enums, interesting

them having methods looks cursed i must admit

[chadmroberts88/advent-of-code-2022] typescript src/data/aoc/2022/data

brute force, but still compact!

[devthedevel/advent_of_code] typescript src/data/aoc/2022/data

wow 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

[mathieuboudreau/advent] python notebook src/data/aoc/2022/data

simple, clean, reads nice

nice

[ericthomasca/adventofcode2022] rust src/data/aoc/2022/data

bruce force, but :crab:

[Keenan-Nicholson/AdventOfCode] typescript src/data/aoc/2022/data

brute force

yeah

[CameronSquires/AdventOfCode2022] python src/data/aoc/2022/data

brute force, an if soup

[RyanBrushett/adventofcode2022] ruby src/data/aoc/2022/data

brute force, but ruby, so its nice <3

pretty compact

[joel1842/advent-of-code-2022] python src/data/aoc/2022/data

brute force, but match statement usage, so nice

[ecumene/advent_of_code] python notebook src/data/aoc/2022/data

a 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

[canetoads.ca] javascript src/data/aoc/2022/data

just mapping the input lines to the resulting score, so technically brute force, but probably the most clean brute force here!

[apreynolds1989/AdventOfCode2022] typescript src/data/aoc/2022/data

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