aboutsummaryrefslogtreecommitdiff
path: root/challenge-128/arne-sommer/raku/minimum-platforms
blob: 9aafcfec5fb99f7a3787d3b9bbe3ed630fb0a634 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#! /usr/bin/env raku

subset HHMM where $_ ~~ /^(<[012]>)\d\:<[012345]>\d$/ && $0 <= 23;

unit sub MAIN (Str $trains = "11:20 11:50 | 14:30 15:00",
   :l(:$loquacious),
   :v(:$verbose) = $loquacious,
); 

class Platform
{
  has Str $.id;
  has Str @.in-use is rw;

  method add-if-vacant (HHMM $from, HHMM $to)
  {
    for @.in-use -> $interval
    {
      my ($start, $end) = $interval.split("-");
      next if $to  lt $start;
      next if $end lt $from;

      return False;
    }

    @.in-use.push: "$from-$to";
    return True;
  }
}

class Station
{
  has Str      $.name;
  has Platform @.platforms is rw;

  method add-train (HHMM $from, HHMM $to)
  {
    for self.platforms -> $platform
    {
      return True if $platform.add-if-vacant($from, $to);
    }
      
    my $platform = Platform.new(id => (self.number-of-platforms + 1).Str);
    self.platforms.push: $platform;
    return $platform.add-if-vacant($from, $to);
  }

  method number-of-platforms
  {
    return @.platforms.elems;
  }
}

my $station = Station.new(name => 'Grand Central');

my @trains = $trains.split("|");

for @trains -> $from-to
{
  my (HHMM $from, HHMM $to) = $from-to.words;

  say ": Visiting train $from -> $to" if $verbose;

  $station.add-train($from, $to);
}

say ": Station: { $station.raku }" if $loquacious;

say $station.number-of-platforms;