aboutsummaryrefslogtreecommitdiff
path: root/2017/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 /2017/lib
parent5fa17cd54a1170449a91da48a2fe88a99349daa2 (diff)
downloadadvent-of-code-a0fcb600572ea0da9dee1b5b9e7a24ce674ebe7e.tar.gz
Add 2018 and 2017
Diffstat (limited to '2017/lib')
-rw-r--r--2017/lib/AdventOfCode.pm678
1 files changed, 78 insertions, 0 deletions
diff --git a/2017/lib/AdventOfCode.pm6 b/2017/lib/AdventOfCode.pm6
new file mode 100644
index 0000000..eef09d0
--- /dev/null
+++ b/2017/lib/AdventOfCode.pm6
@@ -0,0 +1,78 @@
+use v6;
+
+sub do-captcha(@input, $cmp-offset) returns Int {
+ my @tmp = @input.rotate($cmp-offset);
+ (@input Z @tmp).map(-> $z {
+ if $z[0] == $z[1] { $z[0] } else { 0 }
+ }).reduce(&[+])
+}
+
+sub day01(Str $input) returns Int is export {
+ do-captcha($input.comb, 1)
+}
+
+sub day01b(Str $input) returns Int is export {
+ do-captcha($input.comb, $input.comb.elems/2)
+}
+
+sub checksum(@input) returns Int is export {
+ [+] @input.map(-> @i {
+ @i.max - @i.min
+ })
+}
+
+sub find-divisible(@row) returns Int {
+ for @row -> $x {
+ for @row -> $y {
+ if $x %% $y and $x != $y {
+ return ($x/$y).Int
+ }
+ }
+ }
+}
+
+sub evenly-divisible-values(@input) returns Int is export {
+ [+] @input.map(-> @i {
+ find-divisible(@i)
+ })
+}
+
+# Day 3
+
+sub position(Int $num) {
+ my Int $num-square = (^Inf).map(*²).grep(* ≥ $num)[0];
+ my Int $square-size = $num-square.sqrt.Int;
+ my $max-step = ($square-size/2).floor;
+ my @retval = $max-step, $max-step;
+ say "$num, $num-square, $square-size, $max-step, {@retval}";
+ if $num-square == $num { return @retval; }
+
+ while $num-square > $num {
+ say "$num-square > ($square-size - 1)";
+ $num-square -= ($square-size -1);
+ }
+
+ $num-square > $num or die(" Error !! $num-square > $num");
+
+ if $num-square == $num { return @retval; }
+
+ for ^($square-size*4 -1) -> $i {
+ $num-square -= 1;
+ say "Step 2-$i: $num-square == $num ? {@retval[0]}";
+ if $num-square != $num {
+ @retval[0] = (@retval[0]-1) % ($max-step + 1);
+ }
+ }
+ say "Returning: {@retval}";
+ return @retval;
+}
+
+sub manhattan-distance(@position) returns Int {
+ say "Sum {@position.perl}";
+ [+] @position
+}
+
+sub spiral-memory(Int $input) returns Int is export {
+ say "position: {position($input).perl}";
+ manhattan-distance(position($input))
+}