Skip to content

Instantly share code, notes, and snippets.

@MikeRogers0
Created December 16, 2020 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeRogers0/becd1ba31b888a7ae475bdc81edffafc to your computer and use it in GitHub Desktop.
Save MikeRogers0/becd1ba31b888a7ae475bdc81edffafc to your computer and use it in GitHub Desktop.
Advent Of Code - Day 16: Part 1
@section = File.open('input.real.txt').read.split("\n\n").compact
# Get all the rules as ranges (e.g. 0..3)
@rules = @section[0]
.scan(/([0-9\-]+)-([0-9]+)/im)
.collect { |start_range, end_range| (start_range.to_i..end_range.to_i) }
# Get all the numbers on the nearby tickets
@nearby_ticket_numbers = @section[2]
.gsub("nearby tickets:\n", '')
.gsub("\n", ',')
.split(',')
.compact
.collect(&:to_i)
# Filter out the ticket numbers which aren't within any of those ranges
@nearby_ticket_error_rate = @nearby_ticket_numbers
.select { |number| @rules.none? { |rule| rule.cover?(number) } }
.sum
# Part one:
puts @nearby_ticket_error_rate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment