Created
April 13, 2021 19:35
-
-
Save mcsf/cc43a799890730e9be482b9d486ecc91 to your computer and use it in GitHub Desktop.
Messing with Perl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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