Clean up day 5.

This commit is contained in:
Danila Fedorin 2020-12-04 21:52:45 -08:00
parent 3d5c13cf6a
commit 0ba970d517

17
day5.cr
View File

@ -14,32 +14,23 @@ end
def get_id(str) def get_id(str)
col = str[0..6] col = str[0..6]
row = str[7..] row = str[7..]
{partition(col, (0..127).to_a)[0], partition(row, (0..7).to_a)[0]} id({partition(col, (0..127).to_a)[0], partition(row, (0..7).to_a)[0]})
end end
def id(t) def id(t)
r, c = t t[0]*8 + t[1]
r * 8 + c
end end
def part1 def part1
input = INPUT.clone input = INPUT.clone
max = input.max_of do |line| puts(input.max_of &->get_id(String))
r, c = get_id(line)
r * 8 + c
end
puts max
end end
def part2 def part2
input = INPUT.clone input = INPUT.clone
seen = Set(Int32).new seen = input.map &->get_id(String)
max = input.each do |line|
seen << id(get_id(line))
end
candidates = [] of Int32 candidates = [] of Int32
128.times do |r| 128.times do |r|
next if r == 0 || r == 127
8.times do |c| 8.times do |c|
next if seen.includes? id({r, c}) next if seen.includes? id({r, c})
candidates << id({r,c}) candidates << id({r,c})