Skip to content

Instantly share code, notes, and snippets.

/main.p6 Secret

Created January 10, 2017 01:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save anonymous/742781d1e69cf432a753f1b8821f8209 to your computer and use it in GitHub Desktop.
# solution for https://www.reddit.com/r/dailyprogrammer/comments/5m034l/20170104_challenge_298_intermediate_too_many_or/
my @lines = 'input.txt'.IO.lines;
my @stack;
for @lines -> $line {
for $line.comb.kv -> $i, $v {
if $v eq '(' {
@stack.push(($i, $v));
} elsif $v eq ')' {
if @stack.elems > 0 {
if @stack[*-1][1] eq '(' {
@stack.pop();
}
} else {
@stack.push(($i, $v));
last;
}
}
}
if @stack.elems > 0 {
my $val = @stack[*-1][0];
my $n = $line;
substr-rw($n, $val, 0) = '*';
substr-rw($n, $val+2, 0) = '*';
say "$n\n";
}else{
say "$line\n";
}
@stack = [];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment