Skip to content

Instantly share code, notes, and snippets.

@Cheezmeister
Last active December 10, 2017 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cheezmeister/e53143b5a6dd4898844b5f5c87d9d730 to your computer and use it in GitHub Desktop.
Save Cheezmeister/e53143b5a6dd4898844b5f5c87d9d730 to your computer and use it in GitHub Desktop.

#!liberate perl (Huh?)

Git Gud Ante: Upped Speed: Ludicrous Askalski: NOOOOO

These are my solutions to 2017's Advent of Code puzzles! They're all here! Last year, I did most of the problems in Elixir, a mostly-new language for me. This year, I'm using Perl, a mostly-new language for me! I'm using Perl for two reasons:

  • It's what AoC itself is built in[citation needed]: More meta for your feta!
  • I've always wanted to wield the sacred Swiss-army chainsaw
    • ...but I can't be arsed to actually put in the practice time

But not just any Perl. No, this Perl is literate! My code is embedded in a Markdown document, in the spirit of the literate flavor of Coffeescript (RIP). Now, Topaz may tell you that Markdown is an abomination and that you should use Nimble instead because it sucks less. And he may be right. Or not. I don't know. I'm just, like, a programmer, man.

What you are reading is (technically) a source file. Depending on where and how you are reading it, it may look quite pretty. GitHub is quite good at making source files look pretty. But even if you are looking at the raw plaintext (as I do when writing) it will still be quite legible. In fact, it looks like this:

Literate Perl in Vim

Ain't that schwifty? If you think this is neat, you may wish to peruse some other bits like it:

If you are impatient, you can jump to a given day's solution

Otherwise, let's jump in and sling some semicolons.

Cargo all the cults

You're supposed to do this in Perl. I don't know exactly why, but it doesn't hurt, so.

use warnings;
use strict;

Be Modern

use v5.26;

Imports

use Switch;
use List::Util qw/reduce min max uniq first/;
use File::Slurp qw/read_file/;
use Getopt::Long qw/GetOptions/;

Helpers

Usage

sub usage {
}

Debug

$DEBUG is very sophisticated. Not really, but it is undeniably useful, so.

my $DEBUG = 0;

sub dprint {
  print @_ if $DEBUG;
}

sub dsay {
  say @_ if $DEBUG;
}

Load Input

Grab a given day's input with input($day). Maybe some day we'll auto-fetch it?

sub input {
  my $arg = shift;
  my $input = read_file("input.$arg.txt") or return "pants";
  dprint "<INPUT>\n$input</INPUT>\n";
  return $input;
}

Combine Parts 1 and 2

Shield your eyes.

sub compound_solve {
  my @a = @_;
  return sub {
    my $i = 1;
    while (my $ref = shift @a) {
      print "\nPart $i\n"; ++$i;
      print "-------\n";
      $ref->();
    }
  }
}

To Jagged Array

Make an array of arrays out of a bunch of text. Lots, if not most, inputs have been 2d grids of stuff, usually numbers. They are usually rectangular but it's best not to assume so.

sub to_jagged_array {
  my @lines = split '\n', shift;
  my @ary = map {[split /\s/]} @lines;
  return @ary;
}

Gridwise Operations

Sum the 8 adjacent squares on a grid. Handles undef as if it were 0

sub sum8 {
  my ($x, $y, %grid) = @_;
  my $sum = ($grid{$x-1}{$y  } or 0) +
            ($grid{$x-1}{$y+1} or 0) +
            ($grid{$x-1}{$y-1} or 0) +
            ($grid{$x  }{$y+1} or 0) +
            ($grid{$x  }{$y-1} or 0) +
            ($grid{$x+1}{$y  } or 0) +
            ($grid{$x+1}{$y+1} or 0) +
            ($grid{$x+1}{$y-1} or 0);

return $sum;
}

Sum the 4 immediately adjacent squares on a grid (no diagonals).

sub sum4 {
  my ($x, $y, %grid) = @_;
  my $sum = ($grid{$x-1}{$y  } or 0) +
            ($grid{$x  }{$y+1} or 0) +
            ($grid{$x  }{$y-1} or 0) +
            ($grid{$x+1}{$y  } or 0);

return $sum;
}

Sum the 4 diagonally adjacent squares on a grid.

sub sum4x {
  my ($x, $y, %grid) = @_;
  my $sum = ($grid{$x-1}{$y+1} or 0) +
            ($grid{$x-1}{$y-1} or 0) +
            ($grid{$x+1}{$y+1} or 0) +
            ($grid{$x+1}{$y-1} or 0);
}

Check for Duplicates

Check an array for duplicates. Return 1 iff there is a duplicate value.

sub has_dupe {
  return (scalar (@_) != scalar (uniq @_));
}

Similarly, check an array for strings which are anagrams of one another. This is perhaps not a great general-use helper. It will live here for now. Just in case.

sub has_anagram {
  my (@ary) = @_;
  while (my $q = shift @ary) {
    foreach my $p (@ary) {
      dprint "$p/$q\n" and return 1 if 
       (join '', sort { $a cmp $b } split(//, $p)) eq
                (join '', sort { $a cmp $b } split(//, $q));
    }
  }
  return 0;
}

SOLUTIONS

It's all downhill from here, folks. day1 through dayN will take input (hopefully from input.N.txt), do something with it, and print stuff. Hopefully the right answer.

Day 1

Yeah it's butt-ugly. No, I'm not sorry. I'm learning!

sub day1 {
  my $sum = 0;
  my $input = input(1);
  for (my $i = 0; $i < length($input); ++$i) {
    $sum += +(substr($input, $i, 1)) if substr($input, $i, 1) eq substr($input, ($i+(2132/2))%2132, 1);
  }
  print "Sum: $sum\n";
}

Day 2

Still not sorry.

sub day2 {
  my $input = input(2);
  my @lines = split('\n', $input);
  my $checksum = 0;

  foreach my $line (@lines) {
    my $hi = 0;
    my $lo = 99999;
    my @nums = split(/\s/, $line);
    $hi = max(@nums);
    $lo = min(@nums);


    foreach $a (@nums) {
      foreach $b (@nums) {
        if ($a % $b == 0 && $a != $b) {
          $checksum += $a / $b ;
          dprint "($a/$b)"
        }
      }
    }

    dprint $line . ":: $checksum\n";

  }

  print "Checksum is $checksum\n";
}

Day 3

#sorrynotsorry

n.b. See also: Ulam Spiral

I quickly noticed the pattern 1, 9, 25 in the example: Bottom-right corners of each layer are the squares of odd numbers. This gives us an anchor that we can calculate manhattan distances relative to. I ended a sentence with a preposition, but that's okay, because Perl is not English.

The idea is simple: find the largest odd-square n^2 less than our input. That number has a manhattan distance of 1 + (n - 1) / 2. Then we can use the remainder, along with the number of sides it "wraps" around, to figure out its own manhattan distance.

As evidenced from the mess below, I had a harder time wrapping my head around the exact mechanics of it all. sic erat scriptum. Not pictured is a bunch of mental math and calculator checks.

Fortunately, my input wasn't just larger than an even square, because then I'd be in trouble. Perhaps that was intentional. Either way, I took advantage of it to avoid extra odd/even logic.

# Where side == 6 because sqrt(25)+1
# 25 => 2+2 = 4
# 26 => 2+2+1 = 5
# 27 => 2+2+1 - 1 = 4
# 28 => 2+2+1 - 2 = 3
# 29 => 2+2+1 - 1 = 4
# 30 => 2+2+1 = 5
# 31 => ... = 6
# 32 => ... = 5
# 33 = 4
# 34 = 3
# 35 = 4
# 36 = 5
# 37 = 6
sub day3_part1 {
  my $input = input(3);
  my $sqrt = int(sqrt($input));
  my $corner = $sqrt * $sqrt;
  my $side = $sqrt+1;
  my $side2 = $side / 2;
  my $wrap = $input - $corner;
  my $rem = $wrap % $side;
  my $spillover = abs($rem - $side2);
  my $wat = $side2 + $spillover;
  print "Corner: $corner\n";
  print "sqrt: $sqrt\n";
  print "side: $side\n";
  print "side/2: $side2\n";
  print "wrap: $wrap\n";
  print "rem $rem\n";
  print "spillover: $spillover\n";
  print "side / 2 + spillover: $wat:\n";
}

Part 2

This was the first day where my solution for part 2 was completely different than that for part 1. Since the spiral is now defined in a dynamic way, I just implemented it like that, rather than trying to be clever and formulaic.

It turns out that this exact sequence is in fact A Thing! The world is a remarkable place.

In any case, I wasn't wise enough to refer to OEIS, so just hacked out the following.

The key insights are:

  • Square spirals, like Ogres, have layers
  • Each layer is a square of side layer * 2, so the layers are sized thusly:
  • If you squint really hard, it looks like a cake

The Layers, Duke

Layer Side Grid Formed
0 1 Single Cell
1 2 9-grid
2 4 25-grid (5x5)
3 6 49-grid (7x7)
4 8 ...and so on
sub day3 {
  my $input = input(3);
  my %grid = ();
  my $seed = 1;
  $grid{0}{0} = $seed;
  my $layer = 1;
  while (1) {
    my $side = $layer * 2;
    my $anchorX = $layer;
    my $anchorY = $layer;
    my $x = $anchorX;
    my $y = $anchorY;
    for (my $i = 0; $i < 4; ++$i) {
      for (my $j = 0; $j < $side; ++$j) {
        if ($i == 0) {
          --$y;
        }
        elsif ($i == 1) {
          --$x;
        }
        elsif ($i == 2) {
          ++$y;
        }
        else {
          ++$x;
        }
        my $num = sum8($x, $y, %grid);
        dprint "[Layer = $layer, Side = $side, aX = $anchorX, aY = $anchorY] ($x,$y): $num\n";
        $grid{$x}{$y} = $num;
        if ($num > $input) {
          print "Num: $num\n";
          exit;
        }
      }
    }

    ++$layer;
  }
}

I have a feeling we'll be seeing more square spirals in the coming days, so I intend to clean this business up a bit to make it reusable at the drop of a hat. But not now.

Day 4

Okay, this is actually not terrible. It's not good, but...not terrible.

sub day4_part1 {
  my $input = input(4);
  my @lines = to_jagged_array($input);
  my $valid = 0;
  foreach my $l (@lines) {
    next if (has_dupe(@$l));
    ++$valid;
  }
  say $valid;
}

Part 2

Copypasta at its finest. Copy-filet-mignon, if you will.

sub day4_part2 {
  my $input = input(4);
  my @lines = to_jagged_array($input);
  my $valid = 0;
  foreach my $l (@lines) {
    next if (has_anagram(@$l));
    ++$valid;
  }
  say $valid;
}

Day 5

Another mercifully short solve. Part 2 was a trivial if-else addition I won't even bother recording.

Things that slowed me down on day 5:

  • Forgetting a sigil
  • Forgetting a semicolon
  • Perl's amazingly-helpful† error messages
  • Perl uses last instead of break
  • Mispasting my correct part 2 answer (always end with a newline, kids)
  • 60sec penalty due to mis-pasting my correct part 2 answer
  • Wondering what was wrong with the solution that produced my correct part 2 answer
    sub day5 {
      my $input = input(5);
      my @nums = split("\n", $input);
      my $p = 0;
      my $i = 0;
      while (1) {
        $p += $nums[$p]++;
        $i++;
        if ($p >= scalar @nums or $p < 0) {
          last;
        }
      }
      print $i;
    }

† (Except not)

Day 6

I actually slept through the unlock (I was very tired) so I solved this one at a leisurely pace (the better to learn effectively, my dear). I've no idea how I would have placed, but I am guessing "not well" because List::Util#first is one of Perl's many "foolish human, you thought I would do that, but instead I do this" functions.

I assumed, as any reasonable human would, that first { $banks[$_] == max(@banks) } @banks would give the right result. It does not. It does not even give the wrong result. Instead, it errors out and results in my $i uninitialized. The right way to do it is first { $banks[$_] == max(@banks) } 0..$#banks.

It would have been faster to find the "first max" by hand, but I didn't know that when I decided to use the built-in thing. Hindsight's 20-20.

sub day6 {

Set up initial state.

  my $input = input(6);
  my @banks = split /\s+/, $input;

  my %seen = ();

  my $cycles = 0;

Condense @banks into a string in order to hash seen states.

  until (exists($seen{join ',', @banks})) {
    $seen{join ',', @banks} = 1;

    say "Banks: @banks";

    my $i = first { $banks[$_] == max(@banks) } 0..$#banks;
    my $val = $banks[$i];
    $banks[$i] = 0;
    while ($val--) {
      ++$i;
      $i %= scalar @banks;
      $banks[$i]++;
    }
    ++$cycles;

Part 2 was fun because (a) it asked what any inquisitive mind would naturally ask halfway through solving part 1, namely, "how does this cycle?"; and (b) I used a clever™ hack to get it slightly faster.

Instead of factoring out the contents of this loop into a subroutine, as any good programmer would, or copy-pasting it for a second go-round, as any bad programmer would...

    say "     => @banks";

I added this line to get the ending (repeat) state, ran it again on my initial input, pasted that into my input, then ran it once more. I suppose this makes me a badong programmer. No, I'm definitely not sorry.

  }

  say "Cycles taken: $cycles";
}

Day 7

Part 1 was not so bad once I actually parsed the input correctly. This is gnarly, but the basic idea is simply to index each program's name into its parent. Then we can walk upward (downward?) from any leaf node (I used the first, why not) until we arrive at the root.

sub day7 {
  my $input = input(7);
  my @lines = split("\n", $input);

  my @subs = ();

  my @names = ();


  my %tree = ();
  
  foreach my $line (@lines) {
    my @parts = split '->', $line;
    my $left = $parts[0];

    my ($name, $weight) = split ' ', $left;

    if ($parts[1]) {
      my $right = $parts[1];
      my @subprogs = split ', ', $right;
      foreach my $sub (@subprogs) {
        $tree{$sub} = $name;
      }
    }

    unshift @names, $name;

  }

  my $something = $names[0];
  $something = $tree{$something} while exists $tree{$something};

  print "Base: $something\n";
  
}

Part 2 is gnarly. The weights matter. I think recursion is not mandatory, but I used it, because I'm not upping the ante that much So we build bottom-up, but build weights top-down then traverse bottom-up to find the unbalanced node. ¯\_(ツ)_/¯

sub day7_part2 {
  my $input = input(7);
  my @lines = split("\n", $input);

  my %tree = ();

  our %weights = ();
  our %children = ();
  my @leaves = ();

  foreach my $line (@lines) {
    my @parts = split '->', $line;
    my $left = $parts[0];

    $left =~ /(\w+) \((\d+)\)/;

    my ($name, $weight) = ($1, $2);
    dprint "Name: $name, weight: $weight\n";

    $weights{$name} = $weight;

    if ($parts[1]) {
      my $right = $parts[1];
      my @subprogs = split ', ', trim($right);
      printf "Subs '%s'\n", join '/', @subprogs;
      foreach my $sub (@subprogs) {
        $tree{$sub} = $name;
      }
      @{ $children{$name} } = @subprogs; # https://stackoverflow.com/a/12535442
    }
    else {
      unshift @leaves, $name;
    }

  }

  sub recurse {
    my $root = shift;
    my $depth = shift;
    return $weights{$root} unless exists $children{$root};

    my @kids = @{ $children{$root} };
    dprint "Kids: " . join(',', @kids) . "\n";
    my @kidweights = map { recurse($_, $depth) } @kids;
    if (scalar(uniq @kidweights) > 1) {
      my $indent = ' ' * $depth;
      say "$indent Found unbalanced node $root with kids @kids, weights @kidweights";
    }
    return sum(@kidweights) + $weights{$root};
  };

  print recurse('dtacyn', 0);
}

Day 8

Not too terrible. This solution covers both parts, and is more or less as-written.

In hindsight, a regex destructuring was not really necessary, and actually slowed me down as I had to balance parens, and accidentally a term.

Instead, I should have gone something like my ($reg, $op, $num, $ignored, $test, $condition, $compare) = split ' ', $line.

I'm very glad eval was at my disposal. I know that it's evil, but think of the poor Java programmers who had to construct some janky-ass switch statement, after reading the entire input to make sure they didn't miss an operator. Sometimes when there's a job needs doing, it's okay to make a deal with the devil.

sub day8 {
  my $input = input(8);
  our %regs = ();
  my $fullmax = 0;

  foreach my $line (split "\n", $input) {
    $line =~ /(\w+) (inc|dec) (\S+) if (\w+) (.*)/;
    my ($reg, $op, $num, $test, $condition) = ($1, $2, $3, $4, $5);
    if (eval('($regs{$test} // 0) ' . $condition)) {
      $regs{$reg} += $num if ($op eq 'inc');
      $regs{$reg} -= $num if ($op eq 'dec');
    }

    $fullmax = max(values %regs, $fullmax);
  }

  printf "Historical Max: %s\n", $fullmax;
  printf "Last Max: %s\n", max(values %regs);

}

Day 9

(Or, how I learned to stop worrying and locate my missing semicolon?)

I'll be honest, I was expecting to spend hours debugging a real humdinger like days 3 and 7. This puzzle was really not bad at all, despite the mess below, which, again, I will not be cleaning up because that's quot erat scriptum, and also I have no shame.

What was quite bad, indeed, and cost me 30 sproinking minutes of debugging what was probably a correct solution from the get-go (and debugging the bugs I introduced with my diagnostics), was the fact that my input got truncated to 4096 chars out of the 15-thousand-odd in my actual input. This is either a limitation of the MinTTY console, or cat, I'm not sure which. Either way I'm seriously considering wiring up some fancy input-fetching business for future problems, but I really don't wanna.

sub day9 {
  our $input = input(9);

In any case, my misadventures lead to defining impl just so that I could more easily verify my code against the examples, as seen below. Part 2 (the $trashman local) is included.

  sub impl {
    my $arg = shift;
    my @chars = split //, $arg;
    my $depth = 0;
    my $score = 0;
    my $trashman = 0;

As soon as I started reading, I expected some sort of state machine funny business. In fact, all that's needed here is a true/false garbage flag. sic.

    my $state = 'CLEAN';

Originally started simply shift @chars. The indexed loop was added just for diagnostics.

    for (my $i = 0; $i < scalar @chars; ++$i) {
      my $c = $chars[$i];
      if ($c eq '!') {
        dprint "Skipping $i and next\n";
        ++$i;
        next;
      }
      if ($c eq '<') {
        ++$trashman unless $state eq 'CLEAN';
        $state = 'GARB';
        next;
      }
      if ($c eq '>') {
        $state = 'CLEAN';
      }
      if ($state eq 'GARB') {
        ++$trashman;
        next;
      } # Gotcha

      dprint "[$i][$c]: ";
      if ($c eq '{') {
        $depth++;
        dprint "Depth increased to $depth\n";
      }
      if ($c eq '}') {
        $score += $depth;
        dprint "Score upped by $depth to $score\n";
        $depth--;
      }
      dprint "\n";

    }
    say "Score: $score";
    say "Garbage: $trashman";
  }

   #impl '<>' ; #, 0 characters.
   #impl '<random characters>' ; #  17 characters.
   #impl '<<<<>' ; # , 3 characters.
   #impl '<{!>}>' ; # , 2 characters.
   #impl '<!!>' ; # , 0 characters.
   #impl '<!!!>>' ; # , 0 characters.
   #impl '<{o"i!a,<{i<a>' ; # , 10 characters.

   impl $input;

}

Day 10

Another brutal one. I don't really want to talk about it.

sub day10_part1 {
  my $skipsize = 0;
  my $pos = 0;
  my $input = input(10);
  chomp $input;
  my @lengths = split ',', $input;
  my @list = (0..255);
  foreach my $l (@lengths) {
    dsay "Position $pos, Skipsize $skipsize, Length $l:";
    for (my $i = 0; $i < $l / 2; ++$i) {
      my $p = ($pos + $i) % scalar @list;
      my $q = ($pos + ($l - 1 - $i)) % scalar @list;
      dsay "  Swapping items $p and $q";
      my $temp = $list[$q];
      $list[$q] = $list[$p];
      $list[$p] = $temp;
    }

    $pos += $l + $skipsize++;
    $pos %= scalar @list;
    dsay join ',', @list;
  }
  my $a = $list[0] * $list[1];
  say "Answer: $a";

}

I'll just be over here, crying softly to myself, drowning my sorrows in sencha.

sub day10 {
  my $input = input(10);
  chomp $input;
  my @lengths = map { ord($_) } split('', $input);


  my @list = (0..255);
  my $skipsize = 0;
  my $pos = 0;
  push @lengths, (17, 31, 73, 47, 23);
  dsay "Lengths: @lengths";
  foreach my $round (1..64) {
    foreach my $l (@lengths) {
      dsay "Length $l, position $pos, Skipsize $skipsize";
      for (my $i = 0; $i < $l / 2; ++$i) {
        my $p = ($pos + $i) % scalar @list;
        my $q = ($pos + ($l - 1 - $i)) % scalar @list;
        my $temp = $list[$q];
        $list[$q] = $list[$p];
        $list[$p] = $temp;
      }

      $pos += $l + $skipsize++;
      $pos %= scalar @list;
    }
  dsay "List is @list";
  }


  my @hashes = ();
  foreach my $i (map { $_ * 16} 0..15) {
    my @sublist = @list[$i .. $i+15];
    dprint "Block $i is [@sublist]";
    my $xor = reduce { $a ^ $b } @sublist;
    dsay ": $xor";
    push @hashes, $xor;
  }
  say join ',', @hashes;
  say join '', map { sprintf "%02x", $_ } @hashes;

}

Entry Point

I could do something clever when we run ./aoc.md.pl. But I won't.

unless (scalar @ARGV) {
  print "Usage: $0 day <day>\n" and exit;
}

Grab option flags. If I were a better programmer, I would make it so these did not have to come first; sometimes you just want to tack on -d to the thing you just ran, and have it just do the thing. But I am not a better programmer. So I won't.

/u/exploding_cat_wizard points out that we can use Getopt::Long with with the following one-liner to just do the thing. Hooray!

GetOptions('debug!' => \$DEBUG);
 #while ($ARGV[0] =~ /^-(\w)/) {
 #  my $flag = shift;
 #  $DEBUG = 'true' if $1 eq 'd';
 #  dprint "Got flag $flag\n";
 #}

Right before solving day 3 I learned that subrefs are a thing, and they look funny but they totally work! It's an array of functions! Or references to them, anyhow.

my @solutions = (
  sub { print "I'm in ur index sploiting ur off-by-ones\n" },
  \&day1,
  \&day2,
  compound_solve(\&day3_part1, \&day3),
  compound_solve(\&day4_part1, \&day4_part2),
  \&day5,
  \&day6,
  compound_solve(\&day7, \&day7_part2),
  \&day8,
  \&day9,
  compound_solve(\&day10_part1, \&day10),
);

day is the only "command" supported here. It is mostly for ceremony and clarity. There is, though, the convenient side-effect of the else block below...

if (shift @ARGV eq 'day') {
  my $daynum = shift @ARGV;
  if (exists($solutions[$daynum])) {
    $solutions[$daynum]();
  }
  else {
    print "No solution for day $daynum\n"
  }
}

...which allows me to type e.g. ./aoc.pl.md pants to run sloppy one-off sanity checks and such.

else {
  # scratchpad
}

We Need To Go Deeper

Inline::Python;

use Inline Python => "DATA";

doIt();

__END__

(Intermission)

__DATA__

print("Well hallo thar")

__Python__
    def doIt:
      print("Well hallo thar")
    print("Well hallo thar")

Fin~

I would just to say, blender.stackexchange is pretty neat. Wow. The internet is a beautiful place, sometimes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment