Created
December 16, 2020 14:14
-
-
Save MikeRogers0/becd1ba31b888a7ae475bdc81edffafc to your computer and use it in GitHub Desktop.
Advent Of Code - Day 16: Part 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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