Skip to content

Instantly share code, notes, and snippets.

@peterc
Last active December 7, 2021 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterc/d6e1272a9ba9707e8bde17f04491d119 to your computer and use it in GitHub Desktop.
Save peterc/d6e1272a9ba9707e8bde17f04491d119 to your computer and use it in GitHub Desktop.
advent of code day 3 part 2
lines = File.readlines("3.txt")
def dl(lines, m)
12.times do |pos|
rs = lines.map(&:chars).map { |s| s[pos] }.tally
.sort_by { |k, v| [v * m, k.ord * m] }
lines.select! { |l| l[pos] == rs[1][0] }
break lines.first.to_i(2) if lines.size == 1
end
end
p dl(lines.dup, 1) * dl(lines.dup, -1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment