Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Year 2021, Day 10

livebook/2021/day10.livemd

Year 2021, Day 10

Section

alias AdventOfCode.Year2021.Day10

Input

input =
  Day10.test_input()
  |> String.split("\n", trim: true)
  |> Enum.map(&String.graphemes/1)
input = Enum.map(input, &Day10.check_syntax/1)

Part 1

Get just the lines with error in them

part1 = Enum.filter(input, &(elem(&1, 0) == :error))

Get just to first character which caused error

part1 = Enum.map(part1, &elem(&1, 1))

Score the result according to the given rules

Enum.reduce(part1, 0, fn
  ")", acc -> acc + 3
  "]", acc -> acc + 57
  "}", acc -> acc + 1197
  ">", acc -> acc + 25137
end)

Part 2

Get just the incomplete lines from the input

part2 = Enum.filter(input, &(elem(&1, 0) == :incomplete))

Get just the completion strings

part2 = Enum.map(part2, &elem(&1, 1))

Score according to the rules

part2 = Enum.map(part2, &Day10.score_completion/1)

Get the median of the scores

Statistics.median(part2)