~cricket/advent2020

c29a5fd74c7943ea779c311bd4fdef1ede2402ab — cricket 2 months ago 9a3d56c
day 8: small cleanup

* i'll clean it up a lot later lol
1 files changed, 29 insertions(+), 18 deletions(-)

M 8/main.zig
M 8/main.zig => 8/main.zig +29 -18
@@ 4,7 4,7 @@ const mem = std.mem;
const fmt = std.fmt;
const print = std.debug.print;

const Instruction = struct{ op: *const [3]u8, num: isize };
const Instruction = struct { op: *const [3]u8, num: isize };

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const a = &gpa.allocator;


@@ 14,39 14,50 @@ var acc: isize = 0;

fn part1(num: isize) void {
    const instruction = map.get(num) orelse return;
    if(ran.contains(num)) return;
    if (ran.contains(num)) return;
    ran.put(num, true) catch return;
    switch(instruction.op[0]) {
        'a' => { acc = acc + instruction.num; return part1(num + 1); },
    switch (instruction.op[0]) {
        'a' => {
            acc += instruction.num;
            return part1(num + 1);
        },
        'j' => return part1(num + instruction.num),
        'n' => return part1(num+1),
        else => unreachable
        'n' => return part1(num + 1),
        else => unreachable,
    }
    return;
}

fn isLoop(num: isize) bool {
    const instruction = map.get(num) orelse return false;
    if(ran.contains(num)) return true;
    if (ran.contains(num)) return true;
    ran.put(num, true) catch return false; // ??
    switch(instruction.op[0]) {
        'a' => { acc = acc + instruction.num; return isLoop(num + 1); },
    switch (instruction.op[0]) {
        'a' => {
            acc += instruction.num;
            return isLoop(num + 1);
        },
        'j' => return isLoop(num + instruction.num),
        'n' => return isLoop(num + 1),
        else => unreachable
        else => unreachable,
    }
}

fn part2(num: isize) void {
    ran.clearAndFree(); acc = 0;
    ran.clearAndFree();
    acc = 0;
    const instruction = map.get(num) orelse return;
    switch(instruction.op[0]) {
        'a' => part2(num+1),
    switch (instruction.op[0]) {
        'a' => part2(num + 1),
        'j', 'n' => {
            map.put(num, .{ .op = if(instruction.op[0] == 'j') "nop" else "jmp", .num = instruction.num }) catch unreachable;
            if(isLoop(1)) { map.put(num, instruction) catch unreachable; part2(num+1); } else return;
            const op = if (instruction.op[0] == 'j') "nop" else "jmp";
            map.put(num, .{ .op = op, .num = instruction.num }) catch unreachable;
            if (isLoop(1)) {
                map.put(num, instruction) catch unreachable;
                part2(num + 1);
            } else return;
        },
        else => unreachable
        else => unreachable,
    }
}



@@ 59,7 70,7 @@ pub fn main() !void {
        const num = try fmt.parseInt(isize, str[4..str.len], 0);

        i += 1;
        try map.put(i, .{.op = op, .num = num});
        try map.put(i, .{ .op = op, .num = num });
    }

    part1(1);


@@ 67,4 78,4 @@ pub fn main() !void {

    part2(1);
    print("{}\n", .{acc});
}
}
\ No newline at end of file