Day 3: Gear Ratios


Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ or pastebin (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


🔒This post will be unlocked when there is a decent amount of submissions on the leaderboard to avoid cheating for top spots

🔓 Edit: Post has been unlocked after 11 minutes

  • Ananace
    link
    fedilink
    21 year ago

    I get the feeling that I should include some default types for handling 2D maps in my boilerplate, it’s a very recurring problem in AoC after all.

    My solution is reasonably simplistic - and therefore also a bit slow, but the design meant I could do part 2 with just a few extra lines of code on the already processed data, here’s the functional part of it; (I push the previous days solution as part of my workflow for starting with the current day so the full code won’t be up until tomorrow)

    Ruby

    The code has been compressed for brevity.

    Point = Struct.new('Point', :x, :y)
    PartNumber = Struct.new('PartNumber', :number, :adjacent) do
      def adjacent?(to); adjacent.include?(to); end
      def irrelevant?; adjacent.empty?; end
      def to_i; number; end
    end
    
    class Implementation
      def initialize
        @map = []; @dim = { width: 0, height: 0 }; @symbols = []; @numbers = []
      end
    
      def input(line)
        @dim[:width] = line.size; @dim[:height] += 1
        @map += line.chars
      end
    
      def calc
        for y in (0..@dim[:height]-1) do
          for x in (0..@dim[:width]-1) do
            chr = get(x, y); next if chr =~ /\d/ || chr == '.'
            @symbols << Point.new(x, y)
          end
        end
    
        for y in (0..@dim[:height]-1) do
          buf = ""; adj = []
          for x in (0..@dim[:width]) do # Going one over, to fake a non-number as an end char on all lines
            chr = get(x, y)
            if chr =~ /\d/
              buf += chr
              (-1..1).each do |adj_x|
                (-1..1).each do |adj_y|
                  next if adj_x == 0 && adj_y == 0 ||
                    (x + adj_x < 0) || (x + adj_x >= @dim[:width]) ||
                    (y + adj_y < 0) || (y + adj_y >= @dim[:height])
                  sym = Point.new(x + adj_x, y + adj_y)
                  adj << sym if @symbols.any? sym
                end
              end
            elsif !buf.empty?
              @numbers << PartNumber.new(buf.to_i, adj)
              buf = ""; adj = []
            end
          end
        end
      end
    
      def output
        part1 = @numbers.reject(&:irrelevant?).map(&:to_i).sum
        puts "Part 1:", part1
    
        gears = @symbols.select do |sym|
          next unless get(sym) == '*'
          next unless @numbers.select { |num| num.adjacent? sym }.size == 2
          true
        end
        part2 = gears.sum { |gear| @numbers.select { |num| num.adjacent? gear }.map(&:to_i).inject(:*) }
    
        puts "Part 2:", part2
      end
    
      private
    
      def get(x, y = -1)
        y = x.y if x.is_a?(Point)
        x = x.x if x.is_a?(Point)
        return unless (0..@dim[:width]-1).include?(x) && (0..@dim[:height]-1).include?(y)
    
        @map[y * @dim[:width] + x % @dim[:width]]
      end
    end