Last active
December 8, 2020 06:42
-
-
Save matias-eduardo/e636b017f3312cc3c0d4db96673611e0 to your computer and use it in GitHub Desktop.
Advent of Code 2020 - Day 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "core:os" | |
import "core:fmt" | |
import "core:strings" | |
import "core:strconv" | |
import "core:container" | |
import "core:unicode" | |
main :: proc() { | |
valid_passport_count := 0; | |
data: []u8 = #load("day_4.input"); | |
passport_entries: []string = strings.split(s = string(data), sep = "\n\n"); | |
for passport_entry in passport_entries { | |
passport: map[string]string; | |
pairs: []string = strings.split_multi(s = passport_entry, substrs = {" ", "\n"}); | |
for pair in pairs { | |
key_value: []string = strings.split(s = pair, sep = ":"); | |
key: string = key_value[0]; | |
value: string = key_value[1]; | |
switch key { | |
case "byr": passport["byr"] = value; | |
case "iyr": passport["iyr"] = value; | |
case "eyr": passport["eyr"] = value; | |
case "hgt": passport["hgt"] = value; | |
case "hcl": passport["hcl"] = value; | |
case "ecl": passport["ecl"] = value; | |
case "pid": passport["pid"] = value; | |
case "cid": passport["cid"] = value; | |
case: fmt.panicf("Invalid key: %s", key); | |
} | |
} | |
if valid_passport(passport) == true do valid_passport_count += 1; | |
valid_passport :: proc(passport: map[string]string) -> bool { | |
{ | |
if len(passport["byr"]) != 4 { return false; } | |
byr, ok := strconv.parse_int(passport["byr"]); | |
if !ok { return false; } | |
if byr < 1920 || byr > 2002 { return false; } | |
} | |
{ | |
if len(passport["iyr"]) != 4 { return false; } | |
iyr, ok := strconv.parse_int(passport["iyr"]); | |
if !ok { return false; } | |
if iyr < 2010 || iyr > 2020 { return false; } | |
} | |
{ | |
if len(passport["eyr"]) != 4 { return false; } | |
eyr, ok := strconv.parse_int(passport["eyr"]); | |
if !ok { return false; } | |
if eyr < 2020 || eyr > 2030 { return false; } | |
} | |
{ | |
if strings.has_suffix(passport["hgt"], "cm") { | |
hgt_string := strings.trim_suffix(passport["hgt"], "cm"); | |
hgt, ok := strconv.parse_int(hgt_string); | |
if !ok { return false; } | |
if hgt < 150 || hgt > 193 { return false; } | |
} else if strings.has_suffix(passport["hgt"], "in") { | |
hgt_string: string = strings.trim_suffix(passport["hgt"], "in"); | |
hgt, ok := strconv.parse_int(hgt_string); | |
if !ok { return false; } | |
if hgt < 59 || hgt > 76 { return false; } | |
} else { | |
return false; | |
} | |
} | |
{ | |
if strings.has_prefix(passport["hcl"], "#") { | |
hcl_string := strings.trim_prefix(passport["hcl"], "#"); | |
if len(hcl_string) != 6 { return false; } | |
for hex_symbol in hcl_string { | |
is_hex_symbol_valid := false; | |
for valid_hex_symbol in "0123456789abcdef" { | |
if hex_symbol == valid_hex_symbol { | |
is_hex_symbol_valid = true; | |
break; | |
} | |
} | |
if is_hex_symbol_valid == false { | |
return false; | |
} | |
} | |
} else { | |
return false; | |
} | |
} | |
{ | |
switch passport["ecl"] { | |
case "amb", "blu", "brn", "gry", "grn", "hzl", "oth": // ok | |
case: return false; | |
} | |
} | |
{ | |
if len(passport["pid"]) != 9 { return false; } | |
for digit in passport["pid"] { | |
if !unicode.is_digit(digit) { return false; } | |
} | |
} | |
return true; | |
} | |
} | |
fmt.println("valid_passport_count:", valid_passport_count); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment