-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
636 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.