Skip to content

Instantly share code, notes, and snippets.

@mcsf
Created April 13, 2021 19:35
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 mcsf/cc43a799890730e9be482b9d486ecc91 to your computer and use it in GitHub Desktop.
Save mcsf/cc43a799890730e9be482b9d486ecc91 to your computer and use it in GitHub Desktop.
Messing with Perl
#!/usr/bin/perl -l
use strict;
use warnings;
# Solution to https://adventofcode.com/2020/day/5
my @seats = ();
# Collect seat IDs by parsing binary sequences
while (<>) {
chomp;
tr/FLBR/0011/;
push @seats, oct("0b$_");
}
@seats = sort {$a<=>$b} @seats;
# Find highest seat ID
print $seats[$#seats];
# Find missing seat ID
my $prev = 0;
foreach (@seats) {
last if ($prev && ($_ - $prev > 1));
$prev = $_;
}
print $prev + 1;
#!/bin/bash
# For comparison, the equivalent solution as a shell script stitching
# together Unix primitives: `tr` to translate letters into binary
# digits; `bc` for processing arithmetic instructions to convert binary
# to decimal; `sort -g` to perform a numeric sort; `awk` to find the
# maximum (PART ONE) and to find the gap in the sequence (PART TWO).
# PART ONE
< input \
tr FLBR 0011 | cat <(echo ibase=2;) - | bc | \
awk '$1>m{m=$1;l=$0}END{print l}'
# PART TWO
< input \
tr FLBR 0011 | cat <(echo ibase=2;) - | bc | \
sort -g | \
awk '{ if (prev && ($1 != prev + 1)) print $1 - 1; prev = $1 }'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment