-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs.pl
executable file
·77 lines (52 loc) · 1.74 KB
/
jobs.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env perl
use strict;
use warnings;
use 5.10.0;
my %jobs;
chomp(my @lines = <STDIN>);
my @jobs = map {
my($id, $class, @prerequisites) = split /, /;
@prerequisites = map {
s/\*/8/g;
my($job, $level) = $_ =~ /(.+) (\d)/;
[$job, int $level]
} @prerequisites;
$jobs{ $class } = { map { @$_ } @prerequisites };
for (@prerequisites) {
my($job, $level) = @$_;
my %more_prerequisites = (%{ $jobs{ $job } }, $job, $level);
while (my($k, $v) = each %more_prerequisites) {
${ $jobs{ $class } }{ $k } = $v unless exists ${ $jobs{ $class } }{ $k } and ${ $jobs{ $class } }{ $k } > $v;
}
}
my $value_name = uc $class =~ s/ /_/r;
my $object_name = $class =~ s/ //r;
[$id, $class, $value_name, $object_name]
} @lines;
use Data::Dumper; print Dumper \%jobs;
my $longest_name_length = do {
my $champion = (sort { length $b->[2] <=> length $a->[2] } @jobs)[0][2];
length($champion);
};
for (sort { $a->[0] <=> $b->[0] } @jobs) {
say sprintf "val %-${longest_name_length}s = %s", $_->[2], $_->[0];
}
for (sort { $a->[0] <=> $b->[0] } @jobs) {
say sprintf "%-${longest_name_length}s -> 0,", $_->[2];
}
say join ",\n", map { $_->[3] } @jobs;
say join ", ", map { qq!"$_->[3]"! } @jobs;
for (sort { $a->[0] <=> $b->[0] } @jobs) {
say sprintf "%-${longest_name_length}s -> 0,", $_->[3];
}
for my $job (sort { $a->[0] <=> $b->[0] } @jobs) {
my $class_name = $job->[1];
my @prerequisites = keys %{ $jobs{$class_name} };
my $tuples = join ', ', map {
my $object_name = s/ //r;
my $value = $jobs{$class_name}{$_};
"$object_name -> $value"
} @prerequisites;
my $map = @prerequisites ? "Map($tuples)" : "Map[JobClass, Int]()";
say sprintf "%-${longest_name_length}s -> $map,", $job->[3];
}