puzzle.jl 731 B

1234567891011121314151617181920212223242526272829303132
  1. using MLStyle
  2. using BenchmarkTools
  3. inputfile = joinpath(@__DIR__, "input.txt")
  4. input =
  5. readlines(inputfile) .|>
  6. x ->
  7. replace(x, "A" => 1, "B" => 2, "C" => 3, "X" => 1, "Y" => 2, "Z" => 3) |>
  8. split |>
  9. x -> parse.(Int, x) |> x -> tuple(x...)
  10. first(round) = @match round begin
  11. (1, 2) || (2, 3) || (3, 1) => round[2] + 6
  12. (2, 1) || (3, 2) || (1, 3) => round[2]
  13. _ => round[2] + 3
  14. end
  15. second(round) = @match round begin
  16. (x, 1) => x == 1 ? (1, 3) : x == 2 ? (2, 1) : (3, 2)
  17. (x, 2) => (x, x)
  18. (x, 3) => x == 1 ? (1, 2) : x == 2 ? (2, 3) : (3, 1)
  19. end
  20. solve(input) = begin
  21. input .|> first |> sum, input .|> second .|> first |> sum
  22. end
  23. print(solve(input))
  24. @btime solve(input)