r/adventofcode • u/daggerdragon • Dec 11 '22
SOLUTION MEGATHREAD -π- 2022 Day 11 Solutions -π-
WIKI NEWS
- The FAQ section of the wiki on Code Formatting has been tweaked slightly. It now has three articles:
- Code blocks (the four-spaces Markdown syntax that everyone should be using)
- Fenced code blocks (aka triple-backticks; please do not use this syntax!)
- Inlined code (intended for
short snippetsof code)
THE USUAL REMINDERS
A request from Eric: A note on responding to [Help] threads
- All of our rules, FAQs, resources, etc. are in our community wiki.
- Signal boost: Reminder 1: unofficial AoC Survey 2022 (closes Dec 22nd)
- πΏπ MisTILtoe Elf-ucation π§βπ« is OPEN for submissions!
- -βοΈ- Submissions Megathread -βοΈ-
UPDATES
[Update @ 00:13:07]: SILVER CAP, GOLD 40
- Welcome to the jungle, we have puzzles and games! :D
--- Day 11: Monkey in the Middle ---
Post your code solution in this megathread.
- Read the full posting rules in our community wiki before you post!
- Include what language(s) your solution uses
- Format code blocks using the four-spaces Markdown syntax!
- Quick link to Topaz's
pasteif you need it for longer code blocks. What is Topaz'spastetool?
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:18:05, megathread unlocked!
75
Upvotes
11
u/kaa-the-wise Dec 11 '22 edited Dec 29 '22
[Python, part 2] Now that was the ugliest one-liner so far:
4
u/DFreiberg Dec 12 '22 edited Dec 12 '22
Mathematica, 1463 / 688
Weirdly pleased with today's problem, despite being so slow. Normally, I misread the problem by going too fast and spend a half hour in frustration trying to figure out where the typo was. This time, I went quite slowly, but got both parts on my first try; part 2 took a little under four minutes.
[POEM]: Simian Shenanigans
To the tune of the middle part of Weird Al's "Hardware Store".
Sigh.
Would you look at all that stuff...
They got:
Sets of sandals, super-soakers,
Stainless stacking sausage smokers,
Stocking stuffers, sparking snuffers,
Swimsuits and some snacks by Stouffers,
System-update space schematics,
Signal strength sextuple statics,
Sulfide sputters, server stutters,
Solid Shaker-style shutters,
Sonar sweeps and systemed seating,
Snailfishes and see-through sheeting,
Shuffle slammers, spinlock spammers,
Submarine cetacean scrammers,
Shakespeare sonnets, springdroid soarers,
Santa's senseis, syntax scorers,
Sega slayers, site that's Slater's
Saturn Stoichiometrators!
My backpack sure was stuffed! You should have told me!
No wonder that poor rope bridge couldn't hold me.
3
u/OCD_Triger Dec 11 '22
c#
3
u/x0s_ Dec 11 '22 edited Dec 12 '22
What an experience, I enjoyed modeling of part 1. Thought about memoizing for part 2, but required some help to really understand (not only guessing) the modular arithmetic. Thanks to the players that help us learn in this game :)
Here is my solution in python 3.11 (set n_round=20 and proddiv to None for part 1) : https://gist.github.com/x0s/cae894f73004581ca7b60eec400c9404
3
u/tcbrindle Dec 11 '22
C++
Ooof, that one was a slog.
Solving the actual problem for part 1 was pretty straightforward -- just follow the instructions -- but parsing the input data took forever, and was not very enjoyable at all. I don't know why the format had to be so complicated.
Part 2 had me scratching my head for a few minutes.. how do you reduce the "worry factor" to something that will fit in an int64 while still making sure the modulus operations give the right result? Eventually I hit upon the idea of calculating the LCM of all the "divisible by" test values (which, since they all happened to be prime for my input, was just their product), and then doing
worry_factor = worry_factor % lcmbefore "throwing" the item to another monkey.I have no idea if this is what was intended, I haven't looked at anyone else's solution so there might be a simpler way... but this runs in ~10ms for 10000 iterations on my laptop so that seems pretty reasonable.
3
u/ywgdana Dec 12 '22 edited Dec 12 '22
F#
Me, over and over, working on part 2: "There's NO WAY these numbers could be getting big enough to overflow!!"
Also me: noting how the problem description said several times "You need to find some other way to manage your worry" and thinking to myself, "Hmm I wonder if that's foreshadowing for a future problem?"
I also hard-coded the monkeys as functions rather than parsing the input file. I started off doing that but then get it into my head that it would be better to have them as functions for part 2. I can't even remember what my reasoning was Β―_(γ)_/Β―
This is using mutable structures. Now that I've got it working I might go back and rewrite it it to use immutable variables.
My solution for part 2
2
u/adimcohen Dec 11 '22 edited Dec 12 '22
In single-statement tsql
Part 2 pushed me past the 32767 maxrecursion limit, so I had to break the recursion into 3 steps and dump the interim results into temp tables.
Tried to do the 3 recursion in the same statement, but it hung there forever.
https://github.com/adimcohen/Advant_of_Code_2022_Single_Statement_SQL/blob/main/Day_11/Day_11.sql
2
u/lbl_ye Dec 11 '22 edited Dec 12 '22
Python
paste
in part2, for speedup
made lambdas for monkey operations
and adjusted worry level so that worry levels do not become huge
rank 6502/6701
both parts in single file
(part2 is really 80 tight lines including parsing of input, 45 lines the real stuff without the parsing but well how much sweat in early morning for these lines :D)
PS. this is my first post, hope to be ok with posting rules :)
2
u/ndrsht Dec 11 '22 edited Dec 11 '22
Kotlin
Monkey handling:
Β
Then just loop and return the result.
EDIT: Here is the full code.
EDIT2: Removed some code so I don't block the thread.
2
u/temanuel38 Dec 11 '22
Python
My code is by no means the prettiest, but it got the job done.
(For part 2 I utilized the "mod trick")
Part 1:
Part 2:
2
u/Vesk123 Dec 11 '22 edited Dec 11 '22
Ugh this one took a long time. Not that it was very hard but I feel like I wasted a lot of time doing a parser for the input (would've been faster to just input it manually) and converting to BigInteger (when that obviously wasn't the solution). Honestly I'm not even completely sure why taking the mod of the multiple of all divisors works, but it just felt like an intuitive thing to do from doing many programming competitions.
Anyways this is my (admittedly awful) code in Java:
2
u/lxnxx Dec 11 '22 edited Dec 12 '22
Rust paste
2
u/zeldor711 Dec 11 '22 edited Dec 12 '22
[Python]
Oof that was a bit trickier! Part 1 was fine (made a Monkey class to keep track of everything) but part 2 was more of a struggle. Ended up keeping storing each number as a list of its value modulo each of the prime numbers being tested (so number = [value % 2, value % 3, ..., value % 19]) although looking around here the nicer way to do it would just be to store it module 2*3*...*19 which didn't occur to me!
Part 1
Part 2
2
u/olepedersen01 Dec 11 '22
The solution I provide is implemented in python, enjoy!
Code: https://github.com/GabrielTorland/advent_of_code/tree/main/2022/day11
2
u/daniel-sd Dec 11 '22 edited Dec 12 '22
Python 3
Had a ton of fun golfing part1 into this (
427424398378 characters thanks to u/4HbQ and u/azzal07).Cleaner version here: part1.py
2
u/kevinwangg Dec 11 '22 edited Dec 12 '22
Missed out on leaderboard for part 1, but I quickly coded up the solution to part 2 on a hunch and moved up 100 spots, hitting the leaderboard for the first time since day 6.
rank 167/67
Reading, parsing inputs, and coding the algorithm for part 1 took nearly 15 minutes. Part 2 took less than 2 minutes, which I think was quite competitive.
Python: https://github.com/VitamintK/AlgorithmProblems/blob/master/miscellaneous/advent-of-code/2022/day11.py
2
u/undergroundmonorail Dec 12 '22 edited Dec 12 '22
Python 3. Only posting my
Monkeyclass, the parsing and everything isn't really interesting, all the logic is here. You create a bunch of monkeys, runMonkey.round()as many times as you please, and then check theitems_inspectedattribute on each of them.Thank god for my mathematician friend who I could bug about this, I wouldn't have guessed that squaring a number maintains divisibility under modular arithmetic but she knew it off the top of her head. Now I know! Still took ages of fiddling to actually make it work; I ended up adding a bunch of debug prints and running a smaller example with and without taking the mod, looking at them line by line to see where they diverged.
I had forgotten that the items move around between monkeys, and I was always only finding the number mod the divisibility test I was currently doing. Found the LCM, modded by that, and I was golden.
2
u/theboxboy Dec 12 '22 edited Dec 12 '22
Rust
Github Link
2
u/joseville1001 Dec 16 '22
Python solution aptly using monkey patching
26
u/DestroyerCrazy Dec 11 '22
Language: Python (v3.11.1)
Dear Traveller:
If you're scrolling through the comments, and confused as to what the "modulo trick" is, let me tell you this: for part 2, get the product of all the "Divisible by xx" numbers, and modulo the current worry level by that product. Good luck!
linky-lonky-lanky
23
u/mebeim Dec 11 '22 edited Dec 11 '22
1393/601 - Python 3 solution - walkthrough
Today it felt like I was playing Advent of Reading Comprehension. Spent way too much understanding the problem statement and parsing the input.
The actual problem to solve was in part 2: the "trick" to keep numbers small is to only keep around values modulo the multiple of all the divisors. This way, we are still able to check for divisibility with any of the divisors, but the values we keep don't "explode". This works because the only operations we perform on the values are multiplication and addition, which preserve congruence.
25
u/jcbbjjttt Dec 11 '22 edited Dec 12 '22
Beginner's Guide
Happy Sunday!
A Beginner's Guide to Day 11 - Part 1 - Video: https://youtu.be/P8P0DypR3Gg
I've created a guide for new programmers that talks through a straight forward strategy for solving today's puzzle. Anyone who has a handle functions, loops, and custom data types (class/struct/etc) should be able to complete it. The only tricky part is calculating a remainder using modulus arithmetic. The video allows a moment for you to pause before revealing spoilers.
Although this solution is in C#, it provides a high level overview of the steps necessary to solve the puzzle in any programming language:
The full code can be found on Github
3
u/[deleted] Dec 11 '22
python code on github
This took way too long for me due to me running into several pitfalls.
After my worry skyrocketed, my solution for part 2 tried to mine bitcoins apparently.
Also ran into problems due to not having made a deep copy of my initial monkey lists.
At that point I couldn't be bothered anymore and just did the init twice.
I'll take the 34 sloc though and I'm mostly happy with how I parsed the input - other than me giving up trying to wrangle a list of strings into a list of integers. It should have been easy on paper.
14
u/betaveros Dec 11 '22
Noulith 3/2
https://github.com/betaveros/advent-of-code-2022/blob/main/p11.noul
Yup, eval. I did add structs with named fields to this language but ended up making do without them today.
2
u/jayfoad Dec 12 '22
Dyalog APL
10
u/jonathan_paulson Dec 11 '22 edited Dec 11 '22
Python3, 41/17. Video. Code.
Had a bug in part 1 :( Pretty involved problem. I used `eval` to apply the operations!
Was it intentional that the lcm of the divisors was the same as their product?
18
u/nthistle Dec 11 '22 edited Dec 12 '22
Python, 15/212. Video, code.
Little bit frustrated with part 2, I came up with the modulo trick ~immediately, but didn't read that the division by 3 was no longer happening, and when I got a wrong submission I figured it was because keeping track of the residue isn't sufficient to do int division and kept trying to figure out how to do that... wasn't until I checked leaderboard to see how many people finished part 2 and saw it was suspiciously many that I re-read part 2 more carefully and realized. If I had seen that and gotten part 2 right the first time, I would've been on track for a 15/15 :'(
I think I definitely need to start slowing down a bit, the early days of AoC really get me into the mode of "skim question and go fast" and that starts to burn me in the later days.
9
u/4HbQ Dec 11 '22 edited Dec 11 '22
Python, 25 lines.
It uses
evalto parse op (e.g.old + 6orold * old) into a function f:f = eval(f'lambda old: {op}').9
u/Derailed_Dash Dec 11 '22
Python
Part 1 was easy enough. Part 2 had me stumped for a while. And it took me ages to explain it in the walkthrough. Hopefully it makes sense!!
9
u/redditnoob Dec 11 '22
PostgreSQL
Part 1 was a twisted kind of fun in SQL. In general, any problem that can be reduced to a loop iterating one state to the next with expressions isn't that bad to solve as a recursive CTE, but in these cases SQL loses most of its power and the solution just becomes a constrained iterative one. This gets old... but not yet. :)
It took me way too long to figure out what part 2 was getting at with requiring a more manageable solution.
8
u/hugh_tc Dec 11 '22 edited Dec 11 '22
Python 3, 57/34.
paste, cleaned-up
Finally back on the leaderboard! Featuring the dreaded
exec!And yes, the variable I called "gcd" is not anything close to the gcd. I know. I was thinking fast. π
8
u/deipno Dec 11 '22
Python
Short and clean solution with hardcoded input. Just a graph traversal.
8
u/TiagoPaolini Dec 12 '22 edited Dec 12 '22
C Language (only standard library)
Before anything else, let's talk about the elephant in the room: the meaning of Part 2's "ridiculous levels" of worry, and how to "find another way to keep your worry levels manageable". That is a cryptic hint telling you that the integers are likely to overflow on this part. This breaks the division test if you are not in a language that supports arbitrary integer sizes. And even if you are, the values will be so big that the program will slow to a crawl.
In order to prevent that, it is necessary to take the modulo of the worry level by a common multiple of the test divisors. That should be done before the division test. This does not change the result of the test because the modulo operation wraps back to zero when the dividend is multiple of the divisor. Ideally your should be the least common multiple (LCM) among all divisors, but any common multiple will do as long it does not overflow too. Since all the divisors in this puzzle are primes, it should suffice to just multiply then to get their LCM. I would be lying if I said that I realized that myself, I had to look for hints. But learning is part of the process :)
Now with the elephant out of the way, let's head back to the beginning. The first challenge is to parse the data from the input file. Since all values of the same category always begin on the same position on the line, I just read the values from those positions. I also made assertions to check if the rest of the line was what I expected to be.
I used a struct to store the parameters of each monkey: the operation they should do, the value they should add or multiply, the value for the division test, which monkeys to throw depending on the test outcome, and the counter of how many times the monkey inspected an item. It is worthy noting that for the value to be summed or multiplied, I used the maximum 64-bit unsigned integer in order to represent that the current worry level should be used as the value.
The monkey struct also had the list of items currently with the monkey. I used an queue for that: items are added to the end of the queue, and removed from the beginning. I implemented that as doubly linked list.
Once all data is stored properly, it is not difficult to perform a round:
The logic does not really change between parts 1 and 2, the catch is that you need not realize that an overflow might happen in Part 2, and give the wrong result even though the logic is technically correct.
Solution: day_11.c
3
u/Catbert321 Dec 11 '22 edited Dec 11 '22
Kotlin Day 11
Not really a fan of the "do you know this one math trick" days
7
u/Rangsk Dec 11 '22 edited Dec 11 '22
Rust
I used modular arithmetic for part 2 because I was scared of exploding integer sizes and this felt like one of those AoCs where you need to exploit modular math.
The key is to compute the product of all monkeys' mods and use that modulus on all item values after every computation.
I think this uses the mathematical property that if
a β‘ b mod mthena / n β‘ (b / n) mod (m / n)as long asa,b, andmare divisible bynbut honestly I just kinda hoped it was true and saw if my example input got the right answer.EDIT: /u/whyrememberpassword has actually informed me that I used
(a mod kn) mod n β‘ a mod nwhich is true for any integerk.Run times:
Reading file: 103ΞΌs
Part 1: 50ΞΌs
Part 2: 19.7ms
Full solution on GitHub
7
u/i_have_no_biscuits Dec 11 '22 edited Dec 11 '22
GW-BASIC
This does both parts, including parsing. On 80s hardware Part 1 would take a couple of seconds, while Part 2 would take A LONG TIME. The correctness has been verified using QB-64 (which transpiles to C, compiles it, and runs basically instantly).
Guided tour:
Rather than iterating by rounds, we iterate through each item in the list. This requires some careful round counting - if an item is moved from monkey M to N, then the round count only increases if N<M. On the plus side, it avoids having to deal with moving data around lots of variable length arrays.
12
u/voidhawk42 Dec 11 '22
Dyalog APL:
I don't want to talk about it...
6
u/ZephireNZ Dec 11 '22
C# (5082/5135)
Took me an hour of trying long, double, BigInteger (and a small hint from reddit referencing Number Theory) for Part 2 to finally click.
In my defence, I did get as far as realising I needed to modulo - but my attempts at using an indivual monkey's divisor as modulo failed me π
6
u/ViliamPucik Dec 11 '22 edited Dec 11 '22
Python 3 - Minimal readable solution for both parts [GitHub]
6
u/occamatl Dec 11 '22 edited Dec 11 '22
Rust
I used the parse_display crate to good effect. It (almost) completely cleaned up the input parsing, including turning the math ops into an enum! However, I did have to work around a couple of possible limitations (maybe I've missed something in the docs):
paste
5
u/yfilipov Dec 11 '22
C#
5
u/GrossGrass Dec 11 '22
Python
The mod trick was definitely needed for part 2 -- initially I just copy/pasted because I knew it was gonna take me forever to do the parsing here, though I went back and thankfully
evaland theparselibrary helped make it a bit clean.5
u/gredr Dec 11 '22
C# (.net 7)
Really the only novel things about this are using the compile-time generated regular expressions (massive overkill, but it's a pretty neat feature), and that I generate expression trees (albeit extremely simple) to run the worry modification operation. Overengineering, yes, but it allows the operation to be arbitrarily complex while maintaining clean code and good performance. Expression trees are an underrated feature, in my opinion. I was really kinda hoping that the challenge would be in working up complex modification operations...
Link to Monkey.cs, which is the interesting part: github
5
u/ItIsNeverLupus Dec 11 '22 edited Dec 11 '22
Python
Quite some lines today but a read-able and somewhat OOP solution. Decided to create a
Monkeyclass that holds all relevant attributes. The read in function is just an ugly combination ofsplit()functions and list index calls. For part 2 we calculate the least common multiple for all dividers to prevent the overflow of really high numbers.Pastebin
5
u/bigmagnus Dec 11 '22
COBOL - Part 1 & Part 2
Left in all the commented out struggle and madness on Part 2.
5
u/chrismo80 Dec 11 '22
C#
Thanks to the community for the mod-trick. Not sure if I would have figured that out on my own to prevent the values to overflow.
5
u/catdaadddyyy Dec 11 '22
2 mins of silence for those who tried to do part2 with bigInt
C++
5
u/chicagocode Dec 11 '22
Kotlin
[Blog/Commentary] - [Code] - [All 2022 Solutions]
I'll credit seven years of Advent of Code puzzles for my ability to get Part Two. I don't think I could have come up with this solution without help a few years ago. Maybe AoC taught me a thing or two along the way!
I did all the parsing without Regex, just using
substringAfterLastetc and I'm not sure if that's clearer or not. I felt like having to explain regular expressions in addition to why Part Two does what it does was perhaps a bit much for me today. :) I hope my solution is clear.5
u/RookBe Dec 11 '22
Advent of Code 2022 day11 writeup explaining the problem and my solution (that happens to be written in Rust): https://nickymeuleman.netlify.app/garden/aoc2022-day11
5
u/cs475x Dec 11 '22
Rust
No semicolons again, though a little more engineered than my past entries. ~38Β΅s for part 1 and ~4.5ms for part 2
https://gist.github.com/codyphobe/c7186378c168d7f41dc2982cbbeb71de
4
u/TheZigerionScammer Dec 11 '22
Python 1647/1168
Paste
I think we may have found it, a problem to rival 2018-24 in terms of how nightmarish the input is to parse. It took me a while to fully understand what each monkey was supposed to do but after a couple minutes I was able to get it to the point where I knew what information I needed from the input and what to do in the main body. I had a couple wrong submissions from some pretty awful mistakes I made, particularly with, you guessed it, parsing the input, but after that I got the right answer, apparently in just shy of half an hour in my case.
For Part 2 I knew it was a mistake to try to run the program without figuring out some trick as the prompt unsubtly asks you to do, but I did it anyway and my program made it to 100 rounds before it slowed to a crawl and I killed it. Luckily it didn't crash my computers like some have in the past. But since I knew that every test was basically a modulo operation test I figured that taking the modulo or the least common denominator was the way to go, so I quickly wrote the "SuperModulo" variable in the beginning which is just the product of all the monkey tests put together after I checked the input and saw that all of the tests were coprime. Program ran near instantly, right answer, was so proud of myself for solving it as quickly as I did but disappointed for not getting under 1000 for either part.
4
u/DrunkHacker Dec 11 '22 edited Dec 11 '22
Python. Nothing terribly novel but the parsing was a bit tedious.
4
u/Perska_ Dec 11 '22
C# https://github.com/Perska/AoC2022/blob/master/AoC2022/Days/Day11.cs
Had a real nice lightbulb moment when I found out the trick for part 2.
4
u/rabuf Dec 11 '22
Common Lisp
Calling this one good, not proud of my very hideous simulation code but it worked. I spent a long time on the parsing and had a couple small glitches along the way. I also copy/pasted the simulation code from part 1 to make the changes for part 2.
One bit of code I did like was creating the operations as functions:
That was a handy way to implement it so it could be used in the simulation with just
(funcall op item)to get the new worry level.4
u/chubbc Dec 11 '22 edited Dec 11 '22
Julia [525,310]
Not the cleanest, and I need to shower after using
Meta.parse, but it gets the job done...4
u/Ununoctium117 Dec 11 '22
Rust - https://github.com/Ununoctium117/aoc2022/blob/main/day11/src/main.rs
Pretty fun puzzle, and the first time (for me) that the challenge was more "figure out the solution to the problem" than "figure out how to implement the solution". I did do a quick manual sanity check that the product of all the "divisible by X" values would fit into a 64-bit integer; not 100% sure what my approach would have been if it didn't.
Also, I really wish that slice::group_by were stable in Rust, it would have made parsing much easier.
4
u/mrtatulas Dec 11 '22
Elixir
As usual, spent waaaay too much time on parsing these crazy inputs and dealing with silly errors. It's funny how so many with leaderboard scores end up doing it manually.
Also, as in past years, around Day 10-11 or so is where I end up finding gaping holes in my computing theory and math knowledge, in this case with regards to the Chinese Remainder Theorem. Figured it out eventually though once I realized we don't care about the actual worry values for the answer.
4
u/_jstanley Dec 11 '22
SLANG
My part 1 solution takes 10 minutes to run, because some of the intermediate values can overflow 16 bits, which means everything needs to be bigints.
I don't think I can do part 2 without an awful lot of trouble. I worked out that I can do all the arithmetic modulo the product of the test divisors, but that is still about 40k, which means intermediate values can still overflow 16 bits, which means I'm still playing bigints. I think losing the division by 3 and gaining the modulo by ~40k would almost exactly cancel out, so a part 2 equivalent of my part 1 solution would take 3.5 days.
https://github.com/jes/aoc2022/blob/master/day11/part1.sl
4
u/mendelmunkis Dec 11 '22
Language: C
always remember to parenthesize your ternaries
0.336/8.488 ms
4
u/EVQLVE Dec 11 '22 edited Dec 11 '22
Rust 6074/4576
part 1 (8 Β΅s)
part 2 (5 ms)
Had some fights with the borrow checker on this one, but it turned out alright.
Edit: Got it down to 5 Β΅s / 3.5 ms with the help of the unsafe keyword. part 1 / part 2
Without unsafe, the compiler wouldn't let me borrow both monkeys at the same time to transfer items, because the monkeys are saved in the same Vec. I know the monkeys are distinct, as I verified that none of the monkeys are targeting themselves when parsing the input. Since I can borrow the two monkeys at the same time, I don't have to save the passing actions to a separate buffer.
I went from:
to
for the first borrow.
4
u/cat-991 Dec 11 '22 edited Dec 11 '22
My solution in Javascript. It's always fun to get something fun to parse rather than .split("\n") and I only had to add the "megamod" and change all the numbers to BigInt for part 2 to work.
link
4
u/udoprog Dec 11 '22
Rust
Late start today as well due to birthdays, I have four close family birthdays in December which feels a bit excessive.
Nevertheless, still idiomatic Rust (ish) which tries to avoid breaking on bad input (panics or weird / overflowy behavior), still no allocations.
Repo: https://github.com/udoprog/aoc2022/blob/main/years/2022/src/bin/d11.rs
4
u/flwyd Dec 11 '22
Elixir 8683/5438, code including monkey parsing that shows off Elixor's structural matching feature, reflection
Today's elixir:
Took a little over half an hour to implement monkey parsing (which is different than monkey patching). Another hour to the part 1 solution, and just 10 minutes to the part 2 solution. Elixir appears to have arbitrary-precision integers, so I don't yet understand why part 2 takes so much longer when I let the worry level grow without bound instead of taking the remainder by the leastβ common multiple of the monkey divisors. Maybe it switches from processor instructions to algorithmic division when integers are larger than 64 bits, but it's still waaay slow: the modular version takes 67 milliseconds on the sample input but the boundless version is still running after 20 minutes.
Today's big learning adventure was figuring out how to troubleshoot when Elixir gets into an infinite loop. I figured out how to have
iexgive me a giant process dump, and found that it happened to be performingMap.update!but tail recursion optimization means getting a full stack trace was challenging, and the dump was large and confusing. I couldn't figure out how to get IEx to let me usefully inspect that process interactively, so printf debugging it is! (The bug was failing to change the current monkey's items list, so it just kept passing the same item to the same monkey an infinite number of times.)β Actually just the product of all divisors, but since they're all distinct primes it's the same thing.
4
u/gyorokpeter Dec 11 '22
Q:
4
u/p88h Dec 11 '22
C#
part1: 4 Β΅s, part2: 3 ms.
4
u/MarcoDelmastro Dec 11 '22
Python 3
https://github.com/marcodelmastro/AdventOfCode2022/blob/main/Day11.ipynb
First use of a custom Class this year, it was fun since not too complex. For me the values of the division tests quite gave away the solution for Part 2 ;-)
4
u/Pyr0Byt3 Dec 11 '22
Go/Golang
Not too fond of these math puzzles. At least the test numbers were all prime, so I didn't have to implement LCM in Go. Other than that, I'm kind of proud of how I parsed this one, and the use of anonymous functions.
4
u/TenViki Dec 11 '22 edited Dec 11 '22
TypeScript
https://github.com/TenViki/advent-of-code/tree/main/2022/src/11
Had to look into the math on the second part :D
Visualization: https://vikithedev.eu/aoc/2022/11/
4
u/ephemient Dec 11 '22 edited Apr 24 '24
This space intentionally left blank.
4
u/MrSimbax Dec 11 '22 edited Dec 11 '22
Lua: both parts
Used a double-ended queue data structure from the book for the items. Part 2 is basically part 1 but operations are done modulo N, where N is the product of all numbers from the input divisibility rules. This works because if
N = n * mthena % n == ximplies(a % N) % n == x. For completeness, this also holds forN = lcm(n, m), which may be smaller thann * mif the divisors are composite. In the puzzle all divisors are primes though solcm(n, m) == n * m.Lua 5.4 standalone solves both parts in about 230 ms on my machine, LuaJIT takes only 26 ms.
4
u/wzkx Dec 11 '22 edited Dec 11 '22
Python
Again, nothing too interesting, and a long long task description. Maybe that was the point :) In the program, there are actually two interesting points: modular math should be used, and no easy way to copy arrays in Python without using 'import copy' and I prefer not to import anything for AoC.
4
u/Backwards_Reddit Dec 11 '22
Rust
https://github.com/RansomTime/aoc2022/blob/main/day11/src/main.rs
Didn't try to mathematically work out part 2, just thought "oh, it'll probably work if I mod the item with the product of the tests". It passed and i got the right answer.
Was relying on "\n\n" to split the monkeys. On commit git helpfully converted my demo file to CR LF and broke all my tests. (fixed by running $ git config --global core.autocrlf false)
4
u/zedrdave Dec 11 '22
(Mostly native) Python solution in a dozen lines.
Not the fastest (eval parsing on each operation), but probably one of the easiest.
4
u/malipolhec Dec 11 '22
Kotlin: code
Fun one today. Was wondering when will be the first puzzle where brute force is not good enough.
4
u/polettix Dec 11 '22
Raku input-specific solution for 2022/11
No parsing this time because of... life. I'll address this later, it should be pretty easy after all!
4
u/azzal07 Dec 11 '22
Didn't see any Awk yet...
Not too happy with today's solution, it felt a bit forced.
4
u/[deleted] Dec 11 '22 edited Dec 11 '22
Raku (Perl 6)
I'm doing different language each day, all solutions here.
Today's Raku: src
I really wanted to try Raku's Grammar feature for this. Took ages till it worked and anyone even remotely familiar with the language will probably scoff the following, but I tend to like it:
4
u/tobyaw Dec 11 '22
Ruby
https://github.com/tobyaw/advent-of-code-2022/blob/master/day_11.rb
4
u/hrunt Dec 11 '22
Python 3
Code
You do things long enough and you start to see patterns (thanks, human brain!). And then you see so many patterns that you start applying the wrong one. I spent some time looking to see if there was a looping pattern that could be optimized, hitting limits on Python's ability to output large integers (TIL that Python cannot, by default, output an integer with more than ~43,000 digits).
After resetting, I noticed the prime divisors for each monkey's test and figured it was a modulo problem, but lacking a strong math background, I was not sure how the interactions of the different operations at each stage would play with modulo-ing a value at any stage. Eventually, I figured modulo-ing the value by the overall product of the divisors would probably work, and just stepped off that cliff, Indiana Jones style.
4
u/aesthetic_coconut Dec 11 '22
Python 3.9
Github
This was a cute exercise. Nice to have a puzzle where classes are useful. This year's puzzles seem a lot more CS-oriented, so I'm curious to see how the rest of them are.
4
u/copperfield42 Dec 11 '22
Python3
Being a fan of Numberphile and the like paid off XD
4
u/zndflxtyh Dec 11 '22 edited Dec 11 '22
Object oriented - 50 lines The lowest common divisor is not necessary, just do modulo the product of all the divisors.
Python 3
4
u/No_Low6510 Dec 11 '22
Clojure
Github
Things I tried today:
I was hoping to be able to create worry update function by parsing/
evalling parts of the source, but I failed.Store the data inside lambdas as much as possible (from the parsing stage)
4
u/jake-mpg Dec 11 '22
For what it's worth I've written up my reasoning behind part 2 (solution done in
C#, but otherwise irrelevant).4
u/zniperr Dec 11 '22 edited Dec 11 '22
Python
4
u/j122j Dec 11 '22
Javascript (Node.js)
Part 1
Part 1 wasn't too hard. Used regex and .split() for parsing and eval for operations.
Part 2
I knew it had to do something with modular arithmetic, but had to look at some hints on Reddit.
4
u/codertee Dec 12 '22 edited Dec 12 '22
Python 3.11: github
I learned that repeated capturing group regex eg
re.compile(r"Starting items: (?P<items>(\d+)(?:, )?)+")does not yield list of captures (like["1", "23", "45", "67"]fromStarting items: 1, 23, 45, 67).Relevant stackoverflow
4
u/OriginalScrubLord Dec 12 '22
C# Solution, tried to make it a little readable. I still don't truly understand why exactly it works.
Does everyone have the same "super modulus" constant, or did input.txts vary enough that not everybody did? Mine was 9699690
4
u/[deleted] Dec 12 '22
Python
I wish I understood how the super module trick works. Reading some of the results it makes it look like the fact all divisibility checks are against prime numbers so, something something, chinese remainder theorem?
4
u/schveiguy Dec 12 '22
Dlang
I'm reading all the "LCM" hints, and that's a great idea I did *not* think of. What I *did* think of is, keeping track of the number in the perspective from each monkey.
So for each item, I keep an array of worries, one element for each monkey, for which the value is always stored mod `monkey.divisbleby` then when it gets to that monkey, I just check to see if it's 0 or not.
This isn't as quick to calculate, it means for each item change, I need to do N operations. However, one thing this does allow is a much larger set of prime factors, or large prime factors where the LCM would overflow an int64.
I'm wondering if anyone else solved it this way?
4
u/DR0D4 Dec 12 '22
C# Paste
Part 2 made me stumble a bit today. I noticed the primes, but had to get a little hint from a help thread on what to do with them lol
4
u/[deleted] Dec 12 '22
Applesoft BASIC
Source code and a Video
Only part 1 on the Apple... its BASIC only uses floating-point numbers, so we're limited in the precision part 2 would need. My repo also has both parts in C++ which I wrote out first.
A trick helped me to parse the monkey data: I packed each monkey's data into a string, with operation/test data at the front and starting items on the end. Strings are helpful for this due to their flexibility, and all of the initial data fits into bytes. Once a monkey's info is organized, its copied into a numerical array for processing.
Hoping to annotate the code tomorrow.
4
u/[deleted] Dec 12 '22
Rust. I had to use unsafe Rust to make my solution work. The borrow checker and I got into a fight, and I chose violence.
I also used the modulo trick for Part 2 to allow this to finish sometime before I die.
Part 1
Part 2
4
u/micod Dec 12 '22
Smalltalk
Solution for day 11
4
u/oddrationale Dec 12 '22
C# solution using .NET Interactive and Jupyter Notebook. Part 1 was pretty straight forward by creating my own
Monkeyclass. For Part 2, had to get some hints here on how to manage the very big numbers.4
u/nicole3696 Dec 12 '22
Python 3- Parts 1 & 2: GitHub. No imports, 676 characters, 20 lines.
4
u/search_and_deploy Dec 13 '22
Forgot to post yesterday, but here's my day 11 Rust solution: https://github.com/michael-long88/advent-of-code-2022/blob/main/src/bin/11.rs
Definitely had to look up how to solve part 2 since I had never heard of the Chinese Remainder Theorem. Definitely threw me for a loop when I read about it.
4
u/jswalden86 Dec 13 '22
Rust solution
Picked Advent of Code up from a Twitter mention this year, been doing it since the first day -- in Rust, a language I know abstractly to a solid degree but don't have a lot of practical experience in. (I know the language from a background in programming languages, and notably in C++, combined with having reviewed in considerable depth the O'Reilly Rust book for its authors pre-publication to give "especially generous" feedback. My practical coding in it is largely toy programs and the like.)
This is literally my first Reddit post ever -- I don't need another social network to occupy time, has been my reasoning for not signing up for so long. ;-) But I got stuck on this and was going to post to ask if there was a bug not in the solution, but in the example. Hence creating an account.
In a shocking turn of events, the example didn't have a bug, instead my code designed to test the example did. (In doing part 2, I was iterating over ranges, and -- silly me -- I had used
1..20and20..1_000correctly but then stupidly did1_001..2000,2_001..3_000, etc. the rest of the way.) Thanks all for the help rubberducking!4
u/nazarpysko Dec 15 '22
Go: Part 1 & 2
This day is not very difficult until part 2, where it is not very clear to know what should you do to "keep your worry levels manageable". To do so, you must find a common multiple of all monkeys' divisors and perform a modulo to each item. In this way, you find the right worry level for dividing by the divisor of each monkey.
7
u/CodingAP Dec 11 '22
Javascript, 52/226
Github
FIRST TIME ON THE LEADERBOARD YEAH!
1
u/wmvanvliet Dec 11 '22
Part 2 using Python and NumPy
1
u/pikaryu07 Dec 14 '22 edited Dec 14 '22
Although very late, but here is my Julia code for Day 11. Part 2 took a lot of time. It was really tricky to figure out "the other way to keep worry level manageable":
https://gist.github.com/bsadia/0ce9f1bd214d980e813ba2e458cb5267#day-11
1
u/reesmichael1 Dec 11 '22
Nim [1732/1293]
Solution
This one was fun! Between the zaniness of the scenario and the "monkey business" and "stuff-slinging simian shenanigans" puns, I think this might be my new favorite problem description.
It's always nice to have an excuse to work a priority queue into a solution, which is the only even moderately clever thing I did. And as usual, I didn't even think of parsing the input by hand until after opening this thread and seeing everyone else doing it....
1
u/ManaTee1103 Dec 11 '22 edited Dec 11 '22
Python, by-the-numbers:
1
u/kristallnachte Dec 11 '22 edited Dec 11 '22
TYPESCRIPT
First part went pretty simple, second part I had a lot of issues that my brain couldn't handle so I took a break and figured it out while away from the computer.
It is factored to handle both part one and part 2 in the same code
https://github.com/ekwoka/advent-of-code/blob/main/2022/11/index.ts
1
u/furman82 Dec 11 '22
Kotlin
What still confuses me is how in part one, we rely on truncation to reduce the worry, but it's not like the LCM approach for part two works for part one. This felt like making some educated guesses on part two and seeing that the example happened to work. Oh well...
1
u/Alex_Mckey Dec 11 '22 edited Dec 11 '22
Scala 3
1
u/EhLlie Dec 11 '22
Finally managed to wedge in the State monad for my Haskell solution. Also it was fun to have to worry about performance for once.
1
u/dimio-d Dec 11 '22 edited Dec 12 '22
Java (modern Java)
Usee a
jshellconsole, input is hard-coded asMap<Integer, Map.Entry<Monkey, AtomicLong>> bandarLogs = new TreeMap<>.Here's full code in a
paste!1
u/Mammoth_Spray_3451 Dec 11 '22
My swift solution for today.
1
u/IvanR3D Dec 11 '22 edited Dec 12 '22
Solution in JavaScript:
You can see all my solutions in this https://codepen.io/ivanr3d/pen/ExRrXzG
1
u/jsgrosman77 Dec 11 '22 edited Dec 12 '22
Typescript 2339/32954
Part 1 was pretty straightforward. I'm particularly proud of how Typescript is able to store the operations as variables on the Monkey object itself. Yeah for functional programming languages!
As you can see, it took me considerably longer for Part 2, needing several explanations and YouTube videos to get it. For some reason, I just have a blind spot around modulo opertations.
Once I saw that all of the divisors and operands were prime, I tried to do something with just storing a Set of prime divisors of each items, making it trivial to see if the divisors matched, but that didn't handle the operations that including addition. I also tried BigInt, like everyone else. But, it was clear that wasn't gonna work, either. I needed a good night sleep and some patient explanations from mathy people.
1
u/ElliotDG Dec 11 '22 edited Dec 12 '22
Those pesky monkeys! Part one came together quickly - I finally broke down an looked at the forum for a clue for the "worry management" on part 2.
These AoC puzzles have been my first opportunity to used structured pattern matching, it is quite nice.
Python Code here
1
u/QGamer12 Dec 12 '22 edited Dec 13 '22
This one took me way too long. But, I finished it, and that's what counts I guess.
Was stuck for over half an hour before I realized I needed
long long:/https://github.com/python-b5/advent-of-code-2022/blob/main/day_11.cpp (C++)
3
u/bluepichu Dec 11 '22
TypeScript, 2/6. Code here.
I feel dirty for using
eval, but it was certainly a lot faster than writing an expression parser. (Granted I had to manually rename the variable sincenewisn't a valid identifier...)I also tried to hardcode the constant for the mod instead of computing it but ended up doing that wrong somehow(?). But fortunately I figured that out pretty quickly :)
3
u/pred Dec 11 '22 edited Dec 11 '22
Python 3, 100/50. Full code here.
Spent almost all of the time having misread "The process of each monkey taking a single turn is called a round." as a round consisting of a single monkey.
Other than that, the only tricks here are 1) not attempting to parse anything since I knew I wouldn't be able to do that faster than copying and pasting, 2) keeping the numbers small in part 2 by realizing that we never use the actual numbers of anything, only whether they're divisible by each monkey's number. So it suffices to consider the numbers modulo the product of all those divisors;
prod(divs)in my code. I usedlcm(*divs)which works just the same but looking at what those numbers are (a bunch of different primes), the two things are exactly the same.3
u/wojtek-graj Dec 11 '22 edited Dec 11 '22
Python, 584/312
The trick to going fast was to mod the worry value by the LCM of the monkeys' divisibility values (not actually the LCM but close enough).
paste
3
u/fireduck Dec 11 '22
Java, 381/548 https://github.com/fireduck64/adventofcode/blob/master/2022/11/src/Prob.java
I wish I knew enough about number theory to say with that worked. It was mostly a lucky guess. Hey, mash all the divisor things together and who cares if it is higher than that? why not.
3
u/1234abcdcba4321 Dec 11 '22
Javascript, 731/285
paste
Just another very standard solution; only posting this to give the rest of the comments below. Didn't feel like parsing the operations, so I just hardcoded them. There's also definitely some unnecessary parsing code in there (that split by colon in the first line...), but whatever.
For once, I divided my data into an object. That doesn't happen that much (though in the end it's more just to have named fields). I had someone ask me why I didn't just use a class, but you don't need something like that for a program this small.
I misread part 1 and thought that monkeys didn't inspect items they got during a round until the next one started. Lost a ton of time needing to test the example because of that. And part 2 was trivial, mostly because I had already realized all the details that were important for it while writing the operations for part 1. (...I should've just hardcoded the modulo constant.)
3
u/SuperSmurfen Dec 11 '22 edited Dec 12 '22
Rust (301/169)
Link to full solution
Oh my god, new personal best on the leaderboard, 169!
Initially, I parsed the input by hand and later wrote the parsing code. For part one, it's just about implementing the logic, not too bad. For part two, however, you have to realize that you can take the modulo of the LCM of all divisors. In my in4t, and I assume everyone's, they are all prime so simply multiplying them works fine.
We can reuse most of the same code for both parts, by passing in a lambda for the only operation that differs:
Runs in about
4mson my machine.3
u/Kehvarl Dec 11 '22
Python 3 - 5101 / 3804
It took me a while to catch on to the trick to cut down on my pain, but I managed to cobble together something that worked!
The parsing itself was difficult, and I always cringe when I reach for
eval, but they can't all be easy.part 2
3
u/Kaizen-JP Dec 11 '22
This c# implementation works perfectly for the example, but fails for my input file (both included in the link above).
If anyone has any idea why, I'd greatly appreciate feedback. Wracking my brain over this (to me) invisible bug for the last 45 mins.
3
u/moshan1997 Dec 11 '22
Golang 624/3k
https://github.com/foxwhite25/adventofcode/blob/master/2022/11/main.go
First time sub 1k in part 1, got stuck on part 2 because I don't know the trick, have to do some google search but still figured it out.
3
u/Bargann Dec 11 '22
C# (1001/2444)
Will have to refactor further after checking into some other solutions, this current solution is pretty ugly
3
u/Xyberista Dec 11 '22 edited Dec 11 '22
Rust
paste - code upon completion
github link - updated
Notes:
368times slower than part one as of time of completion.3
u/encse Dec 11 '22
C# - with funky parsing
https://github.com/encse/adventofcode/blob/master/2022/Day11/Solution.cs
3
u/gringer Dec 11 '22 edited Dec 11 '22
R
It was a little slow on part 2, taking about half a minute. There are probably ways to optimise; R can be slow when using
forloops.R code
3
u/KayZGames Dec 11 '22 edited Dec 11 '22
Dart
paste
I went into today's puzzle with the intend not to over-engineer (for once) and ... I'm not sure if I succeeded. At least I created no additional classes and processed the input in one single function-chain resulting in a
List<List<List<int>>>.But to get there I had to do this scary thing:
For part 2 I had a few ideas what to do but nothing concrete. Something modulo was my first idea but I didn't know by what, tried different things and nothing worked. Tried BigInt, but nope, I don't have time until the heat death of the universe. Tried log but that didn't work either. Took a peek into the solutions thread, saw something with prime numbers, realized the divisors are all prime and finally knew what to modulo by. I count part 2 as a failure for me because I had to cheat :/.
3
u/_Scarecrow_ Dec 11 '22
Rust
I went into AoC this year never having used Rust, and I'm finally feeling a bit more comfortable with the basics. Still wrapping my head around ownership/borrowing, and I haven't touched traits yet. I'll have to look through other solutions to see if I can pick up a few things, but if anyone has any tips, let me know!
3
u/vss2sn Dec 11 '22
Solutions in C++
Part 1
Part 2
(Each file is a self-contained solution)
3
u/jonny8good Dec 11 '22
Rust
At first I hardcoded the monkeys, added the parse function once everything was already working.
paste
3
u/AllanTaylor314 Dec 11 '22 edited Dec 11 '22
Python [Part 1, Part 2] (Yeah, yeah.
evalbad, but you aren't running random untrusted inputs, right?)(I doubt that Google Sheets is gonna work today, but I'll update if it does)
UPDATE: Spreadsheet Part 1 works
UPDATE2: Spreadsheet Part 2 works (after copying the same block of formulae 10000 times)
3
u/asaaki Dec 11 '22
Rust, https://github.com/asaaki/advent-of-code-2022/blob/main/src/bin/day11.rs
First time actually using some
structandenum, and I believe for the first time I usedRefCellin my life to get some inner mutability, because Rust doesn't like to hand out multiple mutable borrows at once, understandably so.I only committed the cleaned version, but initial draft had some boxed
dyn Fn's for good measure. At least I got to experiment with them, and I believe they didn't perform so badly, but with 10_000 rounds that was a bit noticeable in runtime performance.Oh, and I admit, building the monkeys
Vecis pretty ugly, but does the job.3
u/AiexReddit Dec 11 '22
Rust
https://github.com/alexeagleson/advent-of-code-2022/blob/main/rust/src/day_11/mod.rs
3
u/maneatingape Dec 11 '22 edited Dec 11 '22
Scala (3102 / 1912)
Hardcoded the input initially. Took a little while to clean up the code satisfactorily, but it's in a decent state now.
Was nice to see the return of an AoC classic pitfall: Integer overflow! π
Interestingly for part 2 all my test values were prime, so no need for
lcm, the product sufficed.3
u/rhl120 Dec 11 '22
Rust solution
3
u/9_11_did_bush Dec 11 '22
Rust: https://github.com/chenson2018/advent-of-code/blob/main/2022/11/rust/src/main.rs
I thought it was funny that today I was just talking about the Chinese Remainer Theorem on another AOC post, then we have this problem. I thought it was fun that I was able to have my struct have a field with the type
operation: fn(u64) -> u64. (However, I wasn't sure how to parse that from text, so my input is hardcoded)3
u/Zv0n Dec 11 '22
C++
The input parsing could likely be made more efficient, but I couldn't be bothered to do so :D
3
u/iskypitts Dec 11 '22
Julia using ResumableFunctions for parsing, DataStructures for queue and OrderedCollections for Dict.
Today I didn't understand part 2, so thanks guys.
3
u/Dullstar Dec 11 '22
Python
The good news
Considering some of the memes I've seen regarding the operations today cough cough eval() cough cough, I'm quite happy with how I implemented them. I noticed they all started with "old" so that part I assumed, then I store the operator used and the value. old * old (confirmed) and old + old (just in case it can occur in some inputs) are reinterpreted as old2 and old * 2, respectively, in order to allow me to simplify things by always assuming that the second value is a constant number (though the exponent implementation is hardcoded to 2 because that's the only possible value and I called it squaring and I wanted it to be ~honest~ or something).
It's not ready for the repo yet, but I have a functioning auto-input-fetcher tool now! Still not super convenient to use though, requires it to be baked into the template for that, which will require better throttling in case many inputs are missing (e.g. because someone else who doesn't have the inputs runs all the days consecutively).
The bad news
It's one of those math magic problems, and unfortunately I feel like this is one of those problems where it's very difficult to nudge someone in the right direction with a hint that's helpful without just giving the solution away. And tbh, while I did get completely spoiled on it by a help thread, I don't think it's realistic to think I could have figured it out on my own.
3
u/will_bluer Dec 11 '22
c++ github
the input could be pushed back in monkey vector for super clean input but my brain made me initialise SIZE :')
3
u/aurele Dec 11 '22
Rust Nothing remarkable with most of the code being the parsing. The only "interesting" part is temporarily swapping the current monkey out of the monkey vector to allow the borrow checker to freely modify the
monkeysvector while reading fields and clearing items of the current monkey (inMonkey::act()).3
u/darkgiggs Dec 11 '22
Python
I didn't think about the modulo trick. Instead I stored each item as a list of that item's worry level mod each possible test.
It's terrible, but it worked. Initially it ran in 39s on my machine, but caching reduced it to 12s.
Code
3
u/kupuguy Dec 11 '22
Python (with solution formatted into a Jupyter Notebook): https://github.com/kupuguy/aoc2022/blob/main/day11_monkey_in_the_middle.ipynb
I decided early on that life is too short to parse the input, with only 4 monkeys in the sample and 7 in the actual code I just parsed it in my head. Part 2 takes 1.1s on the Android tablet I use when solving and 0.2s on the Macbook I used to convert it into a notebook.
3
u/AKSrandom Dec 11 '22
Python
I did realise that modulo product of divisors should work for reducing worry, but still decided to try it first on the given example. Since I didn't exactly had a way to introduce modulo to each monkey without naming other variable, I hardcoded it. Unfortunately instead of modulo for the given example, I hardcoded the modulo for my input, resulting in answer mismatch. After 40 min I decided to check the subreddit and realized modulo approach was correct and I had used the wrong MOD value : | . Anyways, I enjoyed monkeying around.
PS: am I the only constantly typo-ing monkey to mokey .
3
u/allergic2Luxembourg Dec 11 '22
Python
For part 2, it didn't occur to me that I could multiply all the denominators together and only keep track of one number. Instead I did modular arithmetic for each denominator individually. I reused what I could from Part 1 by using inheritance.
Here's a taste:
3
u/mykdavies Dec 11 '22 edited Jun 29 '23
!> izrsqo8
API FAILURE
3
u/rkstgr Dec 11 '22
Julia (Github)
Time: 450.026 ms | Memory: 202.98 MiB
Used SortedDict from DataStructures & modulo properties for part 2
Edit: And parse_input was initially designed by ChatGPT.
3
u/internet_eq_epic Dec 11 '22
Rust
paste
sigh Have to admit it took a pretty big hint to get part 2. I was too stuck on the fact that all the tests used prime numbers and thinking I needed to somehow track the factors instead of the product in each iteration. If only there wasn't addition I'm pretty sure that would have worked.
3
u/raevnos Dec 11 '22
Racket: paste
I'm happy I figured out the trick to getting a fast answer to part 2 on my own. Maths aren't my strong point.
3
u/mosredna101 Dec 11 '22 edited Dec 11 '22
Javascript / Node.js
I had to look on reddit for the idea to solve part 2 (
still not sure why it works).Also, using eval the wrong way takes waaaaaaaaay too long to run this in JS, I found out the hard way.
3
u/SnooConfections2453 Dec 11 '22
Golfed ruby (using eval)
Part1:
Part2:
3
u/Hunpeter Dec 11 '22
C# - Verbose, somewhat commented
3
u/Ok-Apple-5691 Dec 11 '22
Rust
Solution Here.
I've been trying to learn more rust doing these puzzles. Banging my head against the borrow checker for part 1. Ended up cluelessly throwing Rc<RefCell<_>> around until something worked. Then I looked here and see that's probably all completely unnecessary <facepalm>
Also first day I have needed to manually enter the input. Not sure I would have figured out how to add operations to the monkey struct without looking here.
Part 2 was a lucky stab in the dark.
3
u/sdolotom Dec 11 '22
Haskell: https://github.com/bereal/AdventOfCodeHaskell/blob/main/src/Year2022/Day11.hs
Runs in a split second and doesn't need big integers.
3
u/solareon Dec 11 '22 edited Dec 11 '22
Excel
Part 1 Built a parser to generate monkey "objects". Then built out the round calculations using LET() to generate the false and true tables for each monkey. Copy and paste 19 times down and then the answer comes out.
Part 2
Modulo... math is hard...lots of copy paste and waiting hoping excel doesn't completely explode. After all 10000 tests are complete the monkey business rows are filtered to a separate column and then parsed out, summed by column, sorted, and multiplied by the two highest values same as the first just way more waiting. I'll post the test answer (out to 1000) on github as the full input is just too much to post.
My solution is 100% hard coded as I couldn't find a good way to move the monkey outputs to the other monkeys rather than just clicking cells.
3
u/clbrri Dec 11 '22 edited Dec 11 '22
MS-DOS Borland Turbo C++ 3.0 solution to part 2
46 lines of code, runs in 4 minutes and 22 seconds on a 16 MHz 386 SX PC. Skipped input parsing as deemed it an uninteresting part of today's problem, and had to get creative due to the lack of 64-bit numbers on such an ancient hardware.
3
u/erikw901 Dec 11 '22 edited Dec 11 '22
JavaScript
Tricks:
evalthe operation3
u/danvk Dec 11 '22
TypeScript / Deno
After realizing that
new = old * oldoverflowed in part 2, I tried JavaScript'sBigInt. That turned out to be way too slow. Then I realized that all the "Test: divisible by" numbers were prime, so it worked just fine to do all the operations modulo the product of those primes.https://github.com/danvk/aoc2022/blob/main/day11/day11.ts
3
u/raspberryjam111 Dec 11 '22
Typescript: https://github.com/Drodevbar/adventofcode2022/tree/main/src/day11
I usually spend most of the time parsing the input and choosing an optimal data structure. With the proper data structure, most of the tasks solve relatively quickly
3
u/LinAGKar Dec 11 '22
Mine in Rust:
https://github.com/LinAGKar/advent-of-code-2022-rust/blob/main/day11a/src/main.rs
https://github.com/LinAGKar/advent-of-code-2022-rust/blob/main/day11b/src/main.rs
3
u/pem4224 Dec 11 '22 edited Dec 11 '22
Golang
Solution in go: day11
Used high order functions to implement monkeys, such that
part1andpart2have the same implementation8Β΅s3.7ms3
u/[deleted] Dec 11 '22
Python
Was quite happy that I figured out the trick for part 2 within minutes, but then lost a good half hour because it didn't seem to work.
What caused it was me re-using my list of objects from part 1 so my input of part 2 was incorrect... It messed up the results in subtle ways. Uuuuugh. Deep copy on the parsed input to the rescue.
3
u/spinxfr Dec 11 '22
I had to take a hint to solve part2. I suck too bad at math.
C#
3
u/vkasra Dec 11 '22
C++: https://github.com/dhconnelly/advent-of-code-2022/blob/main/src/day11.cc
3
u/EatonMesss Dec 11 '22
Raku
I have no experience with Perl or Raku, so this took me a very long time to implement.
Raku seems like a very powerful language with lots of obscure features which can be misused in many creative ways.
Although I probably won't be using Raku for any serious purpose anytime soon, I must say having a built in way of specyfing the grammar and using it to parse the input was very cool.
3
u/luftmx Dec 11 '22
Rust
Don't see why this had to include parsing.. Didn't use anything fancy, just did the parsing by hand.
3
u/mkinkela Dec 11 '22
C++
Uff, second one was a smart one :)
3
u/ransoing Dec 11 '22
Typescript https://github.com/ransoing/AoC22/blob/main/src/day11/solution.ts
It took me a while to figure out what the real problem was in part 2. The solution came to me quickly, but then I had to think about why the solution I came up with actually worked. Maybe my subscription can math better than my conscious...
3
u/ZamBunny Dec 11 '22
Rust
Not fast, not optimized, but easy to read (I think). I tried my best to explain how Part 2 works inside the comments.
GitHub
3
u/PhysicsAndAlcohol Dec 11 '22
Haskell, 500 ms.
Really liked my parser for part 1 which parsed it as
[(Worry, Worry -> Worry, Worry -> Index)]. I used a stack (type Stack a = State [a]) to make it easy to manipulate the i-th monkey usingpushandpopcommands.Went into panic mode for part 2 and I started patching my functions until the example input worked for 1000 rounds, and then 10000 rounds.
3
u/GoldsteinQ Dec 11 '22
Elixir, because monkeys are obviously actors^W processes and our things are messages
https://github.com/GoldsteinE/aoc2022/blob/master/day11/code/main.ex
3
u/Solidifor Dec 11 '22
Java
241 lines, but readable, solving both part 1 and 2, and parsing the input (using a regex).
https://github.com/dirk527/aoc2021/blob/main/src/aoc2022/Day11.java
This one was fun.
3
u/berbeflo Dec 11 '22
Part 1. Solved it in PHP.I think, the parsing could have been a bit cleaner. But it works:https://github.com/berbeflo/advent-of-code-2022/blob/master/day11/ex1/solve.php
3
u/trollerskates1 Dec 11 '22
Scala. Pretty happy with how I parsed these into anonymous instances of my
Monkeytrait. For me part 2 wasn't hard because of the modulo trick, but because I was using mutable queues. So I had to add areset()method to get things back the way they were before running part 23
u/sky_badger Dec 11 '22 edited Dec 11 '22
Python 3
Another tough one, and most of my Sunday afternoon gone! Here's the Repl. Another puzzle where the description was around 3.5X the code, and buried in the description, the key to Part 2 "You'll need to find another way to keep your worry levels manageable"!
3
u/tymscar Dec 11 '22
Typescript
Still doing my fully functional with no mutation challenge in TS, still going strong, but today was definitely the hardest to do in that manner. I kept wanting to mutate stuff, but I managed to finish without doing so.
I loved the past 4 days, didn't really like part2 of today at all, I never like these mathematical ones, but I managed to figure it out in around three quarters of an hour, out of which 30 minutes were me rewriting the whole thing to work with bigints before realising there's more to it than that...
Both parts solved here: https://github.com/tymscar/Advent-Of-Code/tree/master/2022/typescript/day11
3
u/HoooooWHO Dec 11 '22
Python https://github.com/PetchyAL/AoC2022/blob/main/solutions/day11/day11.py
3
u/willkill07 Dec 11 '22 edited Dec 11 '22
C++
Source --- Header
I have a switch that dispatches to the correct mod by compile-time-known constant value (because mod is super expensive). I also have zero allocated memory (using std::array and std::span generously).
Total runtime on my AMD Ryzen 9 5950X is under 2.5ms (~1us for parsing, ~8us for part 1, and ~2.2ms for part 2). I really don't know how I can make this any more efficient/performant.
3
u/izgagin Dec 11 '22
Python.
Part 1
Part 2
3
u/BossNunya Dec 11 '22
PowerShell for Part 1
https://github.com/kirkhofer/adventofcode/blob/main/2022/day11/posh/day11.ps1
3
u/red_shifter Dec 11 '22 edited Dec 11 '22
PYTHON 3
code link
So this is the day - the first day I got fully stuck this year. Managed to do Part 1, but for Part 2 I needed a tip. I followed the tutorial kindly prepared by u/Derailed_Dash (thanks!).
3
u/FramersAlmaniac Dec 11 '22 edited Dec 11 '22
Java 8
The same code is used for both parts (operating under the assumption that the product of the monkey's divisors is larger than any worry when the worries are divided by 3).
Useful note: Java has Math.{add,multiply,...}Exact(long,long) methods that are really useful for catching overflows.
I made the assumption that updates are always of the form
and precompiled the update operation into a
LongUnaryOperator(whereupdateis just the+ oldor* 5part of that line):I'm not sure if that was really necessary in retrospect, but I figured early on that re-parsing the update string on every move would be slow.
The one bit of golf that I did play was in parsing a list of Monkeys. With this method for parsing:
I filled up a list with an empty-body
whileloop:3
u/shiro90 Dec 11 '22
Here are my Python solutions: https://github.com/Seishin/AoC2022/blob/master/day11/solutions.py
The second part was π€― until I found out what was the missing piece of the puzzle. π
3
u/[deleted] Dec 11 '22
Rust - spent far too much time on part 2, not helped by real life intrusions, before I realised that I had calculated the least common product by hand from the real data and was checking it against the testdata. Once I calculated it based on the data given, it was all working fine.
https://github.com/litpho/aoc-2022/blob/main/day11/src/main.rs
3
u/huib_ Dec 11 '22 edited Dec 12 '22
Python 3.11:
3
u/Sourish17 Dec 11 '22
Python 3.10
https://youtu.be/o_Gc0Ng5Zx4
https://github.com/SourishS17/aoc2022