AdventOfCode-2020/day5.cr

47 lines
826 B
Crystal
Raw Normal View History

2020-12-04 21:32:28 -08:00
INPUT = File.read("day5.txt").lines.map(&.chomp)
def partition(choices, list)
b = 0
e = list.size
choices.chars.each do |c|
b = (b+e)//2 if c == 'B' || c == 'R'
e = (b+e)//2 if c == 'F' || c == 'L'
end
return {b,e}
end
def get_id(str)
col = str[0..6]
row = str[7..]
2020-12-04 21:52:45 -08:00
id({partition(col, (0..127).to_a)[0], partition(row, (0..7).to_a)[0]})
2020-12-04 21:32:28 -08:00
end
def id(t)
2020-12-04 21:52:45 -08:00
t[0]*8 + t[1]
2020-12-04 21:32:28 -08:00
end
def part1
input = INPUT.clone
2020-12-04 21:52:45 -08:00
puts(input.max_of &->get_id(String))
2020-12-04 21:32:28 -08:00
end
def part2
input = INPUT.clone
2020-12-04 21:52:45 -08:00
seen = input.map &->get_id(String)
2020-12-04 21:32:28 -08:00
candidates = [] of Int32
128.times do |r|
8.times do |c|
next if seen.includes? id({r, c})
candidates << id({r,c})
end
end
candidates = candidates.select do |i|
seen.includes?(i-1) && seen.includes?(i+1)
end
puts candidates[0]
end
part1
part2