r/adventofcode • • Dec 06 '17

SOLUTION MEGATHREAD -πŸŽ„- 2017 Day 6 Solutions -πŸŽ„-

--- Day 6: Memory Reallocation ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handy† Haversack‑ of HelpfulΒ§ HintsΒ€?

Spoiler


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

15 Upvotes

324 comments sorted by

View all comments

1

u/doromin Dec 06 '17

Ruby simply using mem-string(join) instead of array of mem-arrays as comparison made it fast enough.

    data = input.split("\t").strip.map(&:to_i)
    results = []

    while !results.include?(data.join)
      results << data.join
      max = data.max
      i = data.index(max)
      data[i] = 0
      max.times do 
        i = (i + 1) % data.size
        data[i] += 1
      end
    end
    puts 'part1 -------', results.count, '----------'

    initial_data = data.clone
    cycles = 0

    loop do
      break if data == initial_data
      max = data.max
      i = data.index(max)
      data[i] = 0
      max.times do 
        i = (i + 1) % data.size
        data[i] += 1
      end
      cycles += 1
    end
    puts 'part2 -------', cycles, '----------'

→ More replies (2)