-
Notifications
You must be signed in to change notification settings - Fork 320
/
ch-1.raku
60 lines (54 loc) · 1.39 KB
/
ch-1.raku
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
#!/usr/bin/env raku
use v6;
class DoubleLinkedList { ... }
sub MAIN ($string = 'Perl Weekly Challenge') {
my $order = DoubleLinkedList.new;
my %dll-element-for;
my $i = 0;
for $string.comb -> $character {
if %dll-element-for{$character}:exists {
if defined %dll-element-for{$character} {
$order.remove(%dll-element-for{$character});
%dll-element-for{$character} = Nil;
}
}
else {
%dll-element-for{$character} = $order.push($i).tail;
}
++$i;
}
die 'no result, sorry!' unless defined $order.head;
put $order.head.value;
}
class DoubleLinkedList {
class Element {
has $.value;
has $.pred is rw is built = Nil;
has $.succ is rw is built = Nil;
}
has $.head is rw is built = Nil;
has $.tail is rw is built = Nil;
method push ($value) {
my $element = Element.new(
value => $value, pred => $.tail, succ => Nil);
$.tail.succ = $element if defined $.tail;
$.tail = $element;
$.head //= $element;
return self;
}
method remove ($element) {
if (defined $element.pred) {
$element.pred.succ = $element.succ;
}
else {
$.head = $element.succ;
}
if (defined $element.succ) {
$element.succ.pred = $element.pred;
}
else {
$.tail = $element.pred;
}
return self;
}
}