~cricket/advent2020

ref: 6c216adde7ed7af08c63ab702b900cb2f1e142bb advent2020/3/main.zig -rw-r--r-- 639 bytes
6c216addcricket dont slice the array literal 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
const std = @import("std");
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;
}

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]);

    std.debug.print("part 1: {}\npart 2: {}\n", .{ part1, part2 });
}