summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2020-12-03 13:19:38 +0100
committerPatrick Spek <p.spek@tyil.nl>2020-12-03 13:19:38 +0100
commit9c568c06d6a9996928fdc26664ea2b063367a59d (patch)
treed33f23faab8770014700fd8def23cdf8d1460dd2
parent329cccd89b2f6fb385ed185492cf41d2a7cb4e86 (diff)
downloadadvent-of-code-9c568c06d6a9996928fdc26664ea2b063367a59d.tar.gz
advent-of-code-9c568c06d6a9996928fdc26664ea2b063367a59d.tar.bz2
Add day 3 solutions
-rwxr-xr-x2020/03/bin/first36
-rwxr-xr-x2020/03/bin/second52
-rw-r--r--2020/03/resources/input.txt323
3 files changed, 411 insertions, 0 deletions
diff --git a/2020/03/bin/first b/2020/03/bin/first
new file mode 100755
index 0000000..b45e01d
--- /dev/null
+++ b/2020/03/bin/first
@@ -0,0 +1,36 @@
+#!/usr/bin/env raku
+
+unit sub MAIN ();
+
+my @input = $*PROGRAM.parent(2).add('resources/input.txt').linesĀ».comb;
+
+my $x = 0;
+my $y = 0;
+my $trees = 0;
+
+# The lines are all evenly wide, so just check the width of the first
+# row.
+my $x-max = @input.first.elems - 1;
+
+# Just get the number of rows.
+my $y-max = @input.elems - 1;
+
+
+loop {
+ if (@input[$y][$x] eq '#') {
+ $trees++;
+ }
+
+ $x += 3;
+ $y += 1;
+
+ # The map extends infinitely to the right
+ if ($x-max < $x) {
+ $x -= $x-max + 1;
+ }
+
+ # But we have to stop when we reach the lower end of the map.
+ last if $y-max < $y;
+}
+
+say $trees;
diff --git a/2020/03/bin/second b/2020/03/bin/second
new file mode 100755
index 0000000..ff6059e
--- /dev/null
+++ b/2020/03/bin/second
@@ -0,0 +1,52 @@
+#!/usr/bin/env raku
+
+sub MAIN ()
+{
+ my @input = $*PROGRAM.parent(2).add('resources/input.txt').linesĀ».comb;
+
+ [
+ { right => 1, down => 1 },
+ { right => 3, down => 1 },
+ { right => 5, down => 1 },
+ { right => 7, down => 1 },
+ { right => 1, down => 2 },
+ ]
+ .map({
+ get-trees-in-slope(@input, $_<right>, $_<down>)
+ })
+ .reduce(&infix:<*>)
+ .say
+}
+
+sub get-trees-in-slope (
+ @input,
+ Int:D $right,
+ Int:D $down,
+ --> Int
+) {
+ my $x = 0;
+ my $y = 0;
+ my $trees = 0;
+
+ my $x-max = @input.first.elems - 1;
+ my $y-max = @input.elems - 1;
+
+ loop {
+ if (@input[$y][$x] eq '#') {
+ $trees++;
+ }
+
+ $x += $right;
+ $y += $down;
+
+ # The map extends infinitely to the right
+ if ($x-max < $x) {
+ $x -= $x-max + 1;
+ }
+
+ # But we have to stop when we reach the lower end of the map.
+ last if $y-max < $y;
+ }
+
+ return $trees;
+}
diff --git a/2020/03/resources/input.txt b/2020/03/resources/input.txt
new file mode 100644
index 0000000..c6ed96d
--- /dev/null
+++ b/2020/03/resources/input.txt
@@ -0,0 +1,323 @@
+....##..#........##...#.#..#.##
+.#.#..#....##....#...#..##.....
+##.#..##..#...#..........##.#..
+.#.##.####..#......###.........
+#.#.#...........#.....#...#....
+#.......#....#.#.##..###..##..#
+.#...#...##....#.........#.....
+..........##.#.#.....#....#.#..
+.......##..##...#.#.#...#......
+.#.#.#...#...##...#.##.##..#...
+........##.#.#.###.........##..
+#.#..#.#.#.....#...#...#......#
+.#.#.#...##......#...#.........
+.#..##.##.#...#...##....#.#....
+.##...#..#..#......##.###....##
+.....#...#.###.....#.#.........
+#.##..#....#.#.#.#.............
+........#...#......#...#..#....
+##..##...##.##...#...#.###...##
+#.#....##.#...###......#..#.#..
+..#.....#.##......#..........#.
+#.......#..#......#.....#....#.
+.....###...........#....#.##...
+#.#........##.......#.#...#.##.
+.#.#.#........#........#.#.....
+#..#..##.....#.###..#.#.#.##..#
+..#.#...#..##.#.#.#.......###..
+........#........#..#..#...#...
+##............#...#..##.##...#.
+#....#.#.....##...#............
+............#...#..#.#.#....#..
+#.#.#...##.##.#....#....#......
+................###.....#.....#
+##.#####.#..#...###..#...###...
+...#.....#...#.#....#...#..#...
+.......#....##.##.#.##.........
+..#..#..##.....#...#.#.....#...
+...#...#.#.##.#..###.......#...
+...#...........#.#####..##..#..
+#.#...#........####..#......#.#
+#..#.##...........#.#......#.##
+#.#..#....##..#..##.#..........
+.....#..#.....#........##..#...
+...###.......#.##.......#......
+...##..#..#...#....#.###...#...
+....####....#........#.##.#.#.#
+#....#.....#.###.##...##..##.##
+.##.#...#.##.#......#..##.#....
+...#.............#.............
+..##..##.#.....#........##....#
+#.....#....#.......####...#..#.
+..#...#..#...#...##.#....##....
+.#...##....#....#..#....#......
+##..#.#...##......#..#.......##
+..#...#.##..#.....#.#...#..#.#.
+#..##....#..........#..........
+.#........#..#......#......#.#.
+...##.#.........#.#....#.#...#.
+#.....#.#..#...#...#..#...#...#
+#.........#.#.........##.......
+.#.......#......#.........###..
+.#..#..##...........#.....#..#.
+.#....................#.....#..
+.##.....#....#....#.###.....#..
+...##.#.....#.#....#.........#.
+.........##.....#.....#.##..#..
+......#......#.#..#..###...#..#
+..##...#.#..#...#.#....#.......
+..#..##.###.#..#..#..#......#..
+.....##...##.........#...##...#
+###.#..##....##...##.#..###....
+#...#.#..##......##...#.#..#...
+..........#....###....#........
+#.#.#.#.#.....#..##.##.....#...
+.##.....#...#.....#......#.....
+.#..........#.#.............#..
+.#..##..#.#..##...#....#.##...#
+..#.#..........#...#..........#
+.#.......#.......#...#..#.....#
+##.#...##...#......#.#..#......
+#####..#....#..#...#...#.#.....
+....#.......#.#..#.............
+#..#..#.#.####...#....#....##..
+#..#.##.#......#...#......#....
+#...##.##...#....#..........##.
+..#..#.......##.#....#...#.#...
+.....#.##..............##.....#
+..##.##...##.....#.........#.#.
+.#....##...##...##..#....##..#.
+.#...#....#..#......#.#........
+#....#.#.#..............#....##
+..##..#..#....#####.#....#.#.##
+#....#...#.##..#.##.........###
+#..#..#....#...............#.#.
+#....##...##........##.##.#.##.
+......#......##....##.....#.###
+#...##..#..#.....#.#........##.
+..#.#.##...#...#....#..###...#.
+#..##..#.###..##.#.#....#......
+..###..#.##........###.....#...
+#.............#.............#..
+.#.##....#..##.#...#....#.#####
+###.....###.#......##..#..##...
+.#.#......##.#....#....#.#..#..
+###..#..#....#......###.##.....
+......#.......#......#..##..##.
+..#..#...#..#....#.##....#.#..#
+.......##..#........#.#.##.....
+.#...#..#........#..#.#..#..#.#
+.#..#.##.......#......#...#.#..
+.##..##......##.#...#......####
+.....#....#......#.....#......#
+..........#.#.#...##.#..#.#....
+...#.......##......#..#.#.##...
+.###..#.#.#....................
+##...#...#.##............#.....
+....#....#.##...#..#.#...##....
+..#.#....#.###...#...#.#.####.#
+..#..#.#...#.#......##.........
+#..#..####.##.#.#..###....#...#
+....#..........#.##.#..#.#.#.#.
+#.#.##.........#.....##...#..##
+#......#...#.##.#......#..#.#..
+#...#........#..#..#...##...#..
+.....#.####..##..#.#.##..#...#.
+#..#........#........#...#....#
+...........#..#.....#.#.#.#....
+....#......#....#...#....##....
+.#.#..#...#.#....#..#.#....##.#
+....#...#...#.##..#...#..##...#
+#######...............##.....##
+.#.#..............#....#..#.###
+#......#.#......###....###.....
+##..#...#.##..##..##.#....#....
+#....##..#..#...#.#.#...#......
+..........#..#.##..##.##.#..##.
+....#.#.#.....##........#..#...
+..###...#.....##.##.....##..##.
+....#.#..#.#.......#.......#...
+..##.#..#.....##...###...#...#.
+..#.........#...##...#...#..#..
+..#..#..#..#..##.#...##..#.#...
+...##..#..##..#..####...#.....#
+............#............###...
+.#.#.###.#.....#.#.#..#.###..#.
+...#.........#.....####........
+....##.#..##.#.............#...
+....#.##.....#..#.....#......##
+..........#.............#...##.
+#..#.....#.......##..###.......
+..##.#...........#.......#..#..
+...#...#.#.##.###....#.#..#....
+...#..........##..#..#..#...###
+.........#.....#..##.....#..#..
+#........#...#...#..#.#....##..
+.#.#.....####..#.##.#..........
+###.......##..#.##...#.....#...
+..###.##.#..#..#..#.....##...#.
+.........#.....##.#..#..##.....
+#..#..##...###..............#..
+#....#.#....#..#.....#..####...
+####..#.....##...#..#.#.#.#...#
+...#....#.....#.##.#.#.#....##.
+..........#...#.....###....#.##
+#....#.#.#....#..#..#.....#....
+.....#.#...#......#....#......#
+.####....##...#...#......##..#.
+.#...#..#....#..#..............
+##.#...##...#.##..#......#.....
+..####.##..#....#.#......#.#.##
+........#.....##...#.#..##....#
+....#.#.#.#.###...#.#...##...##
+#.....#...####.#....#.#........
+..#.....#...##.........###.....
+....#....#....#..#......#####.#
+###.....#..#.#.#......#.##.#...
+....#.##......#..#.#...........
+.#....#....#.#..#.......#...##.
+...................#.#.#..#....
+##...#.....#........#....#...#.
+........##......#...##.#..#.#.#
+#.#..###...#....#.#...#.......#
+#..........##......#..#..#.....
+.............#...##.#...#......
+#..#....##..#.........#..#.###.
+.....#..........#....##.#...##.
+###....................#.#.##..
+#........##...##......#....###.
+#....#.............#....#...#..
+##.......##.#.......#....#..#..
+##...#............#..#.#....##.
+..#.#..#...#####..........#....
+..#.........##.....#.#...####..
+....#..............#...........
+..#...#.#.#..#.......##.#.#.#..
+....#.##.....##..#.....#..####.
+#...#...#...#.......#.........#
+......#..#.####...###.#.#.....#
+.......#..#..#.....#.#..##.#..#
+.#......##..#............#.....
+.#........#.#....#....#........
+.....#.#..#.##.#..##....#..#...
+#.#...........#....##.....#....
+..#..#..##.###..##..#..###.#.##
+##.#..#...##.#.........#...#.#.
+......#..#..##...#....#...#.##.
+#.##.......................#...
+.......#..#..#.##..##......#...
+..#.#...#.....#..###....#...#..
+##..#.....#..#..#.##.....#...##
+#...##...###...#.#..###....#...
+...#.#.#..####.....#...##....#.
+.#.#..##.....#..#.....##..##..#
+#...#..........#.##.#.#........
+..##....#.##....#..##......#...
+....#..........###.....####..##
+...........###....##.#.#.#.#...
+...#......................####.
+#.#.#...#.#.#.#.#......#.....##
+..###...#.####...#..##..#....#.
+....#....#.......#...#.........
+.#.###.............##..#...#...
+....#.#....##...#.....#.##.....
+#.......##.....#.#.....#....##.
+....##.....###..#.#..#....#...#
+......#..##...#......#.....#.##
+.#.....#.##.###....#.....#..###
+...#..#.###.#....#..#..#...##.#
+...##..#...#..#.#.#..#...#.....
+##.####...##..#.#.#....#.......
+..##..#.#.......##.#......##.#.
+....##....#....#..#....#..##.#.
+..##..........##....#...#.#..#.
+#.#...#.#.###.#.#..##.#...#....
+.....#..#.............#...#...#
+....#.#..#...##...#....#.##....
+#..#...#.###.....#...#.....#.#.
+#####....#....#....#.......#.##
+#...##....##.#.#...#.....##.#..
+#.......#...#..#..#...#....#...
+....#......#.#..........#....##
+#.###...#.#..##..#.##........#.
+#..#.....##.......#..#..#.#....
+...#...#.##...#....#.#.#.#...#.
+...##..#.#....#......###......#
+#.#....#...#..#..#....#........
+..#..#.#...##..........#.###...
+#..........#...#..#....#....###
+..#..#.#....#..............#...
+...#........#...#.#....###.#..#
+....#.#.#................#..#.#
+..#........##.#....#.#..#......
+...##..#..#.......#..#......#.#
+..#..#....#.........#....#.##..
+#.....#....###.#..#..#...#...#.
+..#..##.###.#..##....#.###.....
+...#...####..#........###.#....
+.........#.#...#..#..#.#.......
+.##.........##.#..............#
+..#.#.#.....###........#.#.#..#
+....##..#....#....#.#..#.......
+#.#.....#...#........##........
+.##.#.#..#..#.#.#.........#....
+#.....#..#.##...#...#..........
+##..#....#....##.#..#.........#
+................#.##.#......#.#
+..#..#.#........##...###..#...#
+##........#.......#...##.##..#.
+##....#.....#..##....#.......#.
+#.#....#.#........#..#.........
+......##......#...#.....#.##...
+###....#..........##.#.#......#
+......#...###.........###..#...
+.####....#...##..#.#.....#...#.
+.##...#...###....#...#.#..###..
+#..#......##...#.###..###...#..
+#....#.#.#..#....##...#.##..#..
+..#.....#...#..........#.##.###
+#.....#....###.......##..##.#..
+#..##...#..#....#.###......#...
+#..#........##..#.....#.#.#....
+#.##.#.#..#....#.#.............
+.#...............#....##.......
+.#.##......##........#...#..#.#
+.#...#....#....#...#..#...##...
+.....#..###...##........#.#....
+...#.......#....##..#..#....#..
+...###....#........#..#.###.#..
+......##..##..............###.#
+.......#.####..##....#.#....#..
+#...#......#...#..#.....##....#
+.#..#..###....#..##.##.#.......
+#......##......#..##....#..##..
+.....#..#.#......##.##..##.....
+...#..#.......#......#.........
+....#..####......#..#....#...#.
+..#.#..#...#....#....#.......#.
+####..#........#.###...##.#.#.#
+.......##........#.#.#...##....
+...#.........#..#.#..##....#...
+.....#..#...#.#....#...#.#.##.#
+#..##.....#.....##.......#...#.
+.......##.#.#.....#....#......#
+...#...#.##...#......#....#....
+..#..#.#...#..#.....#...###.#..
+.........#...#..#.......##.....
+..##...................#..#.###
+.##.##..#.#...#.#....#.....##..
+#.#...##...#...#...##..#......#
+....#..#...#.....##.#.....#..##
+##.#..........###..#...#..#....
+...##....#.##....#......#......
+.....#.........#....#.#.......#
+.......#............#.#.....#..
+..#..#...#..#####..#....##.....
+...##......##...#.#........##..
+.....#..###...##.#.#.##.#...#..
+..#.#.#..##..#.##...##.#.#.....
+......##...#..##......#.#......
+......................#........
+#...#..#....#..#.#.##.#.....#.#
+.#......#.#....#.#.#..#....#...
+.#..#.#.#..#....#..............