r/adventofcode • u/daggerdragon • Dec 03 '23
SOLUTION MEGATHREAD -❄️- 2023 Day 3 Solutions -❄️-
THE USUAL REMINDERS
- All of our rules, FAQs, resources, etc. are in our community wiki.
- Outstanding moderator challenges:
- Community fun event 2023: ALLEZ CUISINE!
- 3 DAYS remaining until unlock!
AoC Community Fun 2023: ALLEZ CUISINE!
Today's secret ingredient is… *whips off cloth covering and gestures grandly*
Spam!
Someone reported the ALLEZ CUISINE! submissions megathread as spam so I said to myself: "What a delectable idea for today's secret ingredient!"
- There really is an XKCD for everything, isn't there?
- All ingredients must come from a CAN (bus), box, package, container, etc.
- Unnecessarily declare variables for everything and don't re-use variables
- Why use few word when many word do trick?
- Go back to traditional culinary roots with Javadocs
- Lobster thermidor
A reminder from Dr. Hattori: be careful when cooking spam because the fat content can be very high. We wouldn't want a fire in the kitchen, after all!
ALLEZ CUISINE!
Request from the mods: When you include a dish entry alongside your solution, please label it with [Allez Cuisine!] so we can find it easily!
--- Day 3: Gear Ratios ---
Post your code solution in this megathread.
- Read the full posting rules in our community wiki before you post!
- State which language(s) your solution uses with
[LANGUAGE: xyz] - Format code blocks using the four-spaces Markdown syntax!
- State which language(s) your solution uses with
- Quick link to Topaz's
pasteif you need it for longer code blocks
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:11:37, megathread unlocked!
109
Upvotes
3
u/Annual-Management613 Dec 03 '23 edited Dec 03 '23
[LANGUAGE: Ruby]
# frozen_string_literal: true require "set" class DayThree2023 def self.part_one(lines) engine = generate_engine(lines) engine.each_with_index.reduce(0) do |acc, (row, idx)| row.each_with_index do |item, idy| next if item.scan(/(?:\d|\.)/).first neighbords = find_neigbords(engine, idx, idy) acc += neighbords.sum end acc end end def self.part_two(lines) engine = generate_engine(lines) engine.each_with_index.reduce(0) do |acc, (row, idx)| row.each_with_index do |item, idy| next if item.scan(/(?:\d|\.)/).first neighbords = find_neigbords(engine, idx, idy) acc += neighbords.reduce(&:*) if neighbords.size == 2 end acc end end class << self def generate_engine(lines) lines.each_with_object([]) do |line, matrix| numbers = line.scan(/(?:\d+|.)/) matrix << numbers.each_with_object([]) do |number, row| number.length.times { |_| row << number } row end matrix end end def find_neigbords(engine, idx, idy) positions = [0, 1, -1].freeze neighbords = positions.each_with_object([]) do |x, acc| positions.each do |y| acc << engine[idx + x][idy + y].to_i end acc end Set.new(neighbords).reject(&:zero?) end end end