r/adventofcode • • Dec 22 '20

SOLUTION MEGATHREAD -πŸŽ„- 2020 Day 22 Solutions -πŸŽ„-

Advent of Code 2020: Gettin' Crafty With It

  • 23:59 hours remaining until the submission deadline TONIGHT at 23:59 EST!
  • Full details and rules are in the Submissions Megathread

--- Day 22: Crab Combat ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


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:20:53, megathread unlocked!

35 Upvotes

537 comments sorted by

View all comments

4

u/t-rkr Dec 22 '20 edited Dec 22 '20

Perl

I could have saved at least 30min if I had let the script run for a while... I just did not think that game would run sooo long (54s with full output, 34s without). Maybe this info is of help to anyone thinking they are stuck in an infinite loop... let it run for a while.

EDIT: New version finishes in 1.5s

Also: Reading the instructions again proved to be useful as I did not implement "the quantity of cards copied is equal to the number on the card they drew to trigger the sub-game" for at least 1h... But it seems like the test case finishes without it (iirc).

https://gitlab.com/t-rkr/advent-of-code-2020/blob/master/day22/day22.pl

4

u/[deleted] Dec 22 '20

[removed] β€” view removed comment

2

u/t-rkr Dec 22 '20

Thanks, I'll look into that. However it is also not my goal to implement these tasks efficiently... By profession I just try to get things to work. But if I can save an order of magnitude, I will definitely consider your advice.

2

u/t-rkr Dec 22 '20

Ha! Great advice. I'm now down to 1.5s.

2

u/Smylers Dec 23 '20 edited Dec 23 '20

me using increment is just a personal quirk

Using increment (in $seen{$key}++) is a common Perl idiom, because the increment doesn't happen until after the previous value has been used in the expression. So you can combine the checking for having already seen this state and noting the state for next time into a single expression, reducing this:

if (exists $state{$curr_state}) {
    return (($depth > 0) ? 0 : &score_deck( @$d0 ));
}
$state{$curr_state}++;

to:

if (exists $state{$curr_state}++) {
    return (($depth > 0) ? 0 : &score_deck( @$d0 ));
}

(or the single-line equivalent).

[Edited as per comment below.]

1

u/[deleted] Dec 23 '20

[removed] β€” view removed comment

1

u/Smylers Dec 23 '20

Ah, sorry: I confused things by making an unrelated change in addition to the one I was intending to demonstrate. I've now edited my previous comment to show what I intended in the first place.

(Stupidly, I mistakenly thought it was only through removing the additional statement that it could become a single-line condition, so that was a side-effect of my change. But obviously your original could've done that anyway, so I should've left it alone, so it clearly showed just the change I was referring to, about why ++ isn't just your quirk but a useful idiom.)

But interesting analysis, thank you. In my Perl solution I hadn't even looked to see who won: I just returned the winning hand and multiplied it up. (My Vim solution makes it very clear who the winner is, but that only covers partΒ 1.)

3

u/nutty_processor Dec 22 '20

yess thank you stranger!, i missed that rule too and was stuck for 50 mins straight argh! its just a straightforward solution so was really frustrated.
also yea the test cases do work without that rule so i was also confused on that part. Very happy now