Created
December 15, 2022 14:21
-
-
Save cjavdev/6b9ce18de21e56c33399e3b8c1bcff00 to your computer and use it in GitHub Desktop.
Part 1 - comparing ruby 3.1.2 vs 3.2.0-preview2
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
| require 'set' | |
| if ARGV.empty? | |
| data = DATA.readlines(chomp: true) | |
| y = 10 | |
| else | |
| data = File.readlines(ARGV[0], chomp: true) | |
| y = 2000000 | |
| end | |
| sensors = Set.new | |
| beacons = Set.new | |
| def distance(a, b) | |
| (a[0] - b[0]).abs + (a[1] - b[1]).abs | |
| end | |
| puts "Parsing lines..." | |
| lines = data.map do |line| | |
| /Sensor at x=(?<x>-?\d+), y=(?<y>-?\d+): closest beacon is at x=(?<bx>-?\d+), y=(?<by>-?\d+)/.match(line) | |
| end.map do |match| | |
| sensor = [match[:x].to_i, match[:y].to_i] | |
| beacon = [match[:bx].to_i, match[:by].to_i] | |
| d = distance(sensor, beacon) | |
| sensors << [sensor, d] | |
| beacons << beacon | |
| end | |
| possible = Set.new | |
| exclude = Set.new | |
| puts "Big loop..." | |
| (-10_000_000..10_000_000).each do |x| | |
| pt = [x, y] | |
| next if beacons.include?(pt) | |
| sensors.each do |sensor, d| | |
| if distance(sensor, pt) <= d | |
| exclude << pt | |
| break | |
| end | |
| end | |
| end | |
| p exclude.size | |
| # p beacons | |
| # p lines | |
| __END__ | |
| Sensor at x=2, y=18: closest beacon is at x=-2, y=15 | |
| Sensor at x=9, y=16: closest beacon is at x=10, y=16 | |
| Sensor at x=13, y=2: closest beacon is at x=15, y=3 | |
| Sensor at x=12, y=14: closest beacon is at x=10, y=16 | |
| Sensor at x=10, y=20: closest beacon is at x=10, y=16 | |
| Sensor at x=14, y=17: closest beacon is at x=10, y=16 | |
| Sensor at x=8, y=7: closest beacon is at x=2, y=10 | |
| Sensor at x=2, y=0: closest beacon is at x=2, y=10 | |
| Sensor at x=0, y=11: closest beacon is at x=2, y=10 | |
| Sensor at x=20, y=14: closest beacon is at x=25, y=17 | |
| Sensor at x=17, y=20: closest beacon is at x=21, y=22 | |
| Sensor at x=16, y=7: closest beacon is at x=15, y=3 | |
| Sensor at x=14, y=3: closest beacon is at x=15, y=3 | |
| Sensor at x=20, y=1: closest beacon is at x=15, y=3 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Made it a little faster: