r/adventofcode • • Dec 02 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 02 Solutions -🎄-

--- Day 2: Password Philosophy ---


Advent of Code 2020: Gettin' Crafty With It


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 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:02:31, megathread unlocked!

99 Upvotes

1.2k comments sorted by

View all comments

2

u/__Abigail__ Dec 02 '20

Perl

I wrote a blog post about the solution.

#!/opt/perl/bin/perl

use 5.032;

use strict;
use warnings;
no  warnings 'syntax';

use experimental 'signatures';
use experimental 'lexical_subs';

my $input = shift // "input";

my $valid1 = 0;
my $valid2 = 0;

open my $fh, "<", $input or die "open: $!";
while (<$fh>) {
    chomp;
    #
    # Parse a line of input.
    #
    /^(?<min>      [0-9]+) -
      (?<max>      [0-9]+)   \s+
      (?<letter>   [a-z])  : \s+
      (?<password> [a-z]+)   \s*$/x or die "Failed to parse '$_'";   

    #
    # Save values from %+ before they're overwritten
    #
    my ($min, $max, $letter, $password) = @+ {qw [min max letter password]};

    #
    # Count the number of times the letter appears in the password.
    # Ideally, we use tr///, but tr/// doesn't do interpolation.
    # We could use an eval to get around that, but we opt to use an m//g
    # instead.
    #
    # Note that we first assign the result of the match to an empty list,
    # to force the match to occur in list context. We're not putting anything
    # in the list, but we do want to know the number of items which would
    # have been put in the list -- which is exactly what list assignment
    # in scalar context returns.
    #
    my $count = () = $password =~ /$letter/g;
    $valid1 ++ if $min <= $count && $count <= $max;

    #
    # Count the number of times if the given letter appears on exactly
    # one of the given positions: xor will return true if one, but not
    # both, of its operands is true.
    # We also check whether the password is long enough -- although with
    # the given input, this isn't really required.
    #
    $valid2 ++ if (length ($password) >= $min &&
                   substr ($password,    $min - 1, 1) eq $letter)
              xor (length ($password) >= $max &&
                   substr ($password,    $max - 1, 1) eq $letter);

}

say "Solution 1: ", $valid1;
say "Solution 2: ", $valid2;
     
__END__

1

u/daggerdragon Dec 02 '20

This code is really hard to read on old.reddit. Could you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple ~? Note that if you're using the visual editor, you may have to "Switch to Markdown" to get Reddit to understand the formatting properly.