~cricket/advent2020

ref: 84f7d5e11fcda7f69f437a29d0f30255965931d7 advent2020/3/bigboy.zig -rw-r--r-- 1.1 KiB
84f7d5e1cricket day 3: add bigboy solution; make main solution stupid compact 10 months ago
                                                                                
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
const std = @import("std");
const input = @embedFile("bigboy");
const big = std.math.big;
const int = big.int;
var w: usize = 0;

fn slope(sx: usize, sy: usize) usize {
    var trees: usize = 0;
    var x: usize = 0;
    var y: usize = 0;
    var i: usize = 0;
    while (i < input.len) : ({ i = (w * y) + @mod(x, w-1); y += sy; x += sx; }) {
        trees += @boolToInt(input[i] == '#');
    }
    return trees;
}

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const a = &gpa.allocator;

    w = std.mem.indexOfScalar(u8, input, '\n').? + 1;
    const slopesx = [_]u8{ 2, 3, 4, 6, 8, 9, 12, 16, 18, 24, 32, 36, 48, 54, 64 };
    const slopesy = [_]u8{ 1, 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 47 };
    var bigboy: int.Managed = try int.Managed.initCapacity(a, 32);
    try bigboy.set(1);

    for (slopesx) |x| { 
        for (slopesy) |y| {
            const arr = [_]usize{ slope(x, y) };
            const bigint = int.Const{ .limbs = &arr, .positive = true };
            try bigboy.mul(bigboy.toConst(), bigint);
        }
    }

    std.debug.print("bigboy: {}\n", .{ try bigboy.toString(a, 10, false) });
}