aboutsummaryrefslogtreecommitdiff
path: root/challenge-098/dave-jacoby/perl/ch-1b.pl
blob: fb06d584c49e22a53f5cf17ff3c8d9a00d505ff4 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env perl

use strict;
use warnings;
use feature qw{ say signatures state };
no warnings qw{ experimental };

use List::Util qw{ sum0 };

use JSON;
my $json = JSON->new->pretty->canonical;

my $n      = 4;
my $flag   = 0;
my @inputs = qw{ twinkie input1.txt input2.txt };
while ( $flag < @inputs ) {
    state $flags;
    for my $input (@inputs) {
        my $output = readN( $input, $n );
        do {
            $flags->{$input} = 1;
            $flag = sum0 values %$flags;
            next;
        } if $output eq '';
        say qq{\t'$input'\t$n\t'$output'};
    }
}
exit;

# returns empty string on failure cases, which include
#   * no file
#   * index beyond file length

sub readN ( $file, $chars ) {
    state $fhs;
    return '' unless -f $file;
    return '' unless -r $file;
    unless ( $fhs->{$file} ) { open $fhs->{$file}, '<', $file }

    my $fh = $fhs->{$file};
    my $output ;
    read $fh, $output, $chars;
    return $output;
}