diff options
author | Nguyễn Gia Phong <mcsinyx@disroot.org> | 2021-12-05 15:18:59 +0700 |
---|---|---|
committer | Nguyễn Gia Phong <mcsinyx@disroot.org> | 2021-12-05 15:18:59 +0700 |
commit | 85bd7ec1bd9cdc7ec53692fce5cae3118b7357a0 (patch) | |
tree | 1880d6822b4c1e6d978abb0dda073dd42755efad /aoc/2021/03 | |
parent | 48f468b9b8a3f909b60399e277405b9723b8643a (diff) | |
download | cp-85bd7ec1bd9cdc7ec53692fce5cae3118b7357a0.tar.gz |
[aoc] Add first five/fifth
Diffstat (limited to 'aoc/2021/03')
-rw-r--r-- | aoc/2021/03/part-one.zig | 19 | ||||
-rw-r--r-- | aoc/2021/03/part-two.raku | 8 |
2 files changed, 27 insertions, 0 deletions
diff --git a/aoc/2021/03/part-one.zig b/aoc/2021/03/part-one.zig new file mode 100644 index 0000000..6088925 --- /dev/null +++ b/aoc/2021/03/part-one.zig @@ -0,0 +1,19 @@ +const print = @import("std").debug.print; + +const input = @embedFile("input"); +const mean = @splat(12, @as(u16, '0' * 1000 + 500)); +const pos = @Vector(12, u4){ 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; + +pub fn main() !void { + var sum = @splat(12, @as(u16, 0)); + var i = @as(usize, 0); + while (i < input.len) : (i += 13) { + var j = @as(usize, 0); + while (j < 12) : (j += 1) + sum[j] += input[i + j]; + } + + const common = @bitCast(@Vector(12, u1), sum > mean); + const gamma = @reduce(.Add, @as(@Vector(12, u12), common) << pos); + print("{}\n", .{ @as(usize, gamma) * ~gamma }); +} diff --git a/aoc/2021/03/part-two.raku b/aoc/2021/03/part-two.raku new file mode 100644 index 0000000..71464a2 --- /dev/null +++ b/aoc/2021/03/part-two.raku @@ -0,0 +1,8 @@ +sub filter(&op, @report, $column=0) { + return parse-base @report[0].join, 2 unless @report.elems > 1; + my $common = ([+] map *[$column], @report) > (@report.elems - 1) div 2; + filter &op, (grep { op $_[$column], $common }, @report), $column + 1 +} + +my $input = map *.comb, words slurp 'input'; +put [*] map { filter $_, $input }, (&infix:<==>, &infix:<!=>) |