aboutsummaryrefslogtreecommitdiff
path: root/2018/lib
diff options
context:
space:
mode:
authorMatias Linares <matiaslina@gmail.com>2020-12-05 10:55:56 -0300
committerMatias Linares <matiaslina@gmail.com>2020-12-05 10:55:56 -0300
commita0fcb600572ea0da9dee1b5b9e7a24ce674ebe7e (patch)
treea663ee4f6a427e98db0878ca539c9288bfbc70d0 /2018/lib
parent5fa17cd54a1170449a91da48a2fe88a99349daa2 (diff)
downloadadvent-of-code-a0fcb600572ea0da9dee1b5b9e7a24ce674ebe7e.tar.gz
Add 2018 and 2017
Diffstat (limited to '2018/lib')
-rw-r--r--2018/lib/Utils.pm645
1 files changed, 45 insertions, 0 deletions
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 { <id> " @ " <position> ": " <size> }
+ token id { "#" \d+ }
+ token position { $<left> = [\d+]","$<top>=[\d+] }
+ token size { $<width>=[\d+]"x"$<height>=[\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];
+}