Add day 4 solutions.

This commit is contained in:
Danila Fedorin 2020-12-03 21:41:49 -08:00
parent f3e3960ea1
commit 2ed1ce8b06

90
day4.cr
View File

@ -1,49 +1,58 @@
INPUT = File.read("day4.txt").lines.map(&.chomp) INPUT = File.read("day4.txt").lines.map(&.chomp)
def parse_passport(string)
new_hash = {} of String => String
string.split(" ").each do |field|
k,v = field.split(":")
new_hash[k] = v
end
new_hash
end
def parse_passports(lines)
passports = [] of Hash(String, String)
passport = [] of String
lines.each do |line|
unless line.empty?
passport << line
next
end
passports << parse_passport(passport.join(" "))
passport = [] of String
end
passports << parse_passport(passport.join(" "))
end
def part1 def part1
input = INPUT.clone input = INPUT.clone
passlines = [] of Array(String) passports = parse_passports(input)
currpass = [] of String total = passports.count do |passport|
input.each do |line| ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"].all? do |key|
if line.empty? passport.has_key? key
passlines << currpass
currpass = [] of String
next
end end
currpass << line
end end
passlines << currpass puts total
end
total = 0 def part2
passlines.each do |pass| input = INPUT.clone
fields = pass.join(" ").split(" ") passports = parse_passports(input)
puts fields total = passports.count do |passport|
values = {} of String => String next unless value = passport["byr"]?
fields.each do |field|
key = field.split(":")[0]
values[key] = field.split(":")[1]
puts "#{key}: #{values[key]}"
end
next unless value = values["byr"]?
value = value.to_i32 value = value.to_i32
next unless value >= 1920 && value <= 2002 next unless value >= 1920 && value <= 2002
puts "byr" next unless value = passport["iyr"]?
next unless value = values["iyr"]?
value = value.to_i32 value = value.to_i32
next unless value >= 2010 && value <= 2020 next unless value >= 2010 && value <= 2020
puts "iry" next unless value = passport["eyr"]?
next unless value = values["eyr"]?
value = value.to_i32 value = value.to_i32
next unless value >= 2020 && value <= 2030 next unless value >= 2020 && value <= 2030
puts "eyr" next unless value = passport["hgt"]?
next unless value = values["hgt"]?
next unless value.ends_with?("cm") || value.ends_with?("in") next unless value.ends_with?("cm") || value.ends_with?("in")
ivalue = value[0..-3].to_i32 ivalue = value[0..-3].to_i32
if value.ends_with? "cm" if value.ends_with? "cm"
@ -52,33 +61,20 @@ def part1
next unless ivalue >= 59 && ivalue <= 76 next unless ivalue >= 59 && ivalue <= 76
end end
puts "hgt" next unless value = passport["hcl"]?
next unless value = values["hcl"]?
next unless value[0] == '#' && (value[1..]).to_i32(16) next unless value[0] == '#' && (value[1..]).to_i32(16)
puts "hcl" next unless value = passport["ecl"]?
next unless value = values["ecl"]?
next unless "amb blu brn gry grn hzl oth".split(" ").includes? value next unless "amb blu brn gry grn hzl oth".split(" ").includes? value
puts "ecl" next unless value = passport["pid"]?
next unless value = values["pid"]?
puts value
next unless value.size == 9 next unless value.size == 9
next unless value = value.to_i32? next unless value = value.to_i32?
puts "pid" true
total += 1
end end
puts total puts total
end end
def part2
input = INPUT.clone
end
part1 part1
part2 part2