2023-12-03

Table of Contents

1. Part 1

— Day 3: Gear Ratios —

You and the Elf eventually reach a gondola lift station; he says the gondola lift will take you up to the water source, but this is as far as he can bring you. You go inside.

It doesn't take long to find the gondolas, but there seems to be a problem: they're not moving.

"Aaah!"

You turn around to see a slightly-greasy Elf with a wrench and a look of surprise. "Sorry, I wasn't expecting anyone! The gondola lift isn't working right now; it'll still be a while before I can fix it." You offer to help.

The engineer explains that an engine part seems to be missing from the engine, but nobody can figure out which one. If you can add up all the part numbers in the engine schematic, it should be easy to work out which part is missing.

The engine schematic (your puzzle input) consists of a visual representation of the engine. There are lots of numbers and symbols you don't really understand, but apparently any number adjacent to a symbol, even diagonally, is a "part number" and should be included in your sum. (Periods (.) do not count as a symbol.)

Here is an example engine schematic:

467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..

In this schematic, two numbers are not part numbers because they are not adjacent to a symbol: 114 (top right) and 58 (middle right). Every other number is adjacent to a symbol and so is a part number; their sum is 4361.

Of course, the actual engine schematic is much larger. What is the sum of all of the part numbers in the engine schematic?

Get files

1.1. Answer

NUMBER = /[0-9]/
DOT = /[.]/
SYMBOL = /[\$|\/|\*|\+|\=|\&|\%|\#|\@|\-]/
MAP = []
SYMBOL_LIST = []
MARK = []
WORD_LIST =[]

class Node
  attr_accessor :row, :column, :value
  attr_accessor :left, :right

  def initialize(row, column, value)
    @row = row
    @column = column
    @value = value
  end

  def left_word(node)
    return '' if node.nil?

    word = left_word(node.left)

    MARK << node
    word + node.value
  end

  def right_word(node)
    return '' if node.nil?

    word = right_word(node.right)

    MARK << node
    node.value + word
  end

  def to_w
    l = left_word(left)
    r = right_word(right)

    MARK << self
    l + value + r
  end
end



File.readlines('input.txt').each.with_index do | line, row |
  tmp = []
  line.scan(/./).each.with_index do |item, column|
    node =  Node.new(row, column, item)

    tmp << node
    SYMBOL_LIST << node if item =~ SYMBOL

  end

  tmp.each.with_index do |node, index|
    if index == 0
      node.right = tmp[index + 1] if tmp[index + 1].value =~ NUMBER
    elsif index == tmp.count - 1
      node.left = tmp[index - 1] if tmp[index - 1].value =~ NUMBER
    else
      node.right = tmp[index + 1] if tmp[index + 1].value =~ NUMBER
      node.left = tmp[index - 1] if tmp[index - 1].value =~ NUMBER
    end
  end

  MAP << tmp

end

TOTAL_COLUMN = MAP[0].count
TOTAL_ROW = MAP.count

def scan(node)
  col = node.column
  row = node.row

  left   = MAP[row][col - 1]
  right  = MAP[row][col + 1]
  top    = MAP[row - 1][col]
  bottom = MAP[row + 1][col]
  top_left = MAP[row - 1][col - 1]
  top_right = MAP[row - 1][col + 1]
  bottom_left= MAP[row + 1][col - 1]
  bottom_right = MAP[row + 1][col + 1]

  direcitons = [left,right,top,bottom, top_left, top_right, bottom_left, bottom_right]

  direcitons.map do | n |
    if n.value =~ NUMBER
      WORD_LIST << n.to_w unless MARK.include? n
    end

  end

end


SYMBOL_LIST.each {|s| scan s}

p WORD_LIST.map(&:to_i).sum

2. Part 2

— Part Two —

The engineer finds the missing part and installs it in the engine! As the engine springs to life, you jump in the closest gondola, finally ready to ascend to the water source.

You don't seem to be going very fast, though. Maybe something is still wrong? Fortunately, the gondola has a phone labeled "help", so you pick it up and the engineer answers.

Before you can explain the situation, she suggests that you look out the window. There stands the engineer, holding a phone in one hand and waving with the other. You're going so slowly that you haven't even left the station. You exit the gondola.

The missing part wasn't the only issue - one of the gears in the engine is wrong. A gear is any * symbol that is adjacent to exactly two part numbers. Its gear ratio is the result of multiplying those two numbers together.

This time, you need to find the gear ratio of every gear and add them all up so that the engineer can figure out which gear needs to be replaced.

Consider the same engine schematic again:

467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..

In this schematic, there are two gears. The first is in the top left; it has part numbers 467 and 35, so its gear ratio is 16345. The second gear is in the lower right; its gear ratio is 451490. (The * adjacent to 617 is not a gear because it is only adjacent to one part number.) Adding up all of the gear ratios produces 467835.

What is the sum of all of the gear ratios in your engine schematic?

Just change the SYMBOLLIST regx and scan function.

NUMBER = /[0-9]/
DOT = /[.]/
SYMBOL = /[\*]/
MAP = []
SYMBOL_LIST = []
MARK = []
WORD_LIST =[]

class Node
  attr_accessor :row, :column, :value
  attr_accessor :left, :right

  def initialize(row, column, value)
    @row = row
    @column = column
    @value = value
  end

  def left_word(node)
    return '' if node.nil?

    word = left_word(node.left)

    MARK << node
    word + node.value
  end

  def right_word(node)
    return '' if node.nil?

    word = right_word(node.right)

    MARK << node
    node.value + word
  end

  def to_w
    l = left_word(left)
    r = right_word(right)

    MARK << self
    l + value + r
  end
end



File.readlines('input.txt').each.with_index do | line, row |
  tmp = []
  line.scan(/./).each.with_index do |item, column|
    node =  Node.new(row, column, item)

    tmp << node
    SYMBOL_LIST << node if item =~ SYMBOL

  end

  tmp.each.with_index do |node, index|
    if index == 0
      node.right = tmp[index + 1] if tmp[index + 1].value =~ NUMBER
    elsif index == tmp.count - 1
      node.left = tmp[index - 1] if tmp[index - 1].value =~ NUMBER
    else
      node.right = tmp[index + 1] if tmp[index + 1].value =~ NUMBER
      node.left = tmp[index - 1] if tmp[index - 1].value =~ NUMBER
    end
  end

  MAP << tmp

end

TOTAL_COLUMN = MAP[0].count
TOTAL_ROW = MAP.count

sym = SYMBOL_LIST[0]

def scan(node)
  col = node.column
  row = node.row

  left   = MAP[row][col - 1]
  right  = MAP[row][col + 1]
  top    = MAP[row - 1][col]
  bottom = MAP[row + 1][col]
  top_left = MAP[row - 1][col - 1]
  top_right = MAP[row - 1][col + 1]
  bottom_left= MAP[row + 1][col - 1]
  bottom_right = MAP[row + 1][col + 1]

  direcitons = [left,right,top,bottom, top_left, top_right, bottom_left, bottom_right]

  tmp = []
  direcitons.map do | n |
    if n.value =~ NUMBER
      tmp << n.to_w unless MARK.include? n
    end

  end

  if tmp.count > 1
    value = 1
    tmp.map {|n| value = n.to_i * value}
    WORD_LIST << value
  end

end


SYMBOL_LIST.each {|s| scan s}

p WORD_LIST.sum

Date: 2023-12-03 Sun 00:00

Author: Terry Fung

Created: 2024-11-10 Sun 14:09

Emacs 29.4 (Org mode 9.6.15)

Validate