~cricket/advent2020

84f7d5e11fcda7f69f437a29d0f30255965931d7 — cricket 2 months ago 6c216ad
day 3: add bigboy solution; make main solution stupid compact
2 files changed, 41 insertions(+), 11 deletions(-)

A 3/bigboy.zig
M 3/main.zig
A 3/bigboy.zig => 3/bigboy.zig +37 -0
@@ 0,0 1,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) });
}

M 3/main.zig => 3/main.zig +4 -11
@@ 3,20 3,13 @@ const input = @embedFile("input");
const w = 32;

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) : ({ y += sy; x += sx; i = (w * y) + @mod(x, w-1); }) {
        trees += @boolToInt(input[i] == '#');
    }
    return trees;
    var t: usize = 0; var x: usize = 0; var y: usize = 0; var i: usize = 0;
    while (i < input.len) : ({ y += sy; x += sx; i = (w * y) + @mod(x, w-1); }) t += @boolToInt(input[i] == '#');
    return t;
}

pub fn main() void {
    const slopes = [_][2]u8{.{ 1, 1 }, .{ 3, 1 }, .{ 5, 1 }, .{ 7, 1 }, .{ 1, 2 }};
    var part1 = slope(3, 1);
    var part2: usize = 1; for (slopes) |s| part2 *= slope(s[0], s[1]);

    var part2: usize = 1; for ([_][2]u8{.{ 1, 1 }, .{ 3, 1 }, .{ 5, 1 }, .{ 7, 1 }, .{ 1, 2 }}) |s| part2 *= slope(s[0], s[1]);
    std.debug.print("part 1: {}\npart 2: {}\n", .{ part1, part2 });
}
\ No newline at end of file