-
Notifications
You must be signed in to change notification settings - Fork 0
/
x06.pl
32 lines (28 loc) · 1003 Bytes
/
x06.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/perl
# Advent of Code 2016 Day 6 - complete solution, alternate map version
# Problem link: http://adventofcode.com/2016/day/6
# Discussion: http://gerikson.com/blog/comp/Advent-of-Code-2016.html#d06
# License: http://gerikson.com/files/AoC2016/UNLICENSE
###########################################################
use 5.016;
use warnings;
use autodie;
#### INIT - load input data into array
my $testing = 0;
my @input;
my $file = $testing ? 'test.txt' : 'input.txt';
{
open( my $fh, '<', "$file" );
while (<$fh>) { chomp; s/\r//gm; push @input, $_; }
}
### CODE
my $part = 1;
my $data;
my $sortings = { 1 => sub { $data->[$_]->{$a} <=> $data->[$_]->{$b} },
2 => sub { $data->[$_]->{$b} <=> $data->[$_]->{$a} }, };
foreach my $line (@input) {
my @chars = split( //, $line );
map { $data->[$_]->{ $chars[$_] }++ } ( 0 .. $#chars );
}
say join( '', map { ( sort { &{ $sortings->{$part} } }
keys %{ $data->[$_] } )[-1] } ( 0 .. $#{$data} ) );