advent-of-code

Solutions for Advent of Code.
git clone git://git.amin.space/advent-of-code.git
Log | Files | Refs | LICENSE

commit 7f14e603d8cb16776ae5c789bf1fad9b9c328ac9
parent 8d4848d3260121fadcdeda4ae90c8a330f65634f
Author: amin <dev@aminmesbah.com>
Date:   Sat,  1 Dec 2018 19:59:31 +0000

Add solution for 01-2

FossilOrigin-Name: 60927fe14d9add066d04bb3afa77d78232a4dae7b63524cff23de83220005b4d
Diffstat:
Mmain.zig | 49++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 46 insertions(+), 3 deletions(-)

diff --git a/main.zig b/main.zig @@ -7,10 +7,11 @@ const os = std.os; pub fn main() !void { var allocator = &std.heap.DirectAllocator.init().allocator; - var input = try get_file_contents(allocator, "input_01.txt"); - defer allocator.free(input); + var input01 = try get_file_contents(allocator, "input_01.txt"); + defer allocator.free(input01); - debug.warn("01-1: {}\n", total_sum(input)); + debug.warn("01-1: {}\n", total_sum(input01)); + debug.warn("01-2: {}\n", first_visited_twice(input01)); } fn total_sum(input: []const u8) !i32 { @@ -35,6 +36,48 @@ test "total_sum" { debug.assert(41 == try total_sum(s)); } +fn first_visited_twice(input: []const u8) !i32 { + var sum: i32 = 0; + var index: usize = 0; + + const visited_magnitude: usize = 1000000; + const visited_size: usize = (visited_magnitude * 2) + 1; + // [ -visited_magnitude, ..., -3, -2, -1, 0, 1, 2, 3, ..., visited_magnitude ] + var visited = []bool{false} ** visited_size; + + //debug.warn("{} ", sum); + while (true) { + var visited_index = @intCast(usize, @intCast(i32, visited_magnitude) + sum); + debug.assert(visited_index >= 0); + if (visited[visited_index] == true) { + return sum; + } else { + visited[visited_index] = true; + } + + var e = index; + while (input[e] != '\n') { + e += 1; + } + debug.assert('\n' == input[e]); + + var num = try fmt.parseInt(i32, input[index..e], 10); + sum += num; + //debug.warn("+ {}\n", num); + //debug.warn("{} ", sum); + index = (e + 1) % input.len; + } + debug.warn("\n---\n"); + return sum; +} + +test "first_visited_twice" { + debug.assert(0 == try first_visited_twice("+1\n-1\n")); + debug.assert(10 == try first_visited_twice("+3\n+3\n+4\n-2\n-4\n")); + debug.assert(5 == try first_visited_twice("-6\n+3\n+8\n+5\n-6\n")); + debug.assert(14 == try first_visited_twice("+7\n+7\n-2\n-7\n-4\n")); +} + fn get_file_contents(allocator: *mem.Allocator, file_name: []const u8) ![]u8 { var file = try os.File.openRead(file_name); defer file.close();