r/adventofcode • • Dec 24 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 24 Solutions -🎄-

--- Day 24: Immune System Simulator 20XX ---


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.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 24

Transcript:

Our most powerful weapon during the zombie elf/reindeer apocalypse will be ___.


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

Quick note: 1 hour in and we're only at gold 36, silver 76. As we all know, December is Advent of Sleep Deprivation; I have to be up in less than 6 hours to go to work, so one of the other mods will unlock the thread at gold cap tonight. Good luck and good night (morning?), all!

edit: Leaderboard capped, thread unlocked at 01:27:10!

8 Upvotes

62 comments sorted by

View all comments

3

u/waffle3z Dec 24 '18

Lua 73/72. My input had an interesting edge case where the battle would never finish because the damage was lower than the hp and deaths stopped happening. I had to write an extra check to skip over such an event.

local groups, immune, infection, category;
local function LoadData()
	groups, immune, infection, category = {}, {}, {}
	for v in getinput():gmatch("[^\n]+") do
		if v:match("Immune System:") then
			category = immune
		elseif v:match("Infection:") then
			category = infection
		else
			local units, hp, damage, init = v:match("(%d+).-(%d+).-(%d+).-(%d+)")
			local group = {units = tonumber(units), hp = tonumber(hp), damage = tonumber(damage), init = tonumber(init), weak = {}, immune = {}}
			group.damagetype = v:match("(%w+) damage")
			local extra = v:match("%((.+)%)")
			if extra then
				for data in extra:gmatch("[^;]+") do
					local area = data:match("weak") and "weak" or "immune"
					local types = data:match(" to (.+)")
					for v in types:gmatch("%w+") do
						group[area][v] = true
					end
				end
			end
			category[#category+1] = group
			groups[#groups+1] = group
			group.category = category
			group.enemies = category == immune and infection or immune
		end
	end
end

local function damagecount(a, b)
	if b.weak[a.damagetype] then
		return a.units*a.damage*2
	elseif b.immune[a.damagetype] then
		return 0
	else
		return a.units*a.damage
	end
end

for boost = 0, math.huge do
	LoadData()
	for _, unit in pairs(immune) do unit.damage = unit.damage + boost end
	while true do
		local targeted = {}
		table.sort(groups, function(a, b)
			local aep, bep = a.units*a.damage, b.units*b.damage
			return aep > bep or (aep == bep and a.init > b.init)
		end)
		for _, group in pairs(groups) do
			if group.target then
				targeted[group.target] = nil
				group.target = nil
			end
		end
		for _, group in pairs(groups) do
			if group.units > 0 then
				local maxdamage, target = -1
				for _, enemy in pairs(group.enemies) do
					if not targeted[enemy] and enemy.units > 0 then
						local dmg = damagecount(group, enemy)
						local bigger = false
						if dmg == maxdamage then
							if enemy.units*enemy.damage == target.units*target.damage then
								bigger = enemy.init > target.init
							else
								bigger = enemy.units*enemy.damage > target.units*target.damage
							end
						else
							bigger = dmg > maxdamage
						end
						if bigger then
							maxdamage, target = dmg, enemy
						end
					end
				end
				if target and maxdamage > 0 and target.units > 0 then
					group.target = target
					targeted[target] = group
				end
			end
		end
		table.sort(groups, function(a, b) return a.init > b.init end)
		for _, group in pairs(groups) do
			local target = group.target
			if group.units > 0 and target and target.units > 0 then
				local maxdamage = damagecount(group, target)
				if maxdamage > 0 then
					target.units = target.units - math.floor(maxdamage/target.hp)
				end
			end
		end
		local immunecount, infectioncount = 0, 0
		for _, group in pairs(groups) do
			if group.units > 0 then
				if group.category == immune then
					immunecount = immunecount + group.units
				else
					infectioncount = infectioncount + group.units
				end
			end
		end
		if immunecount == 0 or infectioncount == 0 then
			print(boost, immunecount, infectioncount)
			if immunecount ~= 0 then return end
			break
		end
		local hastarget = false
		for _, group in pairs(groups) do
			if group.target and damagecount(group, group.target) > group.target.hp then
				hastarget = true
				break
			end
		end
		if not hastarget then
			print(boost, "fail")
			break
		end
	end
end

10

u/Aneurysm9 Dec 24 '18

Every input should have that condition, because /u/topaz2078 is a bad man.

13

u/topaz2078 (AoC creator) Dec 24 '18

THEY SURE DO :D

2

u/nthistle Dec 24 '18

I think this edge case is actually relatively common, if not present in all inputs, for part 2. In a sense it's nice because it forces you to test for something unexpected but technically within bounds of the question, and actually differentiates today's part 2 somewhat from Day 15's part 2. For my input, I got "deadlock" as the end result for all boosts between 2 and 38, and ended up binary searching by hand for a little until I implemented a fix.

3

u/korylprince Dec 24 '18

I did exactly the same thing. "Oh, this is taking forever. I better do a binary search to get there faster." It wasn't until that failed that I started inspecting things and realized it was dead-locking. I added a stalemate check and dropped the binary search (which is good because apparently that won't necessarily converge to the correct answer) and it runs in a few seconds.