From a0fcb600572ea0da9dee1b5b9e7a24ce674ebe7e Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sat, 5 Dec 2020 10:55:56 -0300 Subject: Add 2018 and 2017 --- 2018/lib/Utils.pm6 | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 2018/lib/Utils.pm6 (limited to '2018/lib') diff --git a/2018/lib/Utils.pm6 b/2018/lib/Utils.pm6 new file mode 100644 index 0000000..5b955d3 --- /dev/null +++ b/2018/lib/Utils.pm6 @@ -0,0 +1,45 @@ +use v6; + +grammar Day3 is export { + token TOP { " @ " ": " } + token id { "#" \d+ } + token position { $ = [\d+]","$=[\d+] } + token size { $=[\d+]"x"$=[\d+] } +} + +sub parse-input(--> List) is export { $*IN.lines.List } + +sub to-int(Str @input --> List) is export { @input.map: *.Int } + +sub dd-matrix(@m) { + for ^@m.shape[0] -> \i { + my $s = ""; + for ^@m.shape[1] -> \j { + $s~=@m[i;j]; + } + say "[" ~ $s.comb.join(", ") ~ "]"; + } +} + +sub levenshtein($s, $t) is export { + my $m = $s.chars; + my $n = $t.chars; + my int32 @d[$m+1;$n+1] = (0 xx $n+1) xx $m+1; + + for 1..$m { @d[$_;0] = $_ }; + for 1..$n { @d[0;$_] = $_ }; + + for 1..$n -> \j { + for 1..$m -> \i { + my $sub-cost = 0; + if $s.comb[i-1] ne $t.comb[j-1] { + $sub-cost = 1; + } + @d[i;j] = (@d[i-1;j] + 1, + @d[i;j-1] + 1, + @d[i-1;j-1] + $sub-cost).min; + } + } + + return @d[$m;$n]; +} -- cgit v1.2.3-70-g09d2