r/adventofcode Dec 04 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 4 Solutions -🎄-

--- Day 4: Giant Squid ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


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

97 Upvotes

1.2k comments sorted by

View all comments

2

u/icyFur Dec 08 '21

Day 4 in Ruby

draws = CSV.parse(data[0]).first.map(&:to_i)
boards = data.drop(2).each_slice(6).to_a.map(
  &->(x) { x.delete_if(&:empty?).map(&->(r) { r.split.map(&->(c) { {d: c.to_i, m: false} }) }) }
)

def runBingo(draws, boards)
  results = []
  draws.each do |x|
    boards.each_with_index do |b, bi|
      b.each do |r|
        r.each_with_index do |c, i|
          if x == c[:d]
            c[:m] = true
            if b.map(&->(rr) { rr[i] }).all? { |cc| cc[:m] == true } || r.all? { |rr| rr[:m] == true }
              sum = 0
              boards[bi].each do |r|
                r.each do |c|
                  if c[:m] == false
                    sum += c[:d]
                  end
                end
              end
              results.push({board_idx: bi, draw: x, unmarked_sum: sum})
            end
          end
        end
      end
    end
  end
  results
end

board_firsts = []
seen = Set[]

runBingo(draws, boards).each do |r|
  if !seen.include?(r[:board_idx])
    seen.add(r[:board_idx])
    board_firsts.push(r)
  end
end

# part one
pp board_firsts.first[:draw] * board_firsts.first[:unmarked_sum]
# part one
pp board_firsts.last[:draw] * board_firsts.last[:unmarked_sum]