Skip to content

Commit

Permalink
add solutions for 2015/06
Browse files Browse the repository at this point in the history
  • Loading branch information
demiazz committed Nov 14, 2024
1 parent 4f7ebaa commit 2a7e42e
Show file tree
Hide file tree
Showing 9 changed files with 636 additions and 108 deletions.
35 changes: 35 additions & 0 deletions IDEAS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
### 2015/06:

There are very raw implementation with too much iterations. But what if we
can build search tree for rectangles, and just go through the whole grid the
only once, and then apply changes per point from all rectanges at once?
Would it be faster? Or building the search tree will be much complicated than
performance gains here?

```zig
fn part_one() u32 {
var count: u32 = 0;
for (0..grid_size) |x| {
for (0..grid_size) |y| {
const it = findRectsAt(x, y);
var state = false;
while (it.next()) |action| {
switch (action) {
.on => state = true,
.off => state = false,
.toggle => state = !state;
}
}
if (state) {
count += 1
}
}
}
return count;
}
```
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
YEAR=2015
DAY=6
PART=1
PART=2

.PHONY: build-native build-web build dev-native dev-web

Expand Down
1 change: 1 addition & 0 deletions native/solutions/2015/05/part_2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const AnyReader = std.io.AnyReader;
const LineParser = tools.LineParser;

const Pairs = std.hash_map.AutoHashMap([2]u8, usize);

const Overlap = struct {
byte: u8,
index: usize,
Expand Down
90 changes: 90 additions & 0 deletions native/solutions/2015/06/common.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const std = @import("std");
const tools = @import("tools");

const Allocator = std.mem.Allocator;
const AnyReader = std.io.AnyReader;
const LineParser = tools.LineParser;

pub const Action = enum { on, off, toggle };

pub const Command = struct {
action: Action,

from_x: usize,
from_y: usize,

to_x: usize,
to_y: usize,
};

const Iterator = std.mem.SplitIterator(u8, .any);

fn parseAction(it: *Iterator) !Action {
const prelude = it.first();

if (std.mem.eql(u8, prelude, "toggle")) {
return .toggle;
}

if (!std.mem.eql(u8, prelude, "turn")) {
return error.InvalidInput;
}

const token = it.next() orelse "";

if (std.mem.eql(u8, token, "on")) {
return .on;
}

if (std.mem.eql(u8, token, "off")) {
return .off;
}

return error.InvalidInput;
}

fn checkThrough(it: *Iterator) !void {
const token = it.next() orelse "";

if (!std.mem.eql(u8, token, "through")) {
return error.InvalidInput;
}
}

fn parseValue(it: *Iterator) !usize {
const token = it.next() orelse "";

if (token.len > 3) {
return error.InvalidInput;
}

return std.fmt.parseInt(usize, token, 10) catch {
return error.InvalidInput;
};
}

fn parse(line: []const u8, _: Allocator) !Command {
var it = std.mem.splitAny(u8, line, " ,");

const action = try parseAction(&it);

const from_x = try parseValue(&it);
const from_y = try parseValue(&it);

try checkThrough(&it);

const to_x = try parseValue(&it);
const to_y = try parseValue(&it);

return .{
.action = action,

.from_x = from_x,
.from_y = from_y,

.to_x = to_x,
.to_y = to_y,
};
}

pub const Parser = LineParser(Command, parse);
Loading

0 comments on commit 2a7e42e

Please sign in to comment.