main.jl 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using DataStructures
  2. using BenchmarkTools
  3. selfdir = joinpath(splitdir(@__FILE__)[1], "input")
  4. data =
  5. read(selfdir, String) |>
  6. x -> split(x, "\n\n") .|>
  7. x -> split(x)
  8. #--------part1
  9. function anyone_yes()
  10. sum(map(x->length(x), map(unique, (map(x-> reduce(*, x), data)))))
  11. end
  12. function anyone_yes_sets()
  13. sum(data .|> x -> union(x...) |> length)
  14. end
  15. #--------part2
  16. function all_yes()
  17. data .|>
  18. (
  19. x -> (reduce(*, x), length(x)) |>
  20. x -> (counter(x[1]), x[2]) |>
  21. x -> (values(x[1]), x[2]) |>
  22. x -> count(==(x[2]), x[1])
  23. ) |>
  24. sum
  25. end
  26. function all_yes_sets()
  27. sum(data .|> x -> intersect(x...) |> length)
  28. end
  29. #--------evaluation
  30. println("first impl")
  31. anyone_yes_count = @btime anyone_yes()
  32. all_yes_count = @btime all_yes()
  33. println("check sets union")
  34. @btime anyone_yes_sets()
  35. println("check sets intersect")
  36. @btime all_yes_sets()
  37. println(anyone_yes_count)
  38. println(all_yes_count)