1
0
Fork 0

Day twenty-five Part one

This commit is contained in:
Andrew Tomaka 2024-12-25 10:44:45 -05:00
parent 94e04cc796
commit c6c314ca30
Signed by: atomaka
GPG key ID: 61209BF70A5B18BE
2 changed files with 4068 additions and 0 deletions

3999
25/input Normal file

File diff suppressed because it is too large Load diff

69
25/main.rb Executable file
View file

@ -0,0 +1,69 @@
#!/usr/bin/env ruby
class Component < Array
def self.from_line(line)
new(line.map(&:chars)[1..-2].transpose.map { _1.count("#") })
end
end
class Lock < Component
def accepts?(key) = zip(key).all? { _1.sum <= 5 }
end
Key = Class.new(Component)
def create(line)
(line.first == "#####" ? Lock : Key).from_line(line)
end
input = (ARGV.first.nil? ? DATA : ARGF)
.readlines(chomp: true)
.slice_when { |a, b| b.empty? }
.map { _1.reject { |a| a.empty? } }
.map(&method(:create))
locks, keys = input.partition { _1.is_a?(Lock) }
p locks.sum { |lock| keys.count { lock.accepts?(_1) } }
__END__
#####
.####
.####
.####
.#.#.
.#...
.....
#####
##.##
.#.##
...##
...#.
...#.
.....
.....
#....
#....
#...#
#.#.#
#.###
#####
.....
.....
#.#..
###..
###.#
###.#
#####
.....
.....
.....
#....
#.#..
#.#.#
#####