Advent of code 2020 solutions in Wolfram language

DOWNLOAD-DESKTOP-NOTEBOOK.nb (199 KB)

11 Likes

news-congrads-kkluyshnik-02-04-19.jpg – you have earned Featured Contributor Badge 2015-11-02_10-07-11.png

Your exceptional post has been selected for our editorial column Staff Picks http://wolfr.am/StaffPicks and Your Profile is now distinguished by a Featured Contributor Badge and is displayed on the Featured Contributor Board. Thank you!

Another nice way of doing problem 1 is to use KnapsackSolve, which is a generalization of the subset-sum problem. For part 1.2, this ends up being quite a bit faster too.

Times@@Pick[nums, KnapsackSolve[({#, 1, 1} &)/@nums, {2020, 3}], 1]
3 Likes

That is a nice solution. And it would have been the required approach if they pushed the input size much beyond 200 numbers.

1 Like

My solution to Q1 used pattern-matching:

input /. {___,x_,___,y_,___,z_,___} /; x+y+z == 2020 :> x y z

which easily coped with the given input data.

1 Like

And for Q7, one can do nice visualizations (and analysis) using Graph operations such as Subgraph and GraphUnion, and options, such as GraphHighlight and VertexLabels. And the close integration of such functionality is, to me, what sets the Wolfram Language apart from the other programmatic solutions.

1 Like