Skip to content

Instantly share code, notes, and snippets.

@Cheezmeister
Last active December 3, 2017 03:38
Show Gist options
  • Save Cheezmeister/dda6aafd964afdd8d3251cbc5fa5ac88 to your computer and use it in GitHub Desktop.
Save Cheezmeister/dda6aafd964afdd8d3251cbc5fa5ac88 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use warnings;
use strict;
# IMPORTS
#
use Switch;
use List::Util qw[min max];
# HELPERS
#
my $DEBUG = 0;
sub dprint {
print @_ if $DEBUG;
}
sub input {
my $arg = shift;
my $input = slurp("input.$arg.txt") or return "pants";
dprint "<INPUT>\n$input</INPUT>";
return $input;
}
sub slurp {
open my $fh, '<', shift or return undef;
$/ = undef;
my $content = <$fh>;
$/ = "\n";
close $fh;
return $content;
}
sub to_jagged_array {
my @lines = split '\n', shift;
my @ary = map {[split /\s/]} @lines;
return @ary;
}
# SOLUTIONS
#
# Day 1 solution. Yeah it's butt-ugly. No, I'm not sorry.
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 solution. Still not sorry.
sub day2 {
my $input = input(2);
my @lines = split('\n', $input);
# my $out = join ',', @lines;
# dprint "Lines: $out\n";
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 solution. TBD.
sub day3 {
my $input = <<HERE;
This is a test input
It has some lines
And some words.
HERE
foreach my $line (to_jagged_array($input)) {
foreach my $el (@$line) {
print "($el)";
}
print "--\n";
}
}
# ENTRY
#
while ($ARGV[0] =~ /^-(\w)/) {
my $flag = shift;
$DEBUG = 'true' if $1 eq 'd';
dprint "Got flag $flag\n";
}
my @solutions = (
sub { print "I'm in ur index sploiting ur off-by-ones\n" },
\&day1,
\&day2,
\&day3,
\&day4,
\&day5,
\&day6,
\&day7,
\&day8,
\&day9,
);
if (shift @ARGV eq 'day') {
my $daynum = shift @ARGV;
if (exists($solutions[$daynum])) {
$solutions[$daynum]();
}
else {
print "No solution for day $daynum\n"
}
}
else {
foreach my $line (to_jagged_array input(2)) {
print join ',', @$line;
print "!!\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment