Group Abstract Group Abstract

Message Boards Message Boards

11
|
17.8K Views
|
5 Replies
|
17 Total Likes
View groups...
Share
Share this post:

Advent of code 2020 solutions in Wolfram language

Posted 5 years ago

Attachments:
POSTED BY: Michael Hale
5 Replies
POSTED BY: Paul Abbott

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.

POSTED BY: Paul Abbott
Posted 5 years ago

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

POSTED BY: Michael Hale
Posted 5 years ago

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]
POSTED BY: Tom M
POSTED BY: EDITORIAL BOARD
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard