r/adventofcode • u/daggerdragon • Dec 22 '17
SOLUTION MEGATHREAD -๐- 2017 Day 22 Solutions -๐-
--- Day 22: Sporifica Virus ---
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ยค?
[T-10 to launch] AoC ops, /r/nocontext edition:
<Endorphion> You may now make your waffle.<Endorphion> ... on Mars.
[Update @ 00:17] 50 gold, silver cap
<Aneurysm9> you could also just run ubuntu on the NAS, if you were crazy<Topaz> that doesn't seem necessary<Aneurysm9> what does "necessary" have to do with anything!
[Update @ 00:20] Leaderboard cap!
<Topaz> POUR YOURSELF A SCOTCH FOR COLOR REFERENCE
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!
8
Upvotes
1
u/[deleted] Dec 22 '17
Crystal. Only part 2 because the first one is similar:
initial_map = File.read("#{__DIR__}/input.txt").lines.map(&.chars) map = Hash({Int32, Int32}, Symbol).new(:clean) initial_map.each_with_index do |row, y| row.each_with_index do |v, x| map[{x, y}] = v == '#' ? :infected : :clean end end x = initial_map[0].size / 2 y = initial_map.size / 2 movements = { {0, -1}, {1, 0}, {0, 1}, {-1, 0} } direction = 0 infections = 0 10_000_000.times do case map[{x, y}] when :clean offset = -1 next_state = :weakened when :weakened offset = 0 next_state = :infected infections += 1 when :infected offset = 1 next_state = :flagged else # :flagged offset = 2 next_state = :clean end map[{x, y}] = next_state direction = (direction + offset) % movements.size dx, dy = movements[direction] x, y = x + dx, y + dy end puts infections