}
insert :: (use set: ^Set($T), value: T) {
+ if hashes.data == null do init(set);
lr := lookup(set, value);
if lr.entry_index >= 0 do return;
}
clear :: (use set: ^Set($T)) {
- array.fill(^hashes, -1);
+ array.fill(hashes, -1);
array.clear(^entries);
}
return entries.count == 0;
}
+#match (package core.iter).as_iterator iterator
iterator :: (set: ^Set($T)) -> Iterator(T) {
Context :: struct (T: type_expr) {
- entry_array: [..] Set.Entry(T);
+ set: ^Set(T);
position: i32;
}
context := new(Context(T));
- context.entry_array = set.entries;
+ context.set = set;
context.position = 0;
next :: ($T: type_expr, use context: ^Context(T)) -> (T, bool) {
- if position < entry_array.count {
+ if position < set.entries.count {
defer position += 1;
- return entry_array[position].value, true;
+ return set.entries[position].value, true;
} else {
return __zero_value(T), false;
--- /dev/null
+#load "core/std"
+
+use package core
+
+Pos :: struct {x, y:i32;}
+#match hash.to_u32 (use p: Pos) => x * 45238271 + y * 34725643;
+#operator == (p1, p2: Pos) => p1.x == p2.x && p1.y == p2.y;
+
+main :: (args) => {
+ for file: os.with_file("./tests/aoc-2021/input/day11.txt") {
+ reader := io.reader_make(file);
+
+ octopuses: [..] u32;
+ while !io.reader_empty(^reader) {
+ line := io.read_line(^reader, consume_newline=false, inplace=true);
+ io.skip_whitespace(^reader);
+
+ for ch: line do octopuses << ~~(ch - #char "0");
+ }
+
+ get_octopus :: macro (x, y) => {
+ if x < 0 || y < 0 || x >= 10 || y >= 10 do return -1;
+ return octopuses[y * 10 + x];
+ }
+
+ set_octopus :: macro (x, y, v: i32) {
+ if !(x < 0 || y < 0 || x >= 10 || y >= 10) {
+ octopuses[y * 10 + x] = v;
+ }
+ }
+
+ inc_octopus :: macro (x, y) => {
+ if x < 0 || y < 0 || x >= 10 || y >= 10 do return -1;
+ octopuses[y * 10 + x] += 1;
+ return octopuses[y * 10 + x];
+ }
+
+ flash_count := 0;
+
+ step := 0;
+ sync_step := 0;
+ while true {
+ step += 1;
+ for ^o: octopuses do *o += 1;
+
+ #persist to_flash: Set(Pos);
+ for y: 10 do for x: 10 {
+ if get_octopus(x, y) >= 10 {
+ to_flash << .{x, y};
+ }
+ }
+
+ for flash: iter.as_iterator(^to_flash) {
+ for y: -1 .. 2 do for x: -1 .. 2 {
+ if y == 0 && x == 0 do continue;
+
+ if inc_octopus(flash.x + x, flash.y + y) >= 10 {
+ to_flash << .{flash.x + x, flash.y + y};
+ }
+ }
+ }
+
+ for flash: iter.as_iterator(^to_flash) {
+ set_octopus(flash.x, flash.y, 0);
+ if step <= 100 do flash_count += 1;
+ }
+
+ if to_flash.entries.count == 100 {
+ sync_step = step;
+ break;
+ }
+
+ to_flash->clear();
+ }
+
+ printf("Part 1: {}\n", flash_count);
+ printf("Part 2: {}\n", sync_step);
+ }
+}
\ No newline at end of file