Day3 Part 2

main
Adam Millerchip 4 months ago
parent 6bf0d44e09
commit 9a81f47677
  1. 56
      2020/zig/src/day03.zig

@ -1,12 +1,9 @@
const std = @import("std");
const util = @import("util.zig");
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
const Map = std.AutoHashMap;
const StrMap = std.StringHashMap;
const BitSet = std.DynamicBitSet;
const Str = []const u8;
const util = @import("util.zig");
const tokenize = std.mem.tokenize;
const print = std.debug.print;
const gpa = util.gpa;
const data = @embedFile("input");
@ -25,44 +22,25 @@ pub fn main() !void {
}
var rows = rows_al.toOwnedSlice();
var part_1 = count_trees(rows, 3, 1);
print("Part 1: {}\n", .{part_1});
var part_2 = count_trees(rows, 1, 1) * part_1 * count_trees(rows, 5, 1) * count_trees(rows, 7, 1) * count_trees(rows, 1, 2);
print("Part 2: {}\n", .{part_2});
}
fn count_trees(slope: [][]bool, delta_x: usize, delta_y: usize) usize {
var x: usize = 0;
var y: usize = 0;
var count: usize = 0;
while (y < rows.len) {
var square = rows[y][x];
while (y < slope.len) {
var square = slope[y][x];
if (square) {
count += 1;
}
x = try std.math.mod(usize, x + 3, rows[y].len);
y += 1;
x = std.math.mod(usize, x + delta_x, slope[y].len) catch 0;
y += delta_y;
}
print("{}", .{count});
return count;
}
// Useful stdlib functions
const tokenize = std.mem.tokenize;
const split = std.mem.split;
const indexOf = std.mem.indexOfScalar;
const indexOfAny = std.mem.indexOfAny;
const indexOfStr = std.mem.indexOfPosLinear;
const lastIndexOf = std.mem.lastIndexOfScalar;
const lastIndexOfAny = std.mem.lastIndexOfAny;
const lastIndexOfStr = std.mem.lastIndexOfLinear;
const trim = std.mem.trim;
const sliceMin = std.mem.min;
const sliceMax = std.mem.max;
const parseInt = std.fmt.parseInt;
const parseFloat = std.fmt.parseFloat;
const min = std.math.min;
const min3 = std.math.min3;
const max = std.math.max;
const max3 = std.math.max3;
const print = std.debug.print;
const assert = std.debug.assert;
const sort = std.sort.sort;
const asc = std.sort.asc;
const desc = std.sort.desc;

Loading…
Cancel
Save