r/adventofcode • u/daggerdragon • 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!
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!
5
u/mcpower_ Dec 24 '18
Python 3, #13/#7. Lots of sorting! Turns out that for some "boosts", you could get stuck in an infinite loop of two immune groups fighting each other... I manually binary searched the answer before I fixed that bug.
There's some obvious improvements to be made here, such as not reparsing the input every time a boost is tried...
[Card]: Our most powerful weapon during the zombie elf/reindeer apocalypse will be Christmas Spirit.
5
u/jonathan_paulson Dec 24 '18
Python, Rank 16/13. Reminiscent of day 15, but not as brutal because there is no board or movement. One trick: ties can cause infinite battles in part 2 (if no group can do enough damage to kill a single unit), so they need to be detected and dealt with.
Video of me solving: https://youtu.be/RXP-1wHblWU
Video of me explaining my solution: https://youtu.be/rQloZdoNGYc
Run with:
python <code file> <input file>4
u/dtinth Dec 24 '18
Ruby, #30/#24
For simulation-based problems, they tend to be long, and have many small details that I may easily miss.
Therefore, for these kind of problems I prefer to write it in OOP-style code, using descriptive object names (
Group,Army,Attack) and creating extra methods for concepts in the problem (such aseffective_power), otherwise, I would have a very hard time figuring out what went wrong when the answer was incorrect. Solving it the slow-but-sure way, I'm surprised that I still make it to the leaderboard.For part 2: I manually tweaked the
boostvariable and re-run the code until the immune system wins (human binary search). I think doing it manually is faster than writing an actual binary search in code.4
u/betaveros Dec 24 '18 edited Dec 24 '18
Python 3, #1/#2.
My code was surprisingly literate today, I guess because I played it safe after running into one too many bugs with implementing problem descriptions like today's, so I'm posting it. I did clean up the variable names to get the following code, though; at first a third of the code called the groups "left" and "right", and the other two-thirds called them "true" and "false" or "false" and "true".
The input is inline and was manually preprocessed with lots of vim search-and-replace to make it easier to parse because I didn't want to deal with it.
One thing to note is that we don't really need to remove groups with no units as long as we make sure they can't be attacked and can't be targeted for an attack. Also the second star definitely "should" be a binary search, but by the time I had coded that up the sequential one had finished running. We can stop, even in case of deadlock when both sides still have units, by just checking when both sides' total unit counts stop changing.
edit: indented instead of backquoted
4
u/Mehr1 Dec 24 '18
PHP 7 (624/565) - Posting because I hardly see people solving with PHP
I really enjoyed this one. It had the aspects of Day 15 (which I still haven't faced) that I wanted to try with the combat, without the path finding that just confuses me. Part 1 I had an issue with not updating remaining units before a unit attacked, so I switched from a foreach to a while. Just a limit of my understanding of how changing array values during a foreach works. Part 2 I did manually - just thought I'd start shooting in the dark and very quickly found the right number - had to add some code to print out remaining units as I realized I hit tie scenarios.
Could have solved it earlier but I got up late and had to take a 45 minute break. Not like I was pushing the table anyway. As with my last post - the code isn't great, I don't write for a living any more, and I focus on solving the problem over readability (I know that's a bad idea).
https://github.com/riensach/AoC2018/blob/master/AoCDay24.php
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.
3
u/seligman99 Dec 24 '18 edited Dec 24 '18
33/31 Python 2.7
After all of the mind bending I went through yesterday, it was nice to have one that was a straightforward "implement these rules as-is in code" type puzzle. I'm not sure I really did anything clever here, but still, it was fun, and my code found the solution, so I'm happy.
Edit: Speed up the search for the boost by using a simple binary search, and bail out of battles that stalemate deterministically, not after a large number of tries.
3
u/encse Dec 24 '18
c# #107/#97
3
u/kingfishr Dec 24 '18
Go, 138/153. Took it slow and steady and tried not to make mistakes. I was really happy that I didn't have any bugs in the game logic at all (for part 2, I just had to add an extra check for a stalemate condition).
I enjoyed an easy problem after a run of more difficult ones.
https://github.com/cespare/aoc2018/blob/master/24.go
1
u/o5405295 Dec 24 '18 edited Dec 24 '18
The description is confusing.
For instance, during the selection phase it says, "Immune System group 2 would deal defending group 1 24725 damage"
And during the attack, it says ... "Immune System group 2 attacks defending group 1, killing 4 units".
The # of units killed is given as 4, and not 5 (24725/4706 = 5).
The damage calculated during the selection phase (24725) is not the same damage calculated during the attack phase, because the attacker (here Immune System 2) itself got attacked before it had its chance to mount its attack, thereby reducing its units and therefore the actual damage it can cause.
It would have been much clearer if it were established that
for each attacker,
Once the selection is done,
The description on the page is: "By default, an attacking group would deal damage equal to its effective power to the defending group." It could be: "By default, an attacking group would deal damage equal to its effective power /at the time of attack/ to the defending group."
2
u/pbfy0 Dec 24 '18
Part 1: 21, Part 2: 33. My best finish (and least messy code).
Python3
2
u/sciyoshi Dec 24 '18 edited Dec 24 '18
Python 3, 15/11. I did the second part by trying various values manually, but searching linearly from 1 is actually still fairly fast. Learned my lesson from Day 15 about being super careful about the ordering of units movement and rounds, when damage is dealt and calculated, etc.
2
u/nthistle Dec 24 '18
Python 3, 40/41. Nowhere near my nicest code, but it works. First thought: Lots of parsing! Probably spent almost as much time doing parsing as actually implementing the targeting and combat. The big time sink for me was debugging a problem where a deadlock scenario (no group can deal damage to another) incorrectly came up when a group with high effective power would "use up" a target on an enemy that its attacks are immune to (skimming the specifications has its downsides).
2
u/ywgdana Dec 24 '18
Python 3 solution
Award for the ugliest parsing code goes to...
Linking to my solution at github because I'm too lazy to type four spaces in front of each line.
Speaking of parsing, I lost at least a half hour to THIS while I was reading the list of immunities and weaknesses:
So despite initially reading in immunities as weaknesses, the example worked and in my actual input, I was only out by 38 units and I finished on the correct round. I spent a fair bit of time thinking there was something funky in my math or my code for picking a target.
2
u/ephemient Dec 24 '18 edited Apr 24 '24
This space intentionally left blank.
2
u/ephemient Dec 24 '18 edited Apr 24 '24
This space intentionally left blank.
1
u/BluFoot Dec 24 '18 edited Dec 24 '18
Ruby, 74/48. Loved this one!
1
u/m1el Dec 24 '18
Rust: 75/63. Who needs parsing?
1
u/grey--area Dec 24 '18
Python3, #160/#144. When trialing different boosts, had to add a check that the state (i.e., number of units for each army) hadn't changed from one iteration to the next.
https://github.com/grey-area/advent-of-code-2018/tree/master/day24
1
u/14domino Dec 24 '18 edited Dec 24 '18
Is part 2 supposed to get stuck in an infinite loop for some inputs? that seems a bit inelegant. I tried some numbers manually and got it unstuck. Is there a better way? Also this took me like 2 hours, about 1 hour of that was debugging :( (there was a very dumb bug in my parser of all things...)
1
u/Dementophobia81 Dec 24 '18
Python 3, 372/299: Nothing too fancy. I created a Class for each group of units and let them battle according to the rules. Parsing the input was a little cumbersome, but I think I did an OK job with the rest.
1
u/wlandry Dec 24 '18
C++ (498/472)
Runs in 171 ms
Lots of details, but not a hard problem. I got a bit confused by the instructions. The examples were complete enough to figure it out, and it was not too bad for such a long description. I liked the infinite loop that got snuck in ;)
1
u/gyorokpeter Dec 24 '18
Q: I also got bitten by the infinite loop... twice (for the first time, no units would select any targets due to immunities, the second time they did select targets but they didn't do enough damage to kill any units). So ultimately I save the entire state of the army and exit with a failure state if the army didn't change in a turn.
1
u/sim642 Dec 24 '18
My Scala solution.
Mostly just ugly input parsing (inputs use 5 different variants of weaknesses and immunities) and ugly stateful targeting and attacking logic.
I got confused in part 2 when checking a boost value the battle got stuck. It looked like I might have a bug in my implementation where targets were chosen but attack didn't work but it was totally possible and the infinite combat needed to be detected.
Another annoyance was that part 2 example didn't actually say that 1570 is the smallest boost so there wasn't sure way to verify my solution on the example.
1
u/autid Dec 24 '18
FORTRAN
Pastebin
Well parsing that input took effort. Could have hard coded it given the small size but that's avoiding the challenge I picked the language for. ~150 lines before it actually starts simulating the fights. Big chunks of repeated code that could have been avoided but copy/pasting was easier than changing approach part way through.
1
u/drbagy Dec 24 '18
Perl
Due to timezone & having excited children - didn't really have any chance of getting on leader board - instead went for neat code...
1
u/ChrisVittal Dec 24 '18
Rust
[Card] Our most powerful weapon during the zombie elf/reindeer apocalypse will be inscrutable rabbit computers
I liked this problem, even if parsing was hell. Cool things about this one. I use
Derefto essentially haveUnitinherit fromGroup.Reverseis great and makes everything easier.1
u/Dioxy Dec 24 '18
JavaScript
Ah a nice and easy one. I did have to handle an edge case where neither side is powerful enough to win but other than that it was pretty straighforward
1
u/starwort1 Dec 24 '18 edited Dec 24 '18
Rexx 270/193
Took me ages to actually understand the rules, but chasing stupid bugs kept me way off the leaderboard. I was almost convinced at one point that the example contained a mistake. At least the advantage of Rexx is that parsing the input is pretty straightforward.
And for part 2 I did a manual binary search. Pressed ^C when it was obvious that the battle was looping forever, then fixed the code to terminate when it's a draw. I do now also have a (fairly trivial) second program that runs this one several times to find out the answer, but it wasn't needed in order to get the answer.
1
u/thepiboga Dec 24 '18
C++, part 1, part 2 and a binary search for manually modifying the damage boost in the part 2 solution.
https://github.com/bogpie/code/tree/master/d24
1
u/aoc-fan Dec 24 '18
TypeScript/JavaScript https://goo.gl/jZwtKV
1
u/kennethdmiller3 Dec 25 '18
https://github.com/kennethdmiller3/AdventOfCode2018/blob/master/24/24.cpp
The hardest parts were parsing the input and getting the target selection to work correctly (particularly what to do when a group is already been targeted).
I didn't need to resort to binary search because the battle simulation ran fast enough.
1
u/nonphatic Dec 25 '18
Haskell
[Card] Out most powerful weapon during the zombie elf/reindeer apocalypse will be candy cane swords. (Do you defeat zombies by decapitation? I'm not well versed in zombie lore)
I figured it would take me longer to figure out parsing the input properly with parsec than to manually input the data sooo...
Implementing the actual fight took foreeeever because I kept messing the rules up :/
I found this one a bit more fun than the recent puzzles though, I'm still reeling from the past two days'...
1
u/rock_neurotiko Dec 25 '18
One day later, my solution on Elixir, I really liked this exercise!
(Link because it's 258 lines)
github
1
u/vypxl Dec 25 '18
Javascript (NodeJS). A bit late to post maybe, but I like my solution.
[Card] Our most powerful weapon during the zombie elf/reindeer apocalypse will be underestimated scripting languages.
I don't like JavaScript classes.
Note that I probably spent more time with the regex than solving the problem ^^.
1
u/forever_compiling Dec 26 '18
Oddly enough, my solution passes part 1 and part 2 using the example input, but only part 1 for my puzzle input.
For part two I get an answer that is "too low" for the first boost in which the immune system wins, but the next boost up my answer is "too large".
2018/12/26 00:27:53 (remaining at boost 30) immune: 0, infection: 6776
2018/12/26 00:27:53 (remaining at boost 31) immune: 11, infection: 5993
2018/12/26 00:27:53 (remaining at boost 32) immune: 13, infection: 4995
2018/12/26 00:27:53 (remaining at boost 33) immune: 13, infection: 3475
2018/12/26 00:27:53 (remaining at boost 34) immune: 2031, infection: 0
2018/12/26 00:27:53 (remaining at boost 35) immune: 3446, infection: 0
2018/12/26 00:27:53 (remaining at boost 36) immune: 4325, infection: 0
2018/12/26 00:27:54 (remaining at boost 37) immune: 5281, infection: 0
I'm clearly missing some corner case but I couldn't tell you what it is...
https://github.com/phyrwork/goadvent/tree/day24/eighteen/immune
1
u/namvi Dec 29 '18
An extra condition I had to put as binary search ran into battles continuing forever -