this post was submitted on 03 Dec 2023
20 points (95.5% liked)

Advent Of Code

763 readers
1 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2023

Solution Threads

M T W T F S S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
20
submitted 11 months ago* (last edited 11 months ago) by Ategon to c/advent_of_code
 

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

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 2 points 11 months 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)

RubyThe 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