Skip to content

Commit

Permalink
19 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
gregawoods committed Dec 19, 2023
1 parent 40c9d01 commit be800a9
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 1 deletion.
17 changes: 17 additions & 0 deletions examples/19.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
px{a<2006:qkq,m>2090:A,rfg}
pv{a>1716:R,A}
lnx{m>1548:A,A}
rfg{s<537:gd,x>2440:R,A}
qs{s>3448:A,lnx}
qkq{x<1416:A,crn}
crn{x>2662:A,R}
in{s<1351:px,qqz}
qqz{s>2770:qs,m<1801:hdj,R}
gd{a>3333:R,R}
hdj{m>838:A,pv}

{x=787,m=2655,a=1222,s=2876}
{x=1679,m=44,a=2067,s=496}
{x=2036,m=264,a=79,s=2244}
{x=2461,m=1339,a=466,s=291}
{x=2127,m=1623,a=2188,s=1013}
2 changes: 1 addition & 1 deletion lib/days/16.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Day16
left: [-1, 0]
}

Beam = Struct.new(:x, :y, :dir) do
Beam = Data.define(:x, :y, :dir) do
def move(dir)
mv = MOVEMENTS[dir]
Beam.new(x + mv[0], y + mv[1], dir)
Expand Down
90 changes: 90 additions & 0 deletions lib/days/19.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
class Day19

Rule = Data.define(:destination, :operation, :category, :value) do
def matches?(part)
return true if operation.nil?

part.send(category).send(operation, value)
end
end

Part = Struct.new(:x, :m, :a, :s) do
def value
x + m + a + s
end
end

def parse(input)
rule_regex = /(?<cat>[a-z])(?<op>[<>])(?<value>\d+):(?<dest>[a-zA-Z]+)/

flows_str, parts_str = input.split("\n\n")

flows = flows_str.split("\n").to_h do |line|
name, rest = line.split('{')

rules = rest[0...-1].split(',').map do |str|
if (match = str.match(rule_regex))
Rule.new(
match['dest'],
match['op'],
match['cat'].to_sym,
match['value'].to_i
)
else
Rule.new(str, nil, nil, nil)
end
end

[name, rules]
end

part_regex = /x=(?<x>\d+),m=(?<m>\d+),a=(?<a>\d+),s=(?<s>\d+)/

parts = parts_str.split("\n").map do |line|
if (match = line.match(part_regex))
Part.new(match['x'].to_i, match['m'].to_i, match['a'].to_i, match['s'].to_i)
else
raise "Unexpected part format #{line}!"
end
end

[flows, parts]
end

def part1(input)
flows, parts = parse(input)

accepted = []
rejected = []

parts.each do |part|
current = 'in'

loop do
if current == 'R'
rejected << part
break
elsif current == 'A'
accepted << part
break
end

workflow = flows[current]

workflow.each do |rule|
if rule.matches?(part)
current = rule.destination
break
end
end
end
end

accepted.sum(&:value)
end

def part2(input)
'TODO'
end

end
18 changes: 18 additions & 0 deletions test/19_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'minitest/autorun'
require 'minitest/pride'
require_relative '../app'

class TestDay19 < Minitest::Test
def setup
@data = File.read(File.join(APP_ROOT, 'examples', '19.txt')).rstrip
@day = Day19.new
end

def test_part1
assert_equal @day.part1(@data), 19114
end

def test_part2
assert_equal @day.part2(@data), ''
end
end

0 comments on commit be800a9

Please sign in to comment.