DEV Community

Caleb Weeks
Caleb Weeks

Posted on • Updated on • Originally published at sethcalebweeks.com

Advent of Code Day 2

Links

Highlights

  • It feels really naive to simply hard code the total score for each combination. The mental load of thinking through who would win and encoding that in a smart way would have taken just as long if not longer than just writing down the correct score for each combination.
  • This is where pattern matching works really well. Just match each combination with the correspoding score.
defmodule Day02 do
  use AOC

  def part1 do
    input(2)
    ~> String.split("\n")
    ~> Enum.map(fn round ->
      case round ~> String.split(" ") do
        ["A", "X"] -> 4
        ["B", "X"] -> 1
        ["C", "X"] -> 7
        ["A", "Y"] -> 8
        ["B", "Y"] -> 5
        ["C", "Y"] -> 2
        ["A", "Z"] -> 3
        ["B", "Z"] -> 9
        ["C", "Z"] -> 6
      end
    end)
    ~> Enum.sum()
  end

  def part2 do
    input(2)
    ~> String.split("\n")
    ~> Enum.map(fn round ->
      case round ~> String.split(" ") do
        ["A", "X"] -> 3
        ["B", "X"] -> 1
        ["C", "X"] -> 2
        ["A", "Y"] -> 4
        ["B", "Y"] -> 5
        ["C", "Y"] -> 6
        ["A", "Z"] -> 8
        ["B", "Z"] -> 9
        ["C", "Z"] -> 7
      end
    end)
    ~> Enum.sum()
  end

end
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
sethcalebweeks profile image
Caleb Weeks

I probably thought I could do something clever with assigning values to the letters somehow, so that's why I split out the line into the two letters. Turns out I may as well just be matching on the original line instead of an array of the two letters.