~cricket/advent2020

6cbea23e5be7abbada92149f034287378f981a6e — cricket 9 months ago 2d1384c
compactify
1 files changed, 10 insertions(+), 36 deletions(-)

M 3/main.zig
M 3/main.zig => 3/main.zig +10 -36
@@ 1,48 1,22 @@
const std = @import("std");
const input = @embedFile("input");
const fmt = std.fmt;
const mem = std.mem;
const print = std.debug.print;
const w = 32;

fn array_list(a: *mem.Allocator) !std.ArrayList([]const u8) {
    var strings = mem.split(input, "\n");
    var list = std.ArrayList([]const u8).init(a);
    while (strings.next()) |string| {
        try list.append(string);
    }
    return list;
}

fn slope(list: [][]const u8, slopex: usize, slopey: usize) usize {
fn slope(sx: usize, sy: usize) usize {
    var trees: usize = 0;
    var x: usize = 0;
    var y: usize = 0;

    while (y < list.len) {
        trees += @boolToInt(list[y][@mod(x, 31)] == '#');
        y += slopey;
        x += slopex;
    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;
}

fn part1(list: [][]const u8) usize { slope(list, 3, 1); }
fn part2(list: [][]const u8) usize {
    var pt1 = slope(list, 1, 1);
    var pt2 = slope(list, 3, 1);
    var pt3 = slope(list, 5, 1);
    var pt4 = slope(list, 7, 1);
    var pt5 = slope(list, 1, 2);

    return pt1 * pt2 * pt3 * pt4 * pt5;
}

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const a = &gpa.allocator;
    var lines = try array_list(a);
    const list = lines.items;
pub fn main() void {
    const slopes = ([_][2]u8{.{ 1, 1 }, .{ 3, 1 }, .{ 5, 1 }, .{ 7, 1 }, .{ 1, 2 }})[0..];
    var part1 = slope(3, 1);
    var part2: usize = 1; for (slopes) |s| part2 *= slope(s[0], s[1]);

    print("part 1: {}\npart 2: {}\n", .{part1(list), part2(list)});
    std.debug.print("part 1: {}\npart 2: {}\n", .{ part1, part2 });
}
\ No newline at end of file