aoc-2020-zig

Advent of Code 2020 Solutions in Zig
git clone https://git.sinitax.com/sinitax/aoc-2020-zig
Log | Files | Refs | README | sfeed.txt

main.zig (2056B)


      1const std = @import("std");
      2const aoc = @import("aoc");
      3
      4const Entry = []u1;
      5const Vector = struct { x: u16, y: u16 };
      6
      7fn freeEntries(allocator: std.mem.Allocator, entries: *const std.ArrayList(Entry)) void {
      8    for (entries.items) |entry| {
      9        allocator.free(entry);
     10    }
     11    entries.deinit();
     12}
     13
     14fn parse(allocator: std.mem.Allocator, input: []u8) !std.ArrayList(Entry) {
     15    var entries = std.ArrayList(Entry).init(allocator);
     16    errdefer freeEntries(allocator, &entries);
     17
     18    var lineit = std.mem.tokenize(u8, input, "\n");
     19    while (lineit.next()) |line| {
     20        var linemap: []u1 = try allocator.alloc(u1, line.len);
     21        errdefer allocator.free(linemap);
     22
     23        for (line) |c, i| {
     24            linemap[i] = @boolToInt(c == '#');
     25        }
     26        try entries.append(linemap);
     27    }
     28
     29    return entries;
     30}
     31
     32fn treesHit(map: std.ArrayList(Entry), slope: Vector) u16 {
     33    var count: u16 = 0;
     34    var pos = Vector{ .x = 0, .y = 0 };
     35    while (pos.y < map.items.len - 1) {
     36        pos.x += slope.x;
     37        pos.y += slope.y;
     38
     39        count += map.items[pos.y][pos.x % map.items[0].len];
     40    }
     41    return count;
     42}
     43
     44fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 {
     45    _ = args;
     46
     47    const map = try parse(allocator, input);
     48    defer freeEntries(allocator, &map);
     49
     50    const answer = treesHit(map, Vector{ .x = 3, .y = 1 });
     51
     52    return try std.fmt.allocPrint(allocator, "{d}", .{answer});
     53}
     54
     55fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 {
     56    _ = args;
     57
     58    const map = try parse(allocator, input);
     59    defer freeEntries(allocator, &map);
     60
     61    const slopes = [_]Vector{
     62        Vector{ .x = 1, .y = 1 },
     63        Vector{ .x = 3, .y = 1 },
     64        Vector{ .x = 5, .y = 1 },
     65        Vector{ .x = 7, .y = 1 },
     66        Vector{ .x = 1, .y = 2 },
     67    };
     68
     69    var answer: u32 = 1;
     70    for (slopes) |slope| {
     71        answer *= treesHit(map, slope);
     72    }
     73
     74    return try std.fmt.allocPrint(allocator, "{d}", .{answer});
     75}
     76
     77pub const main = aoc.main(part1, part2, .{ "282", "958815792" });