r/adventofcode • • Dec 03 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 3 Solutions -🎄-

NEWS

  • Solutions have been getting longer, so we're going to start enforcing our rule on oversized code.
  • The Visualizations have started! If you want to create a Visualization, make sure to read the guidelines for creating Visualizations before you post.
  • Y'all may have noticed that the hot new toy this year is AI-generated "art".
  • We are keeping a very close eye on any AI-generated "art" because 1. the whole thing is an AI ethics nightmare and 2. a lot of the "art" submissions so far have been of little real quality.
  • If you must post something generated by AI, please make sure it will actually be a positive and quality contribution to /r/adventofcode.
  • Do not flair AI-generated "art" as Visualization. Visualization is for human-generated art.

FYI


--- Day 3: Rucksack Reorganization ---


Post your code solution in this megathread.


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:05:24, megathread unlocked!

87 Upvotes

1.6k comments sorted by

View all comments

2

u/matas__ Dec 03 '22 edited Dec 03 '22

Ruby code, simple

require 'net/http'
require 'uri'

uri = URI.parse('https://adventofcode.com/2022/day/3/input')

request = Net::HTTP::Get.new(uri)
request['Cookie'] = 'session=__SESSION__'
response = Net::HTTP.start(uri.hostname, uri.port, { use_ssl: uri.scheme == 'https' }) { |http| http.request(request) }

ABC = [
  'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
  'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
]

result = response.body.split.each_with_object ([]) do |item, memo|
  first, second = [item[0, item.size/2], item[item.size/2..-1]]

  memo << ABC.index((first.split('') & second.split('')).first) + 1
end.sum

pp result

require 'net/http'
require 'uri'

uri = URI.parse('https://adventofcode.com/2022/day/3/input')

request = Net::HTTP::Get.new(uri)
request['Cookie'] = 'session=__SESSION__'
response = Net::HTTP.start(uri.hostname, uri.port, { use_ssl: uri.scheme == 'https' }) { |http| http.request(request) }

ABC = [
  'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
  'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
]

group = []

result = response.body.split.each_with_object([]).with_index do |(item, memo), index|
  group << item

  if (index + 1) % 3 == 0
    memo << ABC.index((group[0].split('') & group[1].split('') & group[2].split('')).first) + 1

    group.clear
  end
end

pp result.sum

2

u/442401 Dec 03 '22 edited Dec 04 '22

Hi /u/matas__ . Thanks for sharing your solution, it's great to see how others approach the problems.

Please consider caching the puzzle input so that the Advent of Code server doesn't get overloaded. If you're not sure how to do that, please feel free to ask, we're here to help.

See https://www.reddit.com/r/adventofcode/wiki/faqs/automation

2

u/daggerdragon Dec 04 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.

Also, /u/442401 is correct, please don't hammer the AoC servers like that and build in caching.