main.jl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. selfdir = joinpath(splitdir(@__FILE__)[1], "input")
  2. required_fields = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]
  3. optional_fields = ["cid"]
  4. #--------part1
  5. function valid_documents()
  6. valid = 0
  7. Document = Dict()
  8. for (idx, line) in enumerate(eachline(selfdir))
  9. if (line == "")
  10. valid += required_fields ⊆ keys(Document)
  11. Document = Dict()
  12. else
  13. line != ""
  14. [foreach(x -> (Document[x[1]] = x[2]), [split(i, ':')]) for i in split(line)]
  15. end
  16. end
  17. if ~isempty(Document)
  18. valid += required_fields ⊆ keys(Document)
  19. end
  20. return valid
  21. end
  22. function valid_documents_pipe()
  23. selfdir = joinpath(splitdir(@__FILE__)[1], "input")
  24. required_fields = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]
  25. optional_fields = ["cid"]
  26. a =
  27. read(selfdir, String) |>
  28. x -> split(x, "\n\n") .|>
  29. x -> split(x) .|>
  30. x -> split(x, ':')
  31. b =
  32. a .|>
  33. Dict |>
  34. x -> mapreduce(y -> required_fields ⊆ keys(y), +, x)
  35. return b
  36. end
  37. #---------part2
  38. function check_fields(Document)
  39. checks = 0
  40. if required_fields ⊆ keys(Document)
  41. checks += 1920 <= parse(Int, Document["byr"]) <= 2002
  42. checks += 2010 <= parse(Int, Document["iyr"]) <= 2020
  43. checks += 2020 <= parse(Int, Document["eyr"]) <= 2030
  44. checks += if endswith(Document["hgt"], "cm")
  45. 150 <= parse(Int, Document["hgt"][1:(end - 2)]) <= 193
  46. elseif endswith(Document["hgt"], "in")
  47. 59 <= parse(Int, Document["hgt"][1:(end - 2)]) <= 76
  48. else
  49. false
  50. end
  51. checks += ~isnothing(match(r"#[0-9a-f]{6}"i, Document["hcl"]))
  52. checks += Document["ecl"] ∈ ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]
  53. checks += ~isnothing((match(r"^[0-9]{9}$", Document["pid"])))
  54. end
  55. return checks == 7
  56. end
  57. function valid_documents2()
  58. valid = 0
  59. Document = Dict()
  60. for (idx, line) in enumerate(eachline(selfdir))
  61. if (line != "")
  62. [foreach(x -> (Document[x[1]] = x[2]), [split(i, ':')]) for i in split(line)]
  63. else
  64. valid += check_fields(Document)
  65. Document = Dict()
  66. end
  67. end
  68. if ~isempty(Document)
  69. valid += check_fields(Document)
  70. end
  71. return valid
  72. end
  73. function valid_documents_pipe2()
  74. selfdir = selfdir = joinpath(splitdir(@__FILE__)[1], "input")
  75. required_fields = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]
  76. optional_fields = ["cid"]
  77. a =
  78. read(selfdir, String) |>
  79. x -> split(x, "\n\n") .|>
  80. x -> split(x) .|>
  81. x -> split(x, ':')
  82. b =
  83. a .|>
  84. Dict |>
  85. x -> mapreduce(y -> check_fields(y), +, x)
  86. return b
  87. end
  88. #--------evaluation
  89. valid2 = @btime valid_documents()
  90. valid1 = @btime valid_documents_pipe()
  91. valid3 = @btime valid_documents2()
  92. valid4 = @btime valid_documents_pipe2()