Code for the Advent of Code event
aoc advent-of-code
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add Ruby solution for 2024 day 19

+21
+21
src/2024/19/solve.rb
··· 1 + #!/usr/bin/env ruby 2 + # frozen_string_literal: true 3 + 4 + PATTERNS = ARGF.readline.split(', ').map(&:strip) 5 + DESIGNS = ARGF.read.strip.lines.map(&:strip) 6 + 7 + # REGEX = %r{^(?:#{PATTERNS.join('|')})+$} 8 + 9 + $counts = {} 10 + 11 + def solve(design) 12 + return $counts[design] if $counts.key?(design) 13 + return 1 if design == '' 14 + $counts[design] = PATTERNS.select { design.start_with?(_1) }.sum do |pattern| 15 + solve(design[pattern.size..]) 16 + end 17 + end 18 + 19 + # puts DESIGNS.count { _1 =~ REGEX } 20 + puts DESIGNS.count { solve(_1) > 0 } 21 + puts DESIGNS.sum { solve _1 }