Skip to content

Commit d58f0b3

Browse files
committed
Day 2 in Raku after understanding input subtleties
1 parent ff11396 commit d58f0b3

File tree

7 files changed

+343
-0
lines changed

7 files changed

+343
-0
lines changed

2021/day20/day20.raku

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env raku
2+
# Copyright 2021 Google LLC
3+
#
4+
# Use of this source code is governed by an MIT-style
5+
# license that can be found in the LICENSE file or at
6+
# https://opensource.org/licenses/MIT.
7+
#
8+
# https://adventofcode.com/2021/day/20
9+
use v6.d;
10+
use fatal;
11+
12+
my @binary-trans = '#' => '1', '.' => '0';
13+
14+
class Solver {
15+
has Str $.input is required;
16+
has @.enhancer = $!input.lines[0].trans(@binary-trans).comb;
17+
18+
method print-image(%image) {
19+
my $min = %image.keys.min;
20+
my $max = %image.keys.max;
21+
for $min.re.Int-1..$max.re.Int+1 -> $row {
22+
for $min.im.Int-1..$max.im.Int+1 -> $col {
23+
my $bit = %image{$row+i*$col} // '0';
24+
print ($bit eq '1' ?? '#' !! '.');
25+
}
26+
print "\n";
27+
}
28+
}
29+
30+
method evolve(%image, $horizon) {
31+
my Str %res{Complex};
32+
my $min = %image.keys.min;
33+
my $max = %image.keys.max;
34+
for $min.re.Int-1..$max.re.Int+1 -> $row {
35+
for $min.im.Int-1..$max.im.Int+1 -> $col {
36+
my $points = ($row+i*$col) «+« ((-1, 0, 1) X+ (-1i, 0i, 1i));
37+
my $pixels = $points.map({%image{$_} // $horizon}).join;
38+
%res{$row+i*$col} = @.enhancer[$pixels.parse-base(2)];
39+
}
40+
}
41+
%res
42+
}
43+
44+
method evolve-steps($steps, :$print = False --> Int) {
45+
my Str %image{Complex} = $.input.lines[2..*].pairs
46+
.map(-> $lp { $lp.value.trans(@binary-trans).comb.pairs
47+
.map({ $lp.key + i*.key => .value })
48+
}).flat;
49+
my $horizon = '0';
50+
for ^$steps {
51+
%image = self.evolve(%image, $horizon);
52+
say "{%image.keys.elems} and {%image.values.sum} 1s points after step $_ with horizon $horizon" if $print;
53+
$horizon = @.enhancer[$horizon eq '0' ?? 0 !! 511];
54+
self.print-image(%image) if $print;
55+
}
56+
%image.values.sum
57+
}
58+
}
59+
60+
class Part1 is Solver { method solve( --> Str(Cool)) { self.evolve-steps(2) } }
61+
62+
class Part2 is Solver { method solve( --> Str(Cool)) { self.evolve-steps(50) } }
63+
64+
class RunContext {
65+
has $.input-file;
66+
has $.input;
67+
has %.expected is rw;
68+
has @.passed;
69+
70+
method run-part(Solver:U $part) {
71+
my $num = $part.^name.comb(/\d+/).head;
72+
my $expected = $.expected«$num» // '';
73+
say "Running Day20 part $num on $!input-file expecting '$expected'";
74+
my $start = now;
75+
my $solver = $part.new(:$!input);
76+
my $result = $solver.solve();
77+
my $end = now;
78+
put $result;
79+
"Part $num took %.3fms\n".printf(($end - $start) * 1000);
80+
@!passed.push($result eq 'TODO' || $expected && $expected eq $result);
81+
if $expected {
82+
if $expected eq $result {
83+
say "\c[CHECK MARK] PASS with expected value '$result'";
84+
} else {
85+
say "\c[CROSS MARK] FAIL expected '$expected' but got '$result'";
86+
}
87+
}
88+
}
89+
}
90+
91+
sub MAIN(*@input-files) {
92+
my $exit = all();
93+
for @input-files -> $input-file {
94+
if $input-file.IO.slurp -> $input {
95+
my $context = RunContext.new(:$input, :$input-file);
96+
if (my $expected-file = $input-file.IO.extension('expected')).f {
97+
for $expected-file.lines {
98+
$context.expected«$0» = $1.trim if m/part (\d+) \: \s* (.*)/;
99+
}
100+
}
101+
$context.run-part(Part1);
102+
say '';
103+
$context.run-part(Part2);
104+
$exit &= all($context.passed);
105+
} else {
106+
say "EMPTY INPUT FILE: $input-file";
107+
}
108+
say '=' x 40;
109+
}
110+
exit $exit ?? 0 !! 1;
111+
}

2021/day20/example.output.part2

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
...........................................................................................................
2+
........................................................................................................#..
3+
....................................................................................................##.#...
4+
........................................................#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.....##.
5+
.....................................................#.##############################################...#..
6+
....................................................#######..................................#...##..#####.
7+
................................................#.#...#...#.##..#.#.....#.#.....#.#.....###.#.#.#.####.....
8+
...............................................#######..##.##..##.#.##...###..##.##....##..##.#.....#.#.##.
9+
............................................#.#....###.###....##...#.....#.##....#.#.###.#.####.#.##.##....
10+
...........................................###.#..#.#..##.#.##.#....######...##.###.#.######.##..#.##.####.
11+
..........................................#..#.#.#...##....##.#.###..#.#.##..#.##..#.###....######.####.#..
12+
.........................................#.#..#.#.#..###..##..#.#...##.....#..##....####..#..##..##..##..#.
13+
........................................###.#.###.#...##.#.#####..###.....##..##........#.##..#.#####..##..
14+
.......................................##..##......##.#......##.###..######.#.#.#.#.......#.##.####.#..#.#.
15+
......................................#...#..##..#.#....#...##.........#.##.##.#...#.###..##.#..#.###..##..
16+
.....................................#...#.#....###...#..##..##.##.###..###.......#..#.###.###..#..###...#.
17+
....................................#...#.#.#....#..##..##.#....##..#.....#####.##.###.#...#.#.....#.#.....
18+
...................................###..#..###...##########...#......##..#..#..#.###..###...##.#..##.....#.
19+
..................................##.##...##...###...#.####......####.....#....#.#....#.##.#...#...#...#...
20+
.................................####..##..#..#.###.######.########.#.#..###.##....#.....##...#.#..#.#####.
21+
................................#...####.###.##.#.##.##..###..####..#.#.....####...###...#.#.#.#.#.#.#.#...
22+
...............................#..#..#....##..####..##.##.##.##..#........#..##....##.#..#......####.#####.
23+
..............................#.#..#.....##...#...####.#...##.#.#.##.....###.#..#...####..##...##.#..####..
24+
.............................###..#....#....####..#.#.##.##.#..##..#..###..##.###.########.##.#..###..#.##.
25+
............................##.###.#.#...###.....###.#..#.....#.###.#.###...#.#.#.#...##.#..#.#......####..
26+
...........................###.####..###.....###.##.#.##..#...#..###.#.##.#.####..#..#.#.###.#..##.#...###.
27+
..........................#..##.#.##..#.#.####.##.##......#..#..#..#.###..#..#.#..########..#.#..#.#.#.##..
28+
.........................#....##.##.#....#...#..#.#....###.####..#.#.####.#...##..#.......##...###.##.#..#.
29+
........................#...##.#....#....###.###...#.###...##...#..#....#.#.#..#.......#.#.##..#.#..#...#..
30+
.......................###.##.....##.#.#....#..#...##.#.##.#....###.#....#....#.##....#..##.....##..#.##.#.
31+
......................##.#####..####......##....#.#..#...##.##..#.###..#..#..###.###..#...#.##..#....##.#..
32+
.....................####..##.#..###.#..#.#####...##...#.#.####..###....###....#.###..#..#...###.....###.#.
33+
....................#...######.#..##..#..#..#.#.###......#.#..#.##...#...#######.........#..#.##...##.###..
34+
...................#..##......#.....###..#..###...##....##.#..#.#....#..#..#...###...#.#.#.##.#.#.#.#...##.
35+
..................#..###..#.#.#..###...##..###.#..###...#.....##..###.##..#.....###......#.#..#....#.##....
36+
.................###...##.#...###.##..##.#.#.##.#.#....#######...##...#.....#...#..####..#.##...##.#.###.#.
37+
................##.#.###.#.#.#.#.##..#.......##...#....##..###...#.#.#..#.#.#...#.###....#..##.#.##.#......
38+
...............###..........#..#...#.#..###.#.#####..###.#.......#..##.#.#.#.....#.......#....##.###.#..##.
39+
..............#....#.#.#.#.#.##....#..#...##..#..#.....##.###.....#.#.###....#.##..###.#...#####...#...##..
40+
.............#......####.#.#..#...##.#.#.....#.#.##.#.#####.#.####...##.###.#.##.....###..#.#####.##.##.##.
41+
............#...##..#.#.#..#.##.#.#...##..###.##..#..#..#.....#.#####.#.##.##..#.##.#....######.##.#..#.#..
42+
...........###.##.#..###.#.#.####.###............#..########.#..#....#...####.##.#.#.#.#....#..####.#.#.##.
43+
..........##.#####..##....#.##.##.#....#.#....#.#.#..####.##..#####...####..##...#.###.##...#.#...###.#.#..
44+
.........####.....##..#.....#.###....###......#.#####.#..##.##########...#...#...#...#######.#.#..###...##.
45+
........#...##.#..##.#...#.#.##..##.######.#...##...#####.###...###.#.#.####.##..###..#.#..#..#..#.####.#..
46+
.......#..##....##.....#.#....#.#.#.##.###..#....##.#.###.#.#..#####.#.#.##..#..#.#..##.#.#.#...###..#.###.
47+
......#.#..###...###.##..#..#.###.###.#######...#..#.#....#....#.#######.######.#..#.##.###.#...##...##.#..
48+
.....###..##.#..##.#######....#.###.#..#.###.#...#.#.#.#.##...#.....#.....##...#....#...#.##..##.#...##.##.
49+
....##.#.####........#...#..#.###..#####....###..#.#..#.#..#..#....#.....#.###.#.......#.#..#.#.##...##.#..
50+
...###...#.....#.#.###..#..#..#..###.#.....#..########...###......##.#...####.#....#.###.#...#....#.#..###.
51+
..#...##....##..###.###.#.#....##...##..#..####...#..##..#.####......##..##...#.#.########...######.#...#..
52+
.##.....#.#...####.#.#....##.##.###.####..#.##.#.##....##..###...##.#.##..#......#.....##...#.#######..###.
53+
.#.#.###.#.###..#.#.##.#.##.##.....##.#..#.#.#.......##.#.#.##.###.#..###..###.#..##.#....##.......#.##.#..
54+
.###..#...##.#..#..##..##....#..#.#....##..#.#.#..#####.#..#...###.#.#######..#..#..##.#.#.......#...#.###.
55+
..##.#.###...##.###..###.......#.#....#...#..#.......#.#.#.#.##.###..###.##.####.###.....#.##...##....#.#..
56+
...#..##.#..#.##..###....#..##..#.##.####....#..#.......##.#.###.##..#.#.###..#.#..#...#...#.#.###.#....##.
57+
....#.#..####.#.....##..####..#.#..#...#..#...###.......###.#.#.#.###..#.##..#.#.#...#.#...#.#..#.....#.#..
58+
.....#.#...##.#.###.##..#......#.#..#....#.#.#.#.##..#..#...##....#.#......#..###.##..#...#...#..#...##....
59+
......#..##..#..#..#..###....##..#..##.#..#...........###.##...##..#..##.#....#.#.#...##.###.##..#..###....
60+
.......#..#.#..##..##..####.#...##...##...##....####...##........#..#....#..###....#######..#.##..#.#......
61+
........#.#.##..#..#.#.#..#..##.####.....#.#....#..#..#..#.#.###....##..###..#..##.#####.##......#.........
62+
.........#..#....##....#.#.#..#.#.##.###..#.###..###.#.###..#..###..#..#.#.###.#...##.##.###....##.#.......
63+
..........##.####...######.##.###..#..#######...##...#.#.#...##.#.#.#...#..###......#..#.........#.........
64+
...........##......#...#.####..#..#.#..#.......#.....#.##...##...#.###.#...#####.#.#...###...##.##.........
65+
............###.##...###.#.#..#..##...#.#..#.#####.##..#.#..##....##.#.....#.####.#..####.##...##..........
66+
.............##.##.#.#.#.####.#.##...##.....#..#..#.##.#.#.#.#.#..###.###....###.#.#########.##............
67+
..............#.#######...#...#.#..##..#########...##.#.#..#...###.#..#..##.###.....#...#..#..#............
68+
...............##..####.###########..#.#......#..#.#...##.###.#..#.###.#####.##.##..###..####..............
69+
................#....#...#.#..#.....#.....#....#.#....##..##.#.......#..###.##.####.#.#..##................
70+
.................###..#..##.###.##...#...####...#...###.#..#..#....#####....##.#####.#....##...............
71+
..................#.##.#..#.#.##..####.##..#####....#.#.##.#.###..###.#..##.....##.#....##.................
72+
...................#..#...#..#..#..#....###.#.######..#.#####.##.#...##..#..#..#........##.................
73+
....................##..##...#......#.#.#.......###.##.#..###.....#.#...#####..##....#.#...................
74+
.....................#.#####....####.##.####...#.#####..#...###......####..#.#..##.....#...................
75+
......................###......#.#..##...####.#...#..#..#.##...##..#...#.#...#...##.#.#....................
76+
.......................######.##..###.#.#.##...###...##..#....#..##..##..##.#..##..##......................
77+
........................####.##..####.########.###.#.#..#.###...##.#..#.#...##.###..#......................
78+
.........................#..#.........###.#..##..#..#.##.##..#####........#....##.#........................
79+
..........................#..#.#.#.#.##......#.#.#....###..#.##..###....########...........................
80+
...........................###..##.....##.##.####..##..#......###..##.....#....###.........................
81+
............................#.#.#..####.#.#..#.#.#.#.#.#.##..#......##.#.#.##..#...........................
82+
.............................#.#..#..#......###.#..##..##.###.#.####.####.#..#.#...........................
83+
..............................##....##.###....###..#.######......##.###...#.#.#............................
84+
...............................##..#..#.#####.#.##.##..##.##..##.###..#.....#..............................
85+
................................#.###...#..#..###.##..###..##..##....###..#.#..............................
86+
.................................##..#.##.#.#.##.#.####.####.#.##.#..###..#................................
87+
..................................##......#.....#....###.#..#......###...##................................
88+
...................................#.#.###.##...#.##....#..####..#.##..###.................................
89+
....................................#....###.##.##....#..##.##..##..#.##...................................
90+
.....................................#.##.#.###..#.###...###...#.##...#....................................
91+
......................................##.#.##.####..##...###..#.....#.#....................................
92+
.......................................####..#..##..#..##.#.....#.###......................................
93+
........................................#.##......#...#.#..########........................................
94+
.........................................##..#..##.####..#.#.#.....#.......................................
95+
..........................................#..####...###..#.#..#.##.........................................
96+
...........................................###..####...##....####..........................................
97+
............................................#.####..#...###....#...........................................
98+
.............................................##...###.##...#.###...........................................
99+
..............................................#.#.#.....#..#..#............................................
100+
...............................................##.....#.##...#.............................................
101+
................................................#.#....#.#.#...............................................
102+
.................................................#.###.##..#...............................................
103+
..................................................######...................................................
104+
...................................................#.#..##.................................................
105+
....................................................##.##..................................................
106+
.....................................................###...................................................
107+
...........................................................................................................

2021/day20/input.actual.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
part1: 5432
2+
part2: 16016

0 commit comments

Comments
 (0)