r/adventofcode • • Dec 10 '20

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

Advent of Code 2020: Gettin' Crafty With It

  • 12 days remaining until the submission deadline on December 22 at 23:59 EST
  • Full details and rules are in the Submissions Megathread

--- Day 10: Adapter Array ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

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:08:42, megathread unlocked!

69 Upvotes

1.1k comments sorted by

View all comments

4

u/jsut_ Dec 11 '20

Perl for part 2

This seems far simpler than what a lot of people did.

use 5.18.4;
use strict;
use File::Slurp;

my @lines = read_file('input');
@lines = sort {$a <=> $b} @lines;
@lines = map { chomp $_; $_ } sort {$a <=> $b} @lines;
push @lines, $lines[-1] + 3;

my %routes = (0 => 1);

foreach my $i (@lines) {
    $routes{$i} = $routes{$i-1} + $routes{$i-2} + $routes{$i-3};
    say "$i: $routes{$i}"
}

2

u/Loonis Dec 11 '20

A lot of people don't know this algorithm/method :) That's the great thing about AoC, I can come here and learn new ways to solve problems.

I took the graph walking approach, so my mind has been blown by the number of people who knew how to complete this challenge by adding some numbers.

1

u/[deleted] Dec 11 '20

[removed] β€” view removed comment

1

u/jsut_ Dec 11 '20

I was actually going to build the tree but when i sat down to do it and thought about what I needed as I iterated through the list this approach presented itself.

1

u/[deleted] Dec 11 '20

[removed] β€” view removed comment

2

u/jsut_ Dec 11 '20

It’s been a really time since I’ve written code to make a tree, and I don’t think I’ve ever done it in Perl. I think that phased me. When I thought about the base case and how to build up from there the whole β€œoh, it’s just the sum of the ways you can get to the three before” thing occurred to me.