Skip to content

Commit 4de119f

Browse files
committed
Day 25 in Raku
1 parent 5089068 commit 4de119f

File tree

5 files changed

+291
-0
lines changed

5 files changed

+291
-0
lines changed

2021/day25/day25.raku

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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/25
9+
use v6.d;
10+
use fatal;
11+
12+
class Solver {
13+
has Str $.input is required;
14+
has Complex $.mod;
15+
has SetHash %.cucumbers is rw = east => SetHash.new, south => SetHash.new;
16+
17+
submethod TWEAK {
18+
my $maxre = 0;
19+
my $maxim = 0;
20+
for $!input.lines.kv -> $row, $line {
21+
for $line.comb.kv -> $col, $char {
22+
given $char {
23+
when '>' { %!cucumbers<east>.set($row+i*$col) }
24+
when 'v' { %!cucumbers<south>.set($row+i*$col) }
25+
}
26+
$maxim = max($maxim, $col);
27+
}
28+
$maxre = $row;
29+
}
30+
$!mod = $maxre + 1 + i * ($maxim + 1);
31+
}
32+
33+
method next-pos($pos, $dir --> Complex) {
34+
given $dir {
35+
when 'east' { $pos.re + i*(($pos.im + 1) % $!mod.im) }
36+
when 'south' { ($pos.re + 1) % $!mod.re + i*$pos.im }
37+
}
38+
}
39+
40+
method print-grid() {
41+
for 0..^($!mod.re) -> $row {
42+
for 0..^($!mod.im) -> $col {
43+
print(do given $row + i*$col {
44+
when $_ %!cucumbers<east> { '>' }
45+
when $_ %!cucumbers<south> { 'v' }
46+
default { '.' }
47+
});
48+
}
49+
print "\n";
50+
}
51+
}
52+
}
53+
54+
55+
class Part1 is Solver {
56+
method solve( --> Str(Cool)) {
57+
# say "Start size $.mod with {+%.cucumbers<east>} east and {+%.cucumbers<south>} south";
58+
# self.print-grid;
59+
for 1..-> $i {
60+
my SetHash %prev = %.cucumbers;
61+
%.cucumbers<east> = SetHash.new;
62+
%.cucumbers<south> = SetHash.new;
63+
my $changes = 0;
64+
for %prev<east>.keys {
65+
my $n = self.next-pos($_, 'east');
66+
if $n %prev<east> || $n %prev<south> {
67+
%.cucumbers<east>.set($_);
68+
} else {
69+
%.cucumbers<east>.set($n);
70+
$changes++;
71+
}
72+
}
73+
for %prev<south>.keys {
74+
my $n = self.next-pos($_, 'south');
75+
# east already moved, check new state
76+
if $n %.cucumbers<east> || $n %prev<south> {
77+
%.cucumbers<south>.set($_);
78+
} else {
79+
%.cucumbers<south>.set($n);
80+
$changes++;
81+
}
82+
}
83+
# self.print-grid;
84+
# say "Round $i changed $changes";
85+
return $i if $changes == 0;
86+
}
87+
}
88+
}
89+
90+
class Part2 is Solver { method solve( --> Str(Cool)) { "Merry Christmas"; } }
91+
92+
class RunContext {
93+
has $.input-file;
94+
has $.input;
95+
has %.expected is rw;
96+
has @.passed;
97+
98+
method run-part(Solver:U $part) {
99+
my $num = $part.^name.comb(/\d+/).head;
100+
my $expected = $.expected«$num» // '';
101+
say "Running Day25 part $num on $!input-file expecting '$expected'";
102+
my $start = now;
103+
my $solver = $part.new(:$!input);
104+
my $result = $solver.solve();
105+
my $end = now;
106+
put $result;
107+
"Part $num took %.3fms\n".printf(($end - $start) * 1000);
108+
@!passed.push($result eq 'TODO' || $expected && $expected eq $result);
109+
if $expected {
110+
if $expected eq $result {
111+
say "\c[CHECK MARK] PASS with expected value '$result'";
112+
} else {
113+
say "\c[CROSS MARK] FAIL expected '$expected' but got '$result'";
114+
}
115+
}
116+
$*OUT.flush;
117+
$*ERR.flush;
118+
}
119+
}
120+
121+
sub MAIN(*@input-files) {
122+
my $exit = all();
123+
for @input-files -> $input-file {
124+
if $input-file.IO.slurp -> $input {
125+
my $context = RunContext.new(:$input, :$input-file);
126+
if (my $expected-file = $input-file.IO.extension('expected')).f {
127+
for $expected-file.lines {
128+
$context.expected«$0» = $1.trim if m/part (\d+) \: \s* (.*)/;
129+
}
130+
}
131+
$context.run-part(Part1);
132+
say '';
133+
$context.run-part(Part2);
134+
$exit &= all($context.passed);
135+
} else {
136+
say "EMPTY INPUT FILE: $input-file";
137+
}
138+
say '=' x 40;
139+
}
140+
exit $exit ?? 0 !! 1;
141+
}

2021/day25/input.actual.expected

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
part1: 419
2+
part2: Merry Christmas

0 commit comments

Comments
 (0)