Compare commits

...

2 Commits

Author SHA1 Message Date
Adam Millerchip 9a81f47677 Day3 Part 2 4 months ago
Adam Millerchip 6bf0d44e09 Day3 part 1 4 months ago
  1. 68
      2020/zig/src/day03.zig

@ -1,44 +1,46 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const List = std.ArrayList;
const Map = std.AutoHashMap;
const StrMap = std.StringHashMap;
const BitSet = std.DynamicBitSet;
const Str = []const u8;
const util = @import("util.zig");
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
const tokenize = std.mem.tokenize;
const print = std.debug.print;
const gpa = util.gpa;
const data = @embedFile("../data/day03.txt");
const data = @embedFile("input");
pub fn main() !void {
}
// 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;
// Still have no idea how to clean up the ArrayLists
var it = tokenize(u8, data, "\n");
var rows_al = ArrayList([]bool).init(gpa);
while (it.next()) |line| {
var row = ArrayList(bool).init(gpa);
for (line) |char| {
try row.append(char == '#');
}
try rows_al.append(row.toOwnedSlice());
}
var rows = rows_al.toOwnedSlice();
const parseInt = std.fmt.parseInt;
const parseFloat = std.fmt.parseFloat;
var part_1 = count_trees(rows, 3, 1);
print("Part 1: {}\n", .{part_1});
const min = std.math.min;
const min3 = std.math.min3;
const max = std.math.max;
const max3 = std.math.max3;
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);
const print = std.debug.print;
const assert = std.debug.assert;
print("Part 2: {}\n", .{part_2});
}
const sort = std.sort.sort;
const asc = std.sort.asc;
const desc = std.sort.desc;
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 < slope.len) {
var square = slope[y][x];
if (square) {
count += 1;
}
x = std.math.mod(usize, x + delta_x, slope[y].len) catch 0;
y += delta_y;
}
return count;
}

Loading…
Cancel
Save