advent-of-code

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

commit e64ed476bd0ead0c4ef9ff54ed52196681f56bb2
parent 5054b0ba34e29840791aef08a8fc6c5f378b822f
Author: amin <dev@aminmesbah.com>
Date:   Sun,  2 Dec 2018 06:33:07 +0000

Add solution to 02-2

FossilOrigin-Name: 5f3ce2a6ac5f07eb34541594f543608d44ac15d43b28cd9c25c060dd9fd3b9f4
Diffstat:
Mmain.zig | 45+++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 45 insertions(+), 0 deletions(-)

diff --git a/main.zig b/main.zig @@ -13,6 +13,51 @@ pub fn main() !void { debug.warn("01-1: {}\n", total_sum(input01)); debug.warn("01-2: {}\n", first_visited_twice(input01)); debug.warn("02-1: {}\n", checksum(input_02)); + common_chars(input_02); +} + +fn common_chars(ids: [] const [] const u8) void { + for (ids) |id, i| { + outer: for (ids[i + 1..]) |id2, j| { + if (i != j) { + debug.assert(id.len == id2.len); + var distance: u32 = 0; + for (id) |char, k| { + if (char != id2[k]) { + distance += 1; + } + if (distance > 1) { + continue :outer; + } + } + if (distance == 1) { + //debug.warn("{}\n", id); + //debug.warn("{}\n", id2); + for (id) |char, x| { + if (char == id2[x]) { + debug.warn("{c}", char); + } + } + debug.warn("\n"); + + break; + } + } + } + } +} + +test "common chars" { + const ids = [] const [] const u8 { + "abcde", + "fghij", + "klmno", + "pqrst", + "fguij", + "axcye", + "wvxyz", + }; + common_chars(ids); } fn checksum(input: [] const [] const u8) u32{