aoc-2020-zig

git clone https://git.sinitax.com/sinitax/aoc-2020-zig
Log | Files | Refs | README | sfeed.txt

commit fc7bffb0279093722e5c619fc19629f41410580e
parent 46260c9b3a28452c9e80bb809c1f4c3e0fe83f99
Author: Louis Burda <quent.burda@gmail.com>
Date:   Thu,  3 Dec 2020 22:01:23 +0100

added zig version used and fixed passing arraylist potentially by value

Diffstat:
MREADME | 1+
Msrc/day1/main.zig | 7++++---
Msrc/day2/main.zig | 8++++----
Msrc/day3/main.zig | 17+++++++++--------
4 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/README b/README @@ -3,3 +3,4 @@ Advent Of Code 2020 Solutions to the annual [coding advent calendar](https://adventofcode.com) written in [Zig](https://ziglang.org/). +The version of zig used to compile to code is: `0.7.0+d4c167f3c` diff --git a/src/day1/main.zig b/src/day1/main.zig @@ -4,7 +4,8 @@ const aoc = @import("aoc"); const Parts = struct { a: u32, b: u32 }; -fn findparts(intlist: std.ArrayList(u32), sum: u32) ?Parts { + +fn findparts(intlist: *const std.ArrayList(u32), sum: u32) ?Parts { var start: usize = 0; const items = intlist.items; var end: usize = items.len - 1; @@ -39,7 +40,7 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { std.sort.sort(u32, intlist.items, {}, comptime std.sort.asc(u32)); - if (findparts(intlist, 2020)) |parts| { + if (findparts(&intlist, 2020)) |parts| { std.debug.print("{}\n", .{parts.a * parts.b}); } } @@ -55,7 +56,7 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { while (third < intlist.items.len) : (third += 1) { const tmp = intlist.items[third]; intlist.items[third] = target + 1; - if (findparts(intlist, target - tmp)) |parts| { + if (findparts(&intlist, target - tmp)) |parts| { std.debug.print("{}\n", .{parts.a * parts.b * tmp}); return; } diff --git a/src/day2/main.zig b/src/day2/main.zig @@ -5,7 +5,7 @@ const Entry = struct { a: u16, b: u16, char: u8, pass: []u8 }; -fn deinit_entries(allocator: *std.mem.Allocator, entries: std.ArrayList(Entry)) void { +fn freeEntries(allocator: *std.mem.Allocator, entries: *const std.ArrayList(Entry)) void { for (entries.items) |item| allocator.free(item.pass); entries.deinit(); @@ -13,7 +13,7 @@ fn deinit_entries(allocator: *std.mem.Allocator, entries: std.ArrayList(Entry)) fn parse(allocator: *std.mem.Allocator, input: []u8) !std.ArrayList(Entry) { var entries = std.ArrayList(Entry).init(allocator); - errdefer deinit_entries(allocator, entries); + errdefer freeEntries(allocator, &entries); var lineit = std.mem.tokenize(input, "\n"); while (lineit.next()) |line| { @@ -31,7 +31,7 @@ fn parse(allocator: *std.mem.Allocator, input: []u8) !std.ArrayList(Entry) { fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { const entries = try parse(allocator, input); - defer deinit_entries(allocator, entries); + defer freeEntries(allocator, &entries); var valid: u32 = 0; for (entries.items) |entry| { @@ -44,7 +44,7 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { const entries = try parse(allocator, input); - defer deinit_entries(allocator, entries); + defer freeEntries(allocator, &entries); var valid: u32 = 0; for (entries.items) |entry| { diff --git a/src/day3/main.zig b/src/day3/main.zig @@ -6,15 +6,16 @@ const Vector = struct { x: u16, y: u16 }; -fn free_entries(allocator: *std.mem.Allocator, entries: std.ArrayList(Entry)) void { - for (entries.items) |entry| +fn freeEntries(allocator: *std.mem.Allocator, entries: *const std.ArrayList(Entry)) void { + for (entries.items) |entry| { allocator.free(entry); + } entries.deinit(); } fn parse(allocator: *std.mem.Allocator, input: []u8) !std.ArrayList(Entry) { var entries = std.ArrayList(Entry).init(allocator); - errdefer free_entries(allocator, entries); + errdefer freeEntries(allocator, &entries); var lineit = std.mem.tokenize(input, "\n"); while (lineit.next()) |line| { @@ -30,7 +31,7 @@ fn parse(allocator: *std.mem.Allocator, input: []u8) !std.ArrayList(Entry) { return entries; } -fn trees_hit(map: std.ArrayList(Entry), slope: Vector) u16 { +fn treesHit(map: std.ArrayList(Entry), slope: Vector) u16 { var count: u16 = 0; var pos = Vector{ .x = 0, .y = 0 }; while (pos.y < map.items.len - 1) { @@ -44,15 +45,15 @@ fn trees_hit(map: std.ArrayList(Entry), slope: Vector) u16 { fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { const map = try parse(allocator, input); - defer free_entries(allocator, map); + defer freeEntries(allocator, &map); - const answer = trees_hit(map, Vector{ .x = 3, .y = 1 }); + const answer = treesHit(map, Vector{ .x = 3, .y = 1 }); std.debug.print("{}\n", .{answer}); } fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { const map = try parse(allocator, input); - defer free_entries(allocator, map); + defer freeEntries(allocator, &map); var answer: u32 = 1; const slopes = [_]Vector{ @@ -63,7 +64,7 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { Vector{ .x = 1, .y = 2 }, }; for (slopes) |slope| { - answer *= trees_hit(map, slope); + answer *= treesHit(map, slope); } std.debug.print("{}\n", .{answer});