My code for Advent of Code 2020 in Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
7.2 KiB

2 months ago
2 months ago
2 months ago
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "regexp"
  7. "strconv"
  8. "strings"
  9. )
  10. // You arrive at the airport only to realize that you grabbed your North Pole Credentials instead of your passport. While these documents are extremely similar, North Pole Credentials aren't issued by a country and therefore aren't actually valid documentation for travel in most of the world.
  11. // It seems like you're not the only one having problems, though; a very long line has formed for the automatic passport scanners, and the delay could upset your travel itinerary.
  12. // Due to some questionable network security, you realize you might be able to solve both of these problems at the same time.
  13. // The automatic passport scanners are slow because they're having trouble detecting which passports have all required fields. The expected fields are as follows:
  14. // byr (Birth Year)
  15. // iyr (Issue Year)
  16. // eyr (Expiration Year)
  17. // hgt (Height)
  18. // hcl (Hair Color)
  19. // ecl (Eye Color)
  20. // pid (Passport ID)
  21. // cid (Country ID)
  22. // Passport data is validated in batch files (your puzzle input). Each passport is represented as a sequence of key:value pairs separated by spaces or newlines. Passports are separated by blank lines.
  23. // Here is an example batch file containing four passports:
  24. // ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
  25. // byr:1937 iyr:2017 cid:147 hgt:183cm
  26. // iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
  27. // hcl:#cfa07d byr:1929
  28. // hcl:#ae17e1 iyr:2013
  29. // eyr:2024
  30. // ecl:brn pid:760753108 byr:1931
  31. // hgt:179cm
  32. // hcl:#cfa07d eyr:2025 pid:166559648
  33. // iyr:2011 ecl:brn hgt:59in
  34. // The first passport is valid - all eight fields are present. The second passport is invalid - it is missing hgt (the Height field).
  35. // The third passport is interesting; the only missing field is cid, so it looks like data from North Pole Credentials, not a passport at all! Surely, nobody would mind if you made the system temporarily ignore missing cid fields. Treat this "passport" as valid.
  36. // The fourth passport is missing two fields, cid and byr. Missing cid is fine, but missing any other field is not, so this passport is invalid.
  37. // According to the above rules, your improved system would report 2 valid passports.
  38. // Count the number of valid passports - those that have all required fields. Treat cid as optional. In your batch file, how many passports are valid?
  39. func main() {
  40. file, _ := os.Open("input.txt")
  41. defer file.Close()
  42. passports := []map[string]string{}
  43. passport := map[string]string{}
  44. scanner := bufio.NewScanner(file)
  45. for scanner.Scan() {
  46. text := scanner.Text()
  47. if len(text) == 0 {
  48. // Empty row, new passport
  49. passports = append(passports, passport)
  50. passport = map[string]string{}
  51. } else {
  52. for _, part := range strings.Split(text, " ") {
  53. keyValue := strings.Split(part, ":")
  54. passport[keyValue[0]] = keyValue[1]
  55. }
  56. }
  57. }
  58. // Last passport
  59. passports = append(passports, passport)
  60. count := 0
  61. ppLoop:
  62. for _, pp := range passports {
  63. for _, field := range []string{"byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"} {
  64. if _, fieldPresent := pp[field]; !fieldPresent {
  65. continue ppLoop
  66. }
  67. }
  68. count++
  69. }
  70. fmt.Println("Part 1:", count)
  71. // Part 2, stricter rules
  72. // The line is moving more quickly now, but you overhear airport security talking about how passports with invalid data are getting through. Better add some data validation, quick!
  73. // You can continue to ignore the cid field, but each other field has strict rules about what values are valid for automatic validation:
  74. // byr (Birth Year) - four digits; at least 1920 and at most 2002.
  75. // iyr (Issue Year) - four digits; at least 2010 and at most 2020.
  76. // eyr (Expiration Year) - four digits; at least 2020 and at most 2030.
  77. // hgt (Height) - a number followed by either cm or in:
  78. // If cm, the number must be at least 150 and at most 193.
  79. // If in, the number must be at least 59 and at most 76.
  80. // hcl (Hair Color) - a # followed by exactly six characters 0-9 or a-f.
  81. // ecl (Eye Color) - exactly one of: amb blu brn gry grn hzl oth.
  82. // pid (Passport ID) - a nine-digit number, including leading zeroes.
  83. // cid (Country ID) - ignored, missing or not.
  84. // Your job is to count the passports where all required fields are both present and valid according to the above rules. Here are some example values:
  85. // byr valid: 2002
  86. // byr invalid: 2003
  87. // hgt valid: 60in
  88. // hgt valid: 190cm
  89. // hgt invalid: 190in
  90. // hgt invalid: 190
  91. // hcl valid: #123abc
  92. // hcl invalid: #123abz
  93. // hcl invalid: 123abc
  94. // ecl valid: brn
  95. // ecl invalid: wat
  96. // pid valid: 000000001
  97. // pid invalid: 0123456789
  98. // Here are some invalid passports:
  99. // eyr:1972 cid:100
  100. // hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926
  101. // iyr:2019
  102. // hcl:#602927 eyr:1967 hgt:170cm
  103. // ecl:grn pid:012533040 byr:1946
  104. // hcl:dab227 iyr:2012
  105. // ecl:brn hgt:182cm pid:021572410 eyr:2020 byr:1992 cid:277
  106. // hgt:59cm ecl:zzz
  107. // eyr:2038 hcl:74454a iyr:2023
  108. // pid:3556412378 byr:2007
  109. // Here are some valid passports:
  110. // pid:087499704 hgt:74in ecl:grn iyr:2012 eyr:2030 byr:1980
  111. // hcl:#623a2f
  112. // eyr:2029 ecl:blu cid:129 byr:1989
  113. // iyr:2014 pid:896056539 hcl:#a97842 hgt:165cm
  114. // hcl:#888785
  115. // hgt:164cm byr:2001 iyr:2015 cid:88
  116. // pid:545766238 ecl:hzl
  117. // eyr:2022
  118. // iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719
  119. // Count the number of valid passports - those that have all required fields and valid values. Continue to treat cid as optional. In your batch file, how many passports are valid?
  120. count = 0
  121. yrValid := func(byr string, min, max int) bool {
  122. byrInt, err := strconv.Atoi(byr)
  123. if err != nil || byrInt < min || byrInt > max {
  124. return false
  125. }
  126. return true
  127. }
  128. hgtValid := func(hgt string) bool {
  129. if strings.HasSuffix(hgt, "cm") {
  130. if no, err := strconv.Atoi(strings.TrimSuffix(hgt, "cm")); err != nil || no < 150 || no > 193 {
  131. return false
  132. }
  133. } else if strings.HasSuffix(hgt, "in") {
  134. if no, err := strconv.Atoi(strings.TrimSuffix(hgt, "in")); err != nil || no < 59 || no > 76 {
  135. return false
  136. }
  137. } else {
  138. return false
  139. }
  140. return true
  141. }
  142. hclValid := func(hcl string) bool {
  143. return regexp.MustCompile(`^#[a-f0-9]{6}$`).Match([]byte(hcl))
  144. }
  145. eclValid := func(ecl string) bool {
  146. return ecl == "amb" || ecl == "blu" || ecl == "brn" || ecl == "gry" || ecl == "grn" || ecl == "hzl" || ecl == "oth"
  147. }
  148. pidValid := func(pid string) bool {
  149. if len(pid) != 9 {
  150. return false
  151. }
  152. if id, err := strconv.Atoi(pid); err != nil || id == 0 {
  153. return false
  154. }
  155. return true
  156. }
  157. for _, pp := range passports {
  158. if byr, byrPresent := pp["byr"]; !(byrPresent && yrValid(byr, 1920, 2002)) {
  159. continue
  160. }
  161. if iyr, iyrPresent := pp["iyr"]; !(iyrPresent && yrValid(iyr, 2010, 2020)) {
  162. continue
  163. }
  164. if eyr, eyrPresent := pp["eyr"]; !(eyrPresent && yrValid(eyr, 2020, 2030)) {
  165. continue
  166. }
  167. if hgt, hgtPresent := pp["hgt"]; !(hgtPresent && hgtValid(hgt)) {
  168. continue
  169. }
  170. if hcl, hclPresent := pp["hcl"]; !(hclPresent && hclValid(hcl)) {
  171. continue
  172. }
  173. if ecl, eclPresent := pp["ecl"]; !(eclPresent && eclValid(ecl)) {
  174. continue
  175. }
  176. if pid, pidPresent := pp["pid"]; !(pidPresent && pidValid(pid)) {
  177. continue
  178. }
  179. count++
  180. }
  181. fmt.Println("Part 2:", count)
  182. }