defer cfree(stream);
nums := array.make(u32, 128);
- defer array.free(^nums);
+ defer array.free(&nums);
while num := 1; num > 0 {
// This sets num to be 0 if there is no number
- num = io.read_u32(^reader);
- if num != 0 do array.push(^nums, num);
+ num = io.read_u32(&reader);
+ if num != 0 do array.push(&nums, num);
}
for i: nums do for j: nums do for k: nums {
count_ending_paths :: (nums: [..] u32) -> u64 {
tally := array.make(u64, nums.count);
- defer array.free(^tally);
+ defer array.free(&tally);
- for ^t: tally do *t = 0;
+ for &t: tally do *t = 0;
tally[nums.count - 1] = 1;
while i := cast(i32) (nums.count - 2); i >= 0 {
file := contents;
nums := array.make(u32);
- defer array.free(^nums);
+ defer array.free(&nums);
while !string.empty(file) {
- array.push(^nums, ~~ conv.parse_int(^file));
- string.advance_line(^file);
+ array.push(&nums, ~~ conv.parse_int(&file));
+ string.advance_line(&file);
}
// Slight hack, but having 0 in the array makes both parts easier
- array.push(^nums, 0);
+ array.push(&nums, 0);
array.sort(nums, (a, b) => a - b);
diffs: [3] u32;
- for ^d: diffs do *d = 0;
+ for &d: diffs do *d = 0;
for i: 1 .. nums.count do diffs[nums[i] - nums[i - 1] - 1] += 1;
diffs[2] += 1;
SeatState :: enum (u8) { OOB; Floor; Empty; Occupied; }
-gos_get_seat :: (use gos: ^GameOfSeats, x: i32, y: i32) -> SeatState {
+gos_get_seat :: (use gos: &GameOfSeats, x: i32, y: i32) -> SeatState {
if x < 0 || y < 0 || x >= width || y >= height do return SeatState.OOB;
return seats[x + y * width];
}
-gos_neighbors :: (use gos: ^GameOfSeats, x: i32, y: i32, state := SeatState.Occupied) -> u32 {
+gos_neighbors :: (use gos: &GameOfSeats, x: i32, y: i32, state := SeatState.Occupied) -> u32 {
count := 0;
for dy: -1 .. 2 {
return count;
}
-gos_iter :: (use gos: ^GameOfSeats) -> bool {
+gos_iter :: (use gos: &GameOfSeats) -> bool {
for i: 0 .. seats.count do temp[i] = seats[i];
changed := false;
file := contents;
gos : GameOfSeats;
- array.init(^gos.seats, 32);
- defer array.free(^gos.seats);
+ array.init(&gos.seats, 32);
+ defer array.free(&gos.seats);
gos.height = 0;
while !string.empty(file) {
line, file' := string.bisect(file, #char "\n");
for ch: line do switch ch {
- case #char "." do array.push(^gos.seats, SeatState.Floor);
- case #char "L" do array.push(^gos.seats, SeatState.Empty);
- case #char "#" do array.push(^gos.seats, SeatState.Occupied);
+ case #char "." do array.push(&gos.seats, SeatState.Floor);
+ case #char "L" do array.push(&gos.seats, SeatState.Empty);
+ case #char "#" do array.push(&gos.seats, SeatState.Occupied);
}
gos.width = line.count;
gos.height += 1;
}
- array.init(^gos.temp, gos.seats.count);
- defer array.free(^gos.temp);
+ array.init(&gos.temp, gos.seats.count);
+ defer array.free(&gos.temp);
- while gos_iter(^gos) ---
+ while gos_iter(&gos) ---
occupied := 0;
for s: gos.seats do if s == SeatState.Occupied do occupied += 1;
fy : i32 = 0;
}
-rotate_left :: (ship: ^Ship) {
+rotate_left :: (ship: &Ship) {
ofx := ship.fx;
ofy := ship.fy;
ship.fy = -ofx;
}
-rotate_right :: (ship: ^Ship) {
+rotate_right :: (ship: &Ship) {
ofx := ship.fx;
ofy := ship.fy;
ship.fy = ofx;
}
-turn_around :: (ship: ^Ship) {
+turn_around :: (ship: &Ship) {
ship.fx = -ship.fx;
ship.fy = -ship.fy;
}
ship := Ship.{ fx = 10, fy = -1 };
while !string.empty(file) {
dir := file[0];
- string.advance(^file, 1);
+ string.advance(&file, 1);
- val := cast(u32, conv.parse_int(^file));
- string.advance_line(^file);
+ val := cast(u32, conv.parse_int(&file));
+ string.advance_line(&file);
switch dir {
case #char "N" do ship.fy -= val;
}
case #char "L" do switch val {
- case 90 do rotate_left(^ship);
- case 180 do turn_around(^ship);
- case 270 do rotate_right(^ship);
+ case 90 do rotate_left(&ship);
+ case 180 do turn_around(&ship);
+ case 270 do rotate_right(&ship);
}
case #char "R" do switch val {
- case 90 do rotate_right(^ship);
- case 180 do turn_around(^ship);
- case 270 do rotate_left(^ship);
+ case 90 do rotate_right(&ship);
+ case 180 do turn_around(&ship);
+ case 270 do rotate_left(&ship);
}
}
}
file := contents;
- est := cast(u32, conv.parse_int(^file));
- string.advance_line(^file);
+ est := cast(u32, conv.parse_int(&file));
+ string.advance_line(&file);
buses := array.make(i64);
- defer array.free(^buses);
+ defer array.free(&buses);
rems := array.make(i64);
- defer array.free(^rems);
+ defer array.free(&rems);
offset: i64 = 0;
while !string.empty(file) {
if *file.data == #char "x" {
- string.advance(^file, 2);
+ string.advance(&file, 2);
} else {
- bus := conv.parse_int(^file);
- array.push(^buses, ~~bus);
- array.push(^rems, bus - offset);
+ bus := conv.parse_int(&file);
+ array.push(&buses, ~~bus);
+ array.push(&rems, bus - offset);
- string.advance(^file, 1);
+ string.advance(&file, 1);
}
offset += 1;
}
// Part 1
- // min := array.fold(^buses, 0xffffffff, math.min);
+ // min := array.fold(&buses, 0xffffffff, math.min);
// take_bus := 0;
// depart := 0;
bitmask_p2 :: (mask: Bitmask, val: u64) -> Iterator(u64) {
iterator_next :: (data: rawptr) -> (u64, bool) {
- bmi := cast(^BitmaskIter) data;
+ bmi := cast(&BitmaskIter) data;
if bmi.done do return 0, false;
for ind: bmi.floating_indicies {
}
iterator_close :: (data: rawptr) {
- bmi := cast(^BitmaskIter) data;
- array.free(^bmi.floating_indicies);
+ bmi := cast(&BitmaskIter) data;
+ array.free(&bmi.floating_indicies);
cfree(bmi);
}
bmi := new(BitmaskIter);
bmi.done = false;
- array.init(^bmi.floating_indicies, 8);
+ array.init(&bmi.floating_indicies, 8);
v := val;
for i: 0 .. MASK_SIZE {
if mask[i] == 2 {
v &= ~(1 << cast(u64) i);
- array.push(^bmi.floating_indicies, cast(u8) i);
+ array.push(&bmi.floating_indicies, cast(u8) i);
}
}
file := contents;
mem := map.make(u64, u64, default=0);
- defer map.free(^mem);
+ defer map.free(&mem);
mask : Bitmask;
- for ^m: mask do *m = 2;
+ for &m: mask do *m = 2;
while !string.empty(file) {
- word := string.read_alphanum(^file);
+ word := string.read_alphanum(&file);
if string.equal(word, "mask") {
- string.advance(^file, 3);
+ string.advance(&file, 3);
i := 35;
m, file' := string.bisect(file, #char "\n");
}
}
elseif string.equal(word, "mem") {
- string.advance(^file, 1);
+ string.advance(&file, 1);
- addr := cast(u64, conv.parse_int(^file));
- string.advance(^file, 4);
+ addr := cast(u64, conv.parse_int(&file));
+ string.advance(&file, 4);
- val := cast(u64, conv.parse_int(^file));
+ val := cast(u64, conv.parse_int(&file));
// Part 1
- // map.put(^mem, addr, bitmask_p1(mask, val));
+ // map.put(&mem, addr, bitmask_p1(mask, val));
// Part 2
for real_addr: bitmask_p2(mask, addr) {
- map.put(^mem, real_addr, val);
+ map.put(&mem, real_addr, val);
}
- string.advance_line(^file);
+ string.advance_line(&file);
}
}
// Any changes to the implementation of Map should be tested on this
// file to validate if they 1) work and 2) are faster.
nums := map.make(u32, spoken_times, .{});
- defer map.free(^nums);
+ defer map.free(&nums);
turn := 1;
last_num := 0;
for n: initial_numbers {
- map.put(^nums, n, .{ recent = turn });
+ map.put(&nums, n, .{ recent = turn });
turn += 1;
last_num = n;
}
while turn != 2021 {
- st := map.get(^nums, last_num);
+ st := map.get(&nums, last_num);
if st.previous == 0 do last_num = 0;
else do last_num = st.recent - st.previous;
- st = map.get(^nums, last_num);
+ st = map.get(&nums, last_num);
st.previous = st.recent;
st.recent = turn;
- map.put(^nums, last_num, st);
+ map.put(&nums, last_num, st);
turn += 1;
}
upper1 : u32 = 0;
}
-field_valid :: (use field: ^Field, n: u32) -> bool {
+field_valid :: (use field: &Field, n: u32) -> bool {
return (lower0 <= n && n <= upper0) || (lower1 <= n && n <= upper1);
}
total_scanning_error := 0;
-read_ticket_and_validate :: (file: ^str, fields: [..] Field, ticket_store: ^u32) -> bool {
+read_ticket_and_validate :: (file: &str, fields: [..] Field, ticket_store: &u32) -> bool {
retval := true;
for i: 0 .. fields.count {
if file.data[0] == #char "," do string.advance(file, 1);
valid_count := 0;
- for ^field: fields {
+ for &field: fields {
if field_valid(field, n) do valid_count += 1;
}
file := contents;
fields := array.make(Field, 20);
- defer array.free(^fields);
+ defer array.free(&fields);
// Read until the first empty line
while file.data[0] != #char "\n" {
field := Field.{};
field.name, file = string.bisect(file, #char ":");
- string.advance(^file, 1);
- field.lower0 = ~~ conv.parse_int(^file);
+ string.advance(&file, 1);
+ field.lower0 = ~~ conv.parse_int(&file);
- string.advance(^file, 1);
- field.upper0 = ~~ conv.parse_int(^file);
+ string.advance(&file, 1);
+ field.upper0 = ~~ conv.parse_int(&file);
- string.advance(^file, 4);
- field.lower1 = ~~ conv.parse_int(^file);
+ string.advance(&file, 4);
+ field.lower1 = ~~ conv.parse_int(&file);
- string.advance(^file, 1);
- field.upper1 = ~~ conv.parse_int(^file);
+ string.advance(&file, 1);
+ field.upper1 = ~~ conv.parse_int(&file);
- string.advance_line(^file);
+ string.advance_line(&file);
- array.push(^fields, field);
+ array.push(&fields, field);
}
- for i: 0 .. 2 do string.advance_line(^file);
+ for i: 0 .. 2 do string.advance_line(&file);
my_ticket := array.make(u32, fields.count);
- defer array.free(^my_ticket);
- read_ticket_and_validate(^file, fields, my_ticket.data);
+ defer array.free(&my_ticket);
+ read_ticket_and_validate(&file, fields, my_ticket.data);
- for i: 0 .. 2 do string.advance_line(^file);
+ for i: 0 .. 2 do string.advance_line(&file);
ticket_data := array.make(u32, 1024);
- defer array.free(^ticket_data);
+ defer array.free(&ticket_data);
while !string.empty(file) {
- array.ensure_capacity(^ticket_data, ticket_data.count + fields.count);
- if read_ticket_and_validate(^file, fields, ^ticket_data[ticket_data.count]) {
+ array.ensure_capacity(&ticket_data, ticket_data.count + fields.count);
+ if read_ticket_and_validate(&file, fields, &ticket_data[ticket_data.count]) {
ticket_data.count += fields.count;
}
}
printf("Scanning error: {}\n", total_scanning_error);
cols_to_assign := array.make(u32, fields.count);
- defer array.free(^cols_to_assign);
+ defer array.free(&cols_to_assign);
- for i: 0 .. fields.count do array.push(^cols_to_assign, i);
+ for i: 0 .. fields.count do array.push(&cols_to_assign, i);
while cols_to_assign.count > 0 {
for col: cols_to_assign {
work_count := 0;
- recent_work: ^Field = null;
+ recent_work: &Field = null;
- for ^field: fields {
+ for &field: fields {
if field.column != -1 do continue;
works := true;
if work_count == 1 {
recent_work.column = col;
- array.remove(^cols_to_assign, col);
+ array.remove(&cols_to_assign, col);
break;
}
}
}
prod: u64 = 1;
- for ^field: fields {
+ for &field: fields {
if string.starts_with(field.name, "departure") do prod *= ~~my_ticket[field.column];
}
next := false;
}
-get_neighbor_count :: (cubes: ^Map(CubePos, CubeState), pos: CubePos) -> u32 {
+get_neighbor_count :: (cubes: &Map(CubePos, CubeState), pos: CubePos) -> u32 {
count := 0;
for x: -1 .. 2 do for y: -1 .. 2 do for z: -1 .. 2 do for w: -1 .. 2 {
file := contents;
cubes := map.make(CubePos, CubeState, .{});
- defer map.free(^cubes);
+ defer map.free(&cubes);
z := 0;
while !string.empty(file) {
x := 0;
for ch: line {
- if ch == #char "#" do map.put(^cubes, .{ x, 0, z, 0 }, .{ alive = true });
+ if ch == #char "#" do map.put(&cubes, .{ x, 0, z, 0 }, .{ alive = true });
x += 1;
}
}
cubes_to_consider := array.make(CubePos);
- defer array.free(^cubes_to_consider);
+ defer array.free(&cubes_to_consider);
for i: 0 .. 6 {
- for ^cube_entry: cubes.entries {
+ for &cube_entry: cubes.entries {
if cube_entry.value.alive {
for x: -1 .. 2 do for y: -1 .. 2 do for z: -1 .. 2 do for w: -1 .. 2 {
- array.push(^cubes_to_consider, .{
+ array.push(&cubes_to_consider, .{
cube_entry.key.x + x,
cube_entry.key.y + y,
cube_entry.key.z + z,
}
}
- for ^cube: cubes_to_consider {
- state := map.get(^cubes, *cube);
- ncount := get_neighbor_count(^cubes, *cube);
+ for &cube: cubes_to_consider {
+ state := map.get(&cubes, *cube);
+ ncount := get_neighbor_count(&cubes, *cube);
if state.alive {
state.next = ncount == 2 || ncount == 3;
state.next = ncount == 3;
}
- map.put(^cubes, *cube, state);
+ map.put(&cubes, *cube, state);
}
- for ^cube: cubes_to_consider {
- state := map.get(^cubes, *cube);
+ for &cube: cubes_to_consider {
+ state := map.get(&cubes, *cube);
state.alive = state.next;
- map.put(^cubes, *cube, state);
+ map.put(&cubes, *cube, state);
}
- array.clear(^cubes_to_consider);
+ array.clear(&cubes_to_consider);
}
active_count := 0;
- for ^cube_entry: cubes.entries do if cube_entry.value.alive do active_count += 1;
+ for &cube_entry: cubes.entries do if cube_entry.value.alive do active_count += 1;
printf("Active count: {}\n", active_count);
}
use package core
-parse_factor :: (file: ^str) -> u64 {
+parse_factor :: (file: &str) -> u64 {
string.strip_leading_whitespace(file);
switch *file.data {
return 0;
}
-parse_expression_add :: (file: ^str) -> u64 {
+parse_expression_add :: (file: &str) -> u64 {
string.strip_leading_whitespace(file);
left := parse_factor(file);
return left;
}
-parse_expression_mul :: (file: ^str) -> u64 {
+parse_expression_mul :: (file: &str) -> u64 {
string.strip_leading_whitespace(file);
left := parse_expression_add(file);
total: u64 = 0;
while !string.empty(file) {
- total += parse_expression_mul(^file);
+ total += parse_expression_mul(&file);
}
printf("Total: {}\n", total);
max_terminal : u32;
}
-grammar_init :: (use g: ^Grammar) {
- array.init(^terminate_rules);
- array.init(^unit_rules);
- array.init(^production_rules);
+grammar_init :: (use g: &Grammar) {
+ array.init(&terminate_rules);
+ array.init(&unit_rules);
+ array.init(&production_rules);
max_terminal = 0;
}
-grammar_free :: (use g: ^Grammar) {
- array.free(^terminate_rules);
- array.free(^unit_rules);
- array.free(^production_rules);
+grammar_free :: (use g: &Grammar) {
+ array.free(&terminate_rules);
+ array.free(&unit_rules);
+ array.free(&production_rules);
}
-grammar_prepare :: (use g: ^Grammar) {
+grammar_prepare :: (use g: &Grammar) {
// Not full-proof, but good enough for AOC.
- for ^solo: unit_rules {
- for ^prod: production_rules {
+ for &solo: unit_rules {
+ for &prod: production_rules {
if prod.nt0 == solo.nt1 {
- array.push(^production_rules, Prod.{ solo.nt0, prod.nt1, prod.nt2 });
+ array.push(&production_rules, Prod.{ solo.nt0, prod.nt1, prod.nt2 });
}
}
- for ^unit: terminate_rules {
+ for &unit: terminate_rules {
if unit.nt == solo.nt1 {
- array.push(^terminate_rules, Term.{ solo.nt0, unit.t });
+ array.push(&terminate_rules, Term.{ solo.nt0, unit.t });
}
}
}
terminate_rules[terminate_rules.count - 1].nt) + 1;
}
-cyk_algorithm :: (use grammar: ^Grammar, input: str) -> bool {
+cyk_algorithm :: (use grammar: &Grammar, input: str) -> bool {
dim_0 := input.count * max_terminal;
dim_1 := max_terminal;
dim_2 := 1;
mem_size := sizeof bool * input.count * input.count * max_terminal;
- T := cast(^bool) calloc(mem_size);
+ T := cast(&bool) calloc(mem_size);
defer cfree(T);
memory.set(T, ~~false, mem_size);
for s: 0 .. input.count {
- for ^term: terminate_rules {
+ for &term: terminate_rules {
if term.t == input[s] {
T[0 * dim_0 + s * dim_1 + term.nt * dim_2] = true;
}
for l: 1 .. input.count {
for s: 0 .. input.count - l {
for p: 1 .. l + 1 {
- for ^prod: production_rules {
+ for &prod: production_rules {
if T[(p - 1) * dim_0 + s * dim_1 + prod.nt1 * dim_2]
&& T[(l - p) * dim_0 + (s + p) * dim_1 + prod.nt2 * dim_2] {
T[l * dim_0 + s * dim_1 + prod.nt0 * dim_2] = true;
file := contents;
grammar : Grammar;
- grammar_init(^grammar);
- defer grammar_free(^grammar);
+ grammar_init(&grammar);
+ defer grammar_free(&grammar);
while *file.data != #char "\n" {
- nt0 := cast(u32, conv.parse_int(^file));
+ nt0 := cast(u32, conv.parse_int(&file));
- string.advance(^file, 2); // ': '
+ string.advance(&file, 2); // ': '
if *file.data == #char "\"" {
- string.advance(^file, 1); // '"'
+ string.advance(&file, 1); // '"'
t := file[0];
- string.advance(^file, 1);
+ string.advance(&file, 1);
- array.push(^grammar.terminate_rules, Term.{ nt0, t });
+ array.push(&grammar.terminate_rules, Term.{ nt0, t });
} else {
while true {
- nt1 := cast(u32, conv.parse_int(^file));
+ nt1 := cast(u32, conv.parse_int(&file));
if *file.data == #char "\n" {
- array.push(^grammar.unit_rules, Unit.{ nt0, nt1 });
+ array.push(&grammar.unit_rules, Unit.{ nt0, nt1 });
break;
} else {
- string.advance(^file, 1); // ' '
+ string.advance(&file, 1); // ' '
if next_ch := *file.data; next_ch >= #char "0" && next_ch <= #char "9" {
- nt2 := cast(u32, conv.parse_int(^file));
- array.push(^grammar.production_rules, Prod.{ nt0, nt1, nt2 });
+ nt2 := cast(u32, conv.parse_int(&file));
+ array.push(&grammar.production_rules, Prod.{ nt0, nt1, nt2 });
- if *file.data == #char " " do string.advance(^file, 1);
+ if *file.data == #char " " do string.advance(&file, 1);
} else {
- array.push(^grammar.unit_rules, Unit.{ nt0, nt1 });
+ array.push(&grammar.unit_rules, Unit.{ nt0, nt1 });
}
if *file.data == #char "|" {
- string.advance(^file, 1); // ' |'
+ string.advance(&file, 1); // ' |'
} else {
break;
}
}
}
- string.advance_line(^file);
+ string.advance_line(&file);
}
- grammar_prepare(^grammar);
+ grammar_prepare(&grammar);
valid_count := 0;
- string.advance_line(^file);
+ string.advance_line(&file);
while !string.empty(file) {
line, file' := string.bisect(file, #char "\n");
- if cyk_algorithm(^grammar, line) do valid_count += 1;
+ if cyk_algorithm(&grammar, line) do valid_count += 1;
}
printf("Valid count: {}\n", valid_count);
valid := 0;
- while !io.reader_empty(^reader) {
- lo := io.read_u32(^reader);
+ while !io.reader_empty(&reader) {
+ lo := io.read_u32(&reader);
- io.read_byte(^reader);
- hi := io.read_u32(^reader);
+ io.read_byte(&reader);
+ hi := io.read_u32(&reader);
- io.read_byte(^reader);
- ch := io.read_byte(^reader);
+ io.read_byte(&reader);
+ ch := io.read_byte(&reader);
- io.skip_bytes(^reader, 2);
- pw := io.read_line(^reader);
+ io.skip_bytes(&reader, 2);
+ pw := io.read_line(&reader);
// Part 1
// count := 0;
build_edges :: (tile: [] bool, a := context.allocator) -> [] u32 {
edges : [..] u32;
- array.init(^edges, 8, allocator=a);
+ array.init(&edges, 8, allocator=a);
for y: u32.[0, 9] {
edge := 0;
if tile[x + y * TILE_DATA_WIDTH] do edge |= 1;
}
- array.push(^edges, edge);
+ array.push(&edges, edge);
}
for x: u32.[0, 9] {
if tile[x + y * TILE_DATA_WIDTH] do edge |= 1;
}
- array.push(^edges, edge);
+ array.push(&edges, edge);
}
- for i: 0 .. 4 do array.push(^edges, reverse_binary(edges[i]));
+ for i: 0 .. 4 do array.push(&edges, reverse_binary(edges[i]));
return edges.data[0 .. 8];
}
TO.[ TO.R90, TO.FR270, TO.F, TO.R180, ],
];
-has_matching_edges :: (t1: ^Tile, t2: ^Tile) -> bool {
+has_matching_edges :: (t1: &Tile, t2: &Tile) -> bool {
match := false;
for e_idx: 0 .. t1.edges.count / 2 do for e2_idx: 0 .. t2.edges.count {
}
// This assumes the `t` was in NORMAL orientation to begin with.
-apply_orientation :: (t: ^Tile, ori: TO) {
+apply_orientation :: (t: &Tile, ori: TO) {
new_sides := t.edges_match;
switch ori {
t.orientation = ori;
}
-index_square_with_orientation :: (data: ^$T, ori: TO, size: i32, x: i32, y: i32) -> ^T {
+index_square_with_orientation :: (data: &$T, ori: TO, size: i32, x: i32, y: i32) -> &T {
switch ori {
- case TO.N do return ^data[x + y * size];
- case TO.R90 do return ^data[y + (size - 1 - x) * size];
- case TO.R180 do return ^data[(size - 1 - x) + (size - 1 - y) * size];
- case TO.R270 do return ^data[(size - 1 - y) + x * size];
- case TO.F do return ^data[x + (size - 1 - y) * size];
- case TO.FR90 do return ^data[y + x * size];
- case TO.FR180 do return ^data[(size - 1 - x) + y * size];
- case TO.FR270 do return ^data[(size - 1 - y) + (size - 1 - x) * size];
+ case TO.N do return &data[x + y * size];
+ case TO.R90 do return &data[y + (size - 1 - x) * size];
+ case TO.R180 do return &data[(size - 1 - x) + (size - 1 - y) * size];
+ case TO.R270 do return &data[(size - 1 - y) + x * size];
+ case TO.F do return &data[x + (size - 1 - y) * size];
+ case TO.FR90 do return &data[y + x * size];
+ case TO.FR180 do return &data[(size - 1 - x) + y * size];
+ case TO.FR270 do return &data[(size - 1 - y) + (size - 1 - x) * size];
}
return null;
#char" ",#char"#",#char" ",#char" ",#char"#",#char" ",#char" ",#char"#",#char" ",#char" ",#char"#",#char" ",#char" ",#char"#",#char" ",#char" ",#char"#",#char" ",#char" ",#char" ",
];
-scan_for_monsters :: (forest: ^u8, ori: TO, width: u32, height: u32) -> bool {
+scan_for_monsters :: (forest: &u8, ori: TO, width: u32, height: u32) -> bool {
found_monsters := false;
for y: 0 .. height - sea_monster_height {
file := contents;
tiles := array.make(Tile);
- defer array.free(^tiles);
+ defer array.free(&tiles);
- tile_map := map.make(u32, ^Tile, null);
- defer map.free(^tile_map);
+ tile_map := map.make(u32, &Tile, null);
+ defer map.free(&tile_map);
- tile_data := cast(^TileData) calloc(200 * sizeof TileData);
+ tile_data := cast(&TileData) calloc(200 * sizeof TileData);
defer cfree(tile_data);
// This ring allocator could technically overflow and start
// should be more than enough space in the allocator to not
// run into that problem... Hopefully.
tile_data_ring := alloc.ring.make(.{ ~~ tile_data, 200 * sizeof TileData });
- tile_allocator := alloc.ring.make_allocator(^tile_data_ring);
+ tile_allocator := alloc.ring.make_allocator(&tile_data_ring);
while !string.empty(file) {
- string.advance(^file, 5); // 'Tile '
- id := cast(u32, conv.parse_int(^file));
+ string.advance(&file, 5); // 'Tile '
+ id := cast(u32, conv.parse_int(&file));
- string.advance_line(^file);
+ string.advance_line(&file);
- td := cast(^bool) raw_alloc(tile_allocator, sizeof TileData);
+ td := cast(&bool) raw_alloc(tile_allocator, sizeof TileData);
for y: 0 .. 10 {
line, file' := string.bisect(file, #char "\n");
tile_data := td[0 .. TILE_DATA_HEIGHT * TILE_DATA_WIDTH];
edges := build_edges(tile_data, tile_allocator);
- array.push(^tiles, .{
+ array.push(&tiles, .{
id = id,
orientation = TO.N,
data = tile_data,
edges = edges,
});
- string.advance_line(^file);
+ string.advance_line(&file);
}
- for ^t: tiles do map.put(^tile_map, t.id, t);
+ for &t: tiles do map.put(&tile_map, t.id, t);
prod: u64 = 1;
top_left_id := 0;
for j: 0 .. tiles.count {
if i == j do continue;
- if has_matching_edges(^tiles[i], ^tiles[j]) {
+ if has_matching_edges(&tiles[i], &tiles[j]) {
matching_count += 1;
}
}
printf("Corner product: {}\n", prod);
grid : [12 * 12] u32;
- memory.set(^grid, 0, sizeof [12 * 12] u32);
+ memory.set(&grid, 0, sizeof [12 * 12] u32);
tile_pos_state :: struct {
match : SideRelation;
pos_y : i32;
}
to_process := array.make(tile_pos_state);
- defer array.free(^to_process);
+ defer array.free(&to_process);
- array.push(^to_process, .{ .{ top_left_id, TO.F }, 0, 0 });
+ array.push(&to_process, .{ .{ top_left_id, TO.F }, 0, 0 });
while to_process.count > 0 {
tid := to_process[0];
- array.delete(^to_process, 0);
+ array.delete(&to_process, 0);
if grid[tid.pos_x + 12 * tid.pos_y] != 0 do continue;
- tile_ptr := map.get(^tile_map, tid.match.tile);
+ tile_ptr := map.get(&tile_map, tid.match.tile);
tile_ptr.pos_x = tid.pos_x;
tile_ptr.pos_y = tid.pos_y;
grid[tid.pos_x + 12 * tid.pos_y] = tid.match.tile;
apply_orientation(tile_ptr, tid.match.ori);
- if tile_ptr.edges_match.top.tile != 0 do array.push(^to_process, .{ tile_ptr.edges_match.top, tid.pos_x, tid.pos_y - 1 });
- if tile_ptr.edges_match.bottom.tile != 0 do array.push(^to_process, .{ tile_ptr.edges_match.bottom, tid.pos_x, tid.pos_y + 1 });
- if tile_ptr.edges_match.left.tile != 0 do array.push(^to_process, .{ tile_ptr.edges_match.left, tid.pos_x - 1, tid.pos_y });
- if tile_ptr.edges_match.right.tile != 0 do array.push(^to_process, .{ tile_ptr.edges_match.right, tid.pos_x + 1, tid.pos_y });
+ if tile_ptr.edges_match.top.tile != 0 do array.push(&to_process, .{ tile_ptr.edges_match.top, tid.pos_x, tid.pos_y - 1 });
+ if tile_ptr.edges_match.bottom.tile != 0 do array.push(&to_process, .{ tile_ptr.edges_match.bottom, tid.pos_x, tid.pos_y + 1 });
+ if tile_ptr.edges_match.left.tile != 0 do array.push(&to_process, .{ tile_ptr.edges_match.left, tid.pos_x - 1, tid.pos_y });
+ if tile_ptr.edges_match.right.tile != 0 do array.push(&to_process, .{ tile_ptr.edges_match.right, tid.pos_x + 1, tid.pos_y });
}
forest : [12 * 8 * 12 * 8] u8;
for y: 0 .. 12 {
for x: 0 .. 12 {
- tile := map.get(^tile_map, grid[y * 12 + x]);
+ tile := map.get(&tile_map, grid[y * 12 + x]);
for fy: 0 .. 8 {
for fx: 0 .. 8 {
- res := *index_square_with_orientation(cast(^bool) tile.data.data, tile.orientation, 10, fx + 1, fy + 1);
+ res := *index_square_with_orientation(cast(&bool) tile.data.data, tile.orientation, 10, fx + 1, fy + 1);
loc := (y * 12 * 8 * 8) + (fy * 12 * 8) + (x * 8) + fx;
if res do forest[loc] = #char "#";
else do forest[loc] = #char ".";
}
for ori: .[ TO.N, TO.R90, TO.R180, TO.R270, TO.F, TO.FR90, TO.FR180, TO.FR270 ] {
- if scan_for_monsters(cast(^u8) forest, ori, 12 * 8, 12 * 8) do break;
+ if scan_for_monsters(cast(&u8) forest, ori, 12 * 8, 12 * 8) do break;
}
safe_count := 0;
file := contents;
- map.init(^ingredient_map, .{});
- map.init(^allergen_map, .{});
+ map.init(&ingredient_map, .{});
+ map.init(&allergen_map, .{});
defer {
- map.free(^ingredient_map);
- map.free(^allergen_map);
+ map.free(&ingredient_map);
+ map.free(&allergen_map);
}
foods := array.make(Food);
- defer array.free(^foods);
+ defer array.free(&foods);
line_num := 0;
while !string.empty(file) {
food : Food;
- array.init(^food.ingredients, 16);
- array.init(^food.allergens);
+ array.init(&food.ingredients, 16);
+ array.init(&food.allergens);
while *file.data != #char "(" {
- ingredient_name := string.read_alphanum(^file);
- string.advance(^file, 1); // ' '
+ ingredient_name := string.read_alphanum(&file);
+ string.advance(&file, 1); // ' '
- array.push(^food.ingredients, ingredient_name);
+ array.push(&food.ingredients, ingredient_name);
- ingredient := map.get(^ingredient_map, ingredient_name);
+ ingredient := map.get(&ingredient_map, ingredient_name);
if ingredient.name.data == null {
ingredient.name = ingredient_name;
- array.init(^ingredient.appears_on, 4);
+ array.init(&ingredient.appears_on, 4);
}
- array.push(^ingredient.appears_on, line_num);
+ array.push(&ingredient.appears_on, line_num);
- map.put(^ingredient_map, ingredient_name, ingredient);
+ map.put(&ingredient_map, ingredient_name, ingredient);
}
- string.advance(^file, 10); // '(contains '
+ string.advance(&file, 10); // '(contains '
while *file.data != #char ")" {
- allergen_name := string.read_alphanum(^file);
- if *file.data == #char "," do string.advance(^file, 2); // ', '
+ allergen_name := string.read_alphanum(&file);
+ if *file.data == #char "," do string.advance(&file, 2); // ', '
- array.push(^food.allergens, allergen_name);
+ array.push(&food.allergens, allergen_name);
- allergen := map.get(^allergen_map, allergen_name);
+ allergen := map.get(&allergen_map, allergen_name);
if allergen.name.data == null {
allergen.name = allergen_name;
- array.init(^allergen.appears_on, 4);
+ array.init(&allergen.appears_on, 4);
}
- array.push(^allergen.appears_on, line_num);
+ array.push(&allergen.appears_on, line_num);
- map.put(^allergen_map, allergen_name, allergen);
+ map.put(&allergen_map, allergen_name, allergen);
}
- array.push(^foods, food);
+ array.push(&foods, food);
- string.advance_line(^file);
+ string.advance_line(&file);
line_num += 1;
}
definitely_safe := array.make(str);
- defer array.free(^definitely_safe);
+ defer array.free(&definitely_safe);
- for ^ingredient_entry: ingredient_map.entries {
+ for &ingredient_entry: ingredient_map.entries {
potential_allergens := array.make(str);
- defer array.free(^potential_allergens);
+ defer array.free(&potential_allergens);
for food_num: ingredient_entry.value.appears_on {
- for ^allergen_name: foods[food_num].allergens {
- array.push(^potential_allergens, *allergen_name);
+ for &allergen_name: foods[food_num].allergens {
+ array.push(&potential_allergens, *allergen_name);
}
}
potential_allergen_count := 0;
- for ^allergen_name: potential_allergens {
- c := array_count_contains(^potential_allergens, *allergen_name, string.equal);
- allergen := map.get(^allergen_map, *allergen_name);
+ for &allergen_name: potential_allergens {
+ c := array_count_contains(&potential_allergens, *allergen_name, string.equal);
+ allergen := map.get(&allergen_map, *allergen_name);
if c == allergen.appears_on.count {
potential_allergen_count += 1;
}
}
if potential_allergen_count == 0 {
- array.push(^definitely_safe, ingredient_entry.key);
+ array.push(&definitely_safe, ingredient_entry.key);
}
}
total_safe := 0;
for safe: definitely_safe {
- ingredient := map.get(^ingredient_map, safe);
+ ingredient := map.get(&ingredient_map, safe);
total_safe += ingredient.appears_on.count;
- map.delete(^ingredient_map, safe);
+ map.delete(&ingredient_map, safe);
}
printf("Total safe: {}\n", total_safe);
matched_ingredients := array.make(Ingredient);
- defer array.free(^matched_ingredients);
+ defer array.free(&matched_ingredients);
- while !map.empty(^ingredient_map) {
- for ^allergen_entry: allergen_map.entries {
+ while !map.empty(&ingredient_map) {
+ for &allergen_entry: allergen_map.entries {
match_count := 0;
matching_ingredient_name := str.{ null, 0 };
- for ^ingredient_entry: ingredient_map.entries {
+ for &ingredient_entry: ingredient_map.entries {
matches := true;
for ap: allergen_entry.value.appears_on {
}
if match_count == 1 {
- ingredient := map.get(^ingredient_map, matching_ingredient_name);
- map.delete(^ingredient_map, matching_ingredient_name);
+ ingredient := map.get(&ingredient_map, matching_ingredient_name);
+ map.delete(&ingredient_map, matching_ingredient_name);
ingredient.allergen = allergen_entry.key;
- array.push(^matched_ingredients, ingredient);
+ array.push(&matched_ingredients, ingredient);
}
}
}
- array.sort(matched_ingredients, (i1: ^Ingredient, i2: ^Ingredient) => string.compare(i1.allergen, i2.allergen));
+ array.sort(matched_ingredients, (i1: &Ingredient, i2: &Ingredient) => string.compare(i1.allergen, i2.allergen));
- for ^mi: matched_ingredients do printf("{} -> {}\n", mi.name, mi.allergen);
- for ^mi: matched_ingredients do printf("{},", mi.name);
+ for &mi: matched_ingredients do printf("{} -> {}\n", mi.name, mi.allergen);
+ for &mi: matched_ingredients do printf("{},", mi.name);
println("\n(Don't copy the last ','!)");
}
-array_count_contains :: (arr: ^[..] $T, x: T, equal: (T, T) -> bool) -> u32 {
+array_count_contains :: (arr: &[..] $T, x: T, equal: (T, T) -> bool) -> u32 {
count := 0;
- for ^it: *arr do if equal(*it, x) do count += 1;
+ for &it: *arr do if equal(*it, x) do count += 1;
return count;
}
key_arena : alloc.arena.ArenaState;
key_alloc : Allocator;
-score_player :: (p: ^[..] u32) -> u32 {
+score_player :: (p: &[..] u32) -> u32 {
count := 0;
mul := p.count;
for c: *p {
return count;
}
-combat :: (player1: ^[..] u32, player2: ^[..] u32) -> u32 {
+combat :: (player1: &[..] u32, player2: &[..] u32) -> u32 {
while player1.count > 0 && player2.count > 0 {
p1 := player1.data[0];
p2 := player2.data[0];
// into:
// 4,5,2,8,|1,3,9,7,
// Larger numbers are encoded in base 64.
-encode_hands :: (alloc: Allocator, p1: ^[..] u32, p2: ^[..] u32) -> str {
+encode_hands :: (alloc: Allocator, p1: &[..] u32, p2: &[..] u32) -> str {
stream := io.buffer_stream_make(256, alloc);
- writer := io.writer_make(^stream, 0);
+ writer := io.writer_make(&stream, 0);
for n: *p1 {
- io.write_i64(^writer, ~~n, 64);
- io.write_str(^writer, ",");
+ io.write_i64(&writer, ~~n, 64);
+ io.write_str(&writer, ",");
}
- io.write_str(^writer, "|");
+ io.write_str(&writer, "|");
for n: *p2 {
- io.write_i64(^writer, ~~n, 64);
- io.write_str(^writer, ",");
+ io.write_i64(&writer, ~~n, 64);
+ io.write_str(&writer, ",");
}
- return io.buffer_stream_to_str(^stream);
+ return io.buffer_stream_to_str(&stream);
}
-recursive_combat :: (player1: ^[..] u32, player2: ^[..] u32) -> u32 {
+recursive_combat :: (player1: &[..] u32, player2: &[..] u32) -> u32 {
hand_seen := map.make(str, bool, false);
- defer map.free(^hand_seen);
+ defer map.free(&hand_seen);
while player1.count > 0 && player2.count > 0 {
enc_hand := encode_hands(key_alloc, player1, player2);
- if map.has(^hand_seen, enc_hand) do return 1;
- map.put(^hand_seen, enc_hand, true);
+ if map.has(&hand_seen, enc_hand) do return 1;
+ map.put(&hand_seen, enc_hand, true);
p1 := player1.data[0];
p2 := player2.data[0];
if player1.count >= p1 && player2.count >= p2 {
p1_copy := array.copy_range(player1, 0 .. p1);
p2_copy := array.copy_range(player2, 0 .. p2);
- defer array.free(^p1_copy);
- defer array.free(^p2_copy);
+ defer array.free(&p1_copy);
+ defer array.free(&p2_copy);
- round_win = recursive_combat(^p1_copy, ^p2_copy);
+ round_win = recursive_combat(&p1_copy, &p2_copy);
} else {
if p1 > p2 do round_win = 1;
else do round_win = 2;
player1 := array.make(u32);
player2 := array.make(u32);
- defer array.free(^player1);
- defer array.free(^player2);
+ defer array.free(&player1);
+ defer array.free(&player2);
- string.advance_line(^file); // 'Player 1:'
+ string.advance_line(&file); // 'Player 1:'
while *file.data != #char "\n" {
- card := cast(u32, conv.parse_int(^file));
- array.push(^player1, card);
+ card := cast(u32, conv.parse_int(&file));
+ array.push(&player1, card);
- string.advance_line(^file);
+ string.advance_line(&file);
}
- string.advance_line(^file); // '\n'
- string.advance_line(^file); // 'Player 2:'
+ string.advance_line(&file); // '\n'
+ string.advance_line(&file); // 'Player 2:'
while !string.empty(file) {
- card := cast(u32, conv.parse_int(^file));
- array.push(^player2, card);
+ card := cast(u32, conv.parse_int(&file));
+ array.push(&player2, card);
- string.advance_line(^file);
+ string.advance_line(&file);
}
{
- player1_copy := array.copy(^player1);
- player2_copy := array.copy(^player2);
- defer array.free(^player1_copy);
- defer array.free(^player2_copy);
+ player1_copy := array.copy(&player1);
+ player2_copy := array.copy(&player2);
+ defer array.free(&player1_copy);
+ defer array.free(&player2_copy);
- combat_score := combat(^player1_copy, ^player2_copy);
+ combat_score := combat(&player1_copy, &player2_copy);
printf("Answer: {}\n", combat_score);
}
#if false {
key_arena = alloc.arena.make(context.allocator, 1 << 14);
- key_alloc = alloc.arena.make_allocator(^key_arena);
+ key_alloc = alloc.arena.make_allocator(&key_arena);
- player1_copy := array.copy(^player1);
- player2_copy := array.copy(^player2);
- defer array.free(^player1_copy);
- defer array.free(^player2_copy);
+ player1_copy := array.copy(&player1);
+ player2_copy := array.copy(&player2);
+ defer array.free(&player1_copy);
+ defer array.free(&player2_copy);
- winner := recursive_combat(^player1_copy, ^player2_copy);
+ winner := recursive_combat(&player1_copy, &player2_copy);
score : u32;
- if winner == 1 do score = score_player(^player1_copy);
- if winner == 2 do score = score_player(^player2_copy);
+ if winner == 1 do score = score_player(&player1_copy);
+ if winner == 2 do score = score_player(&player2_copy);
printf("Recursive answer: {}\n", score);
}
cups_data := i32.[ 9, 6, 2, 7, 1, 3, 8, 5, 4 ];
// Easier think about in zero-indexed
- for ^cup: cups_data do *cup -= 1;
+ for &cup: cups_data do *cup -= 1;
- array.init(^cups, 1000000);
- defer array.free(^cups);
+ array.init(&cups, 1000000);
+ defer array.free(&cups);
- for cup: cups_data do array.push(^cups, cup);
+ for cup: cups_data do array.push(&cups, cup);
// Part 1
- // simulate(array.to_slice(^cups));
+ // simulate(array.to_slice(&cups));
// Part 2
- for i: 9 .. 1000000 do array.push(^cups, i);
+ for i: 9 .. 1000000 do array.push(&cups, i);
simulate(cups, 10000000);
// Undo the zero-indexing
- for ^cup: cups do *cup += 1;
+ for &cup: cups do *cup += 1;
// Part 1
- // one_idx := get_idx(array.to_slice(^cups), 1);
+ // one_idx := get_idx(array.to_slice(&cups), 1);
// for i: 1 .. 9 {
// printf("{}", cast(i32) cups[w(one_idx + i)]);
// }
contents := #file_contents "./tests/aoc-2020/input/day24.txt";
file_stream := io.buffer_stream_make(contents);
- file := io.reader_make(^file_stream);
+ file := io.reader_make(&file_stream);
grid := map.make(Vec2, Cell, .{}); // `true` is black
- defer map.free(^grid);
+ defer map.free(&grid);
- while !io.reader_empty(^file) {
- line := io.read_line(^file);
+ while !io.reader_empty(&file) {
+ line := io.read_line(&file);
loc := Vec2.{ 0, 0 };
s := 0;
}
- curr := map.get(^grid, loc);
- map.put(^grid, loc, .{ alive = !curr.alive });
+ curr := map.get(&grid, loc);
+ map.put(&grid, loc, .{ alive = !curr.alive });
}
// Part 1
black_count := 0;
- for ^cell: grid.entries {
+ for &cell: grid.entries {
if cell.value.alive do black_count += 1;
}
printf("Black count: {}\n", black_count);
// Part 2
cells_to_consider := array.make(Vec2);
- defer array.free(^cells_to_consider);
+ defer array.free(&cells_to_consider);
for i: 0 .. 100 {
- for ^cell: grid.entries {
+ for &cell: grid.entries {
if cell.value.alive {
- array.push(^cells_to_consider, cell.key);
+ array.push(&cells_to_consider, cell.key);
- for ^dir: Hex_Directions {
- array.push(^cells_to_consider, .{
+ for &dir: Hex_Directions {
+ array.push(&cells_to_consider, .{
x = cell.key.x + dir.x,
y = cell.key.y + dir.y,
});
}
}
- for ^cell: cells_to_consider {
- state := map.get(^grid, *cell);
- ncount := get_neighbor_count(^grid, *cell);
+ for &cell: cells_to_consider {
+ state := map.get(&grid, *cell);
+ ncount := get_neighbor_count(&grid, *cell);
if state.alive {
state.next = ncount == 1 || ncount == 2;
state.next = ncount == 2;
}
- map.put(^grid, *cell, state);
+ map.put(&grid, *cell, state);
}
- for ^cell: cells_to_consider {
- map.update(^grid, *cell, #quote { it.alive = it.next; });
+ for &cell: cells_to_consider {
+ map.update(&grid, *cell, #quote { it.alive = it.next; });
}
- array.clear(^cells_to_consider);
+ array.clear(&cells_to_consider);
}
black_count = 0;
- for ^cell: grid.entries {
+ for &cell: grid.entries {
if cell.value.alive do black_count += 1;
}
printf("GOL black count: {}\n", black_count);
}
-get_neighbor_count :: (grid: ^map.Map(Vec2, Cell), pos: Vec2) -> u32 {
+get_neighbor_count :: (grid: &map.Map(Vec2, Cell), pos: Vec2) -> u32 {
count := 0;
- for ^dir: Hex_Directions {
+ for &dir: Hex_Directions {
cell := map.get(grid, Vec2.{ x = pos.x + dir.x, y = pos.y + dir.y });
if cell.alive do count += 1;
}
m := cast(u32) math.ceil(math.sqrt(cast(f64) n));
t := map.make(u32, u32, default=0);
- defer map.free(^t);
+ defer map.free(&t);
tmp: u64 = 1;
for i: 0 .. m {
- map.put(^t, ~~tmp, i);
+ map.put(&t, ~~tmp, i);
tmp = (tmp * ~~a) % ~~n;
}
tmp = ~~b;
for i: 0 .. m {
- if map.has(^t, ~~tmp) {
- v := map.get(^t, ~~tmp);
+ if map.has(&t, ~~tmp) {
+ v := map.get(&t, ~~tmp);
return i * m + v;
}
file := contents;
- card_pub_key := cast(u32, conv.parse_int(^file));
- door_pub_key := cast(u32, conv.parse_int(^file));
+ card_pub_key := cast(u32, conv.parse_int(&file));
+ door_pub_key := cast(u32, conv.parse_int(&file));
card_loop_secret := dlp_bsgs(20201227, 7, card_pub_key);
door_loop_secret := dlp_bsgs(20201227, 7, door_pub_key);
contents := #file_contents "./tests/aoc-2020/input/day3.txt";
forest := array.make(u8, 1024);
- defer array.free(^forest);
+ defer array.free(&forest);
width := 0;
height := 0;
while true {
- line := string.read_until(^contents, #char "\n");
- string.advance(^contents, 1);
+ line := string.read_until(&contents, #char "\n");
+ string.advance(&contents, 1);
if line.count == 0 do break;
width = line.count;
height = height + 1;
- for ch: line do array.push(^forest, ch);
+ for ch: line do array.push(&forest, ch);
}
lis := LineInterp.[
use package core
// Returns the number of fields
-process_passport :: (contents: ^str) -> u32 {
+process_passport :: (contents: &str) -> u32 {
field_count := 0;
while true {
while true {
if contents.count == 0 do break;
- field_count := process_passport(^contents);
+ field_count := process_passport(&contents);
if field_count == 7 do valid_passports += 1;
}
contents := #file_contents "./tests/aoc-2020/input/day5.txt";
vals := array.make(u32);
- defer array.free(^vals);
+ defer array.free(&vals);
max_val := 0;
while true {
- line := string.read_until(^contents, #char "\n");
- string.advance(^contents);
+ line := string.read_until(&contents, #char "\n");
+ string.advance(&contents);
if line.count == 0 do break;
val := 0;
}
max_val = math.max(max_val, val);
- array.push(^vals, val);
+ array.push(&vals, val);
}
missing := 0;
use package core
-part_1 :: (contents: ^str) -> u32 {
+part_1 :: (contents: &str) -> u32 {
chars : [26] bool;
- for ^ch: chars do *ch = false;
+ for &ch: chars do *ch = false;
while true {
line := string.read_until(contents, #char "\n");
return sum;
}
-part_2 :: (contents: ^str) -> u32 {
+part_2 :: (contents: &str) -> u32 {
chars : [26] u32;
- for ^ch: chars do *ch = 0;
+ for &ch: chars do *ch = 0;
person_count := 0;
while true {
while true {
if contents.count == 0 do break;
- unique := part_1(^contents);
+ unique := part_1(&contents);
unique_sum += unique;
}
use package core
BagGraph :: struct {
- nodes : [..] ^BagNode;
- node_map : map.Map(str, ^BagNode);
+ nodes : [..] &BagNode;
+ node_map : map.Map(str, &BagNode);
}
BagNode :: struct {
}
BagContainment :: struct {
- bag : ^BagNode;
+ bag : &BagNode;
count : u32;
}
-bg_init :: (use graph: ^BagGraph) {
- array.init(^nodes, 16);
- map.init(^node_map, null);
+bg_init :: (use graph: &BagGraph) {
+ array.init(&nodes, 16);
+ map.init(&node_map, null);
}
-bg_free :: (use graph: ^BagGraph) {
- array.free(^nodes);
- map.free(^node_map);
+bg_free :: (use graph: &BagGraph) {
+ array.free(&nodes);
+ map.free(&node_map);
}
-bg_get_node :: (use graph: ^BagGraph, name: str) -> ^BagNode {
- node := map.get(^node_map, name);
+bg_get_node :: (use graph: &BagGraph, name: str) -> &BagNode {
+ node := map.get(&node_map, name);
if node == null {
node = calloc(sizeof BagNode);
// Part 1
- // array.init(^node.contained_in, 2);
+ // array.init(&node.contained_in, 2);
// Part 2
- array.init(^node.contain, 2);
+ array.init(&node.contain, 2);
node.color = name;
- array.push(^nodes, node);
- map.put(^node_map, node.color, node);
+ array.push(&nodes, node);
+ map.put(&node_map, node.color, node);
}
return node;
file := contents;
graph : BagGraph;
- bg_init(^graph);
- defer bg_free(^graph);
+ bg_init(&graph);
+ defer bg_free(&graph);
while true {
- name := string.read_until(^file, #char " ", 1);
+ name := string.read_until(&file, #char " ", 1);
if name.count == 0 do break;
- container := bg_get_node(^graph, name);
+ container := bg_get_node(&graph, name);
- string.read_until(^file, #char " ", 2);
+ string.read_until(&file, #char " ", 2);
while true {
if string.starts_with(file, " no") do break;
- count := cast(u32, conv.parse_int(^file));
- string.advance(^file, 1);
+ count := cast(u32, conv.parse_int(&file));
+ string.advance(&file, 1);
- contained_name := string.read_until(^file, #char " ", 1);
- contained := bg_get_node(^graph, contained_name);
+ contained_name := string.read_until(&file, #char " ", 1);
+ contained := bg_get_node(&graph, contained_name);
// Part 1
- // array.push(^contained.contained_in, BagContainment.{
+ // array.push(&contained.contained_in, BagContainment.{
// bag = container,
// count = count
// });
// Part 2
- array.push(^container.contain, .{ bag = contained, count = count });
+ array.push(&container.contain, .{ bag = contained, count = count });
- bag_word := string.read_until_any(^file, 1, #char " ", #char "\n");
+ bag_word := string.read_until_any(&file, 1, #char " ", #char "\n");
if bag_word[bag_word.count - 1] == #char "." do break;
}
- string.advance_line(^file);
+ string.advance_line(&file);
}
// Part 1
- // to_process_bags : [..] ^BagNode;
- // processed_bags: [..] ^BagNode;
+ // to_process_bags : [..] &BagNode;
+ // processed_bags: [..] &BagNode;
- // array.init(^to_process_bags);
- // array.init(^processed_bags, 32);
- // array.push(^to_process_bags, bg_get_node(^graph, "shiny gold"));
+ // array.init(&to_process_bags);
+ // array.init(&processed_bags, 32);
+ // array.push(&to_process_bags, bg_get_node(&graph, "shiny gold"));
// while to_process_bags.count > 0 {
- // bag := array.pop(^to_process_bags);
- // array.push(^processed_bags, bag);
+ // bag := array.pop(&to_process_bags);
+ // array.push(&processed_bags, bag);
//
// for container: bag.contained_in {
- // if !array.contains(^processed_bags, container.bag) && !array.contains(^to_process_bags, container.bag) {
+ // if !array.contains(&processed_bags, container.bag) && !array.contains(&to_process_bags, container.bag) {
// // printf("Adding {} to process.\n", container.bag.color);
- // array.push(^to_process_bags, container.bag);
+ // array.push(&to_process_bags, container.bag);
// }
// }
// }
// printf("Count: {}\n", processed_bags.count - 1);
// Part 2
- to_process := array.make(^BagNode);
+ to_process := array.make(&BagNode);
multiplier := array.make(u32);
- array.push(^to_process, bg_get_node(^graph, "shiny gold"));
- array.push(^multiplier, 1);
+ array.push(&to_process, bg_get_node(&graph, "shiny gold"));
+ array.push(&multiplier, 1);
count := 0;
while to_process.count > 0 {
- bag := array.pop(^to_process);
- mul := array.pop(^multiplier);
+ bag := array.pop(&to_process);
+ mul := array.pop(&multiplier);
count += mul;
for bc: bag.contain {
- array.push(^to_process, bc.bag);
- array.push(^multiplier, bc.count * mul);
+ array.push(&to_process, bc.bag);
+ array.push(&multiplier, bc.count * mul);
}
}
}
// Returns if the program successfully exited.
-get_acc_value :: (instrs: [..] Instruction, ret_acc: ^i32) -> bool {
+get_acc_value :: (instrs: [..] Instruction, ret_acc: &i32) -> bool {
already_been := map.make(i32, bool, false);
- defer map.free(^already_been);
+ defer map.free(&already_been);
ip := 0;
acc := 0;
break;
}
- if map.has(^already_been, ip) do break;
- map.put(^already_been, ip, true);
+ if map.has(&already_been, ip) do break;
+ map.put(&already_been, ip, true);
switch instrs[ip].opcode {
case OpCode.Nop do ip += 1;
file := contents;
instrs := array.make(Instruction, 32);
- defer array.free(^instrs);
+ defer array.free(&instrs);
while !string.empty(file) {
word := file.data[0 .. 3];
- string.advance(^file, 4);
+ string.advance(&file, 4);
sign := file.data[0];
- string.advance(^file, 1);
- val := cast(i32) conv.parse_int(^file);
+ string.advance(&file, 1);
+ val := cast(i32) conv.parse_int(&file);
- string.advance_line(^file);
+ string.advance_line(&file);
if sign == #char "-" do val *= -1;
elseif string.equal(word, "acc") do opcode = OpCode.Acc;
elseif string.equal(word, "jmp") do opcode = OpCode.Jmp;
- array.push(^instrs, .{ opcode, ~~val });
+ array.push(&instrs, .{ opcode, ~~val });
}
acc: i32;
- for ^instr: instrs {
+ for &instr: instrs {
if instr.opcode == OpCode.Nop do instr.opcode = OpCode.Jmp;
elseif instr.opcode == OpCode.Jmp do instr.opcode = OpCode.Nop;
- if get_acc_value(instrs, ^acc) do break;
+ if get_acc_value(instrs, &acc) do break;
if instr.opcode == OpCode.Nop do instr.opcode = OpCode.Jmp;
elseif instr.opcode == OpCode.Jmp do instr.opcode = OpCode.Nop;
file := contents;
nums := array.make(u64, 32);
- defer array.free(^nums);
+ defer array.free(&nums);
while !string.empty(file) {
- num := conv.parse_int(^file);
- array.push(^nums, num);
+ num := conv.parse_int(&file);
+ array.push(&nums, num);
}
preamble_length :: 25;
main :: (args) => {
file := os.open("./tests/aoc-2021/input/day01.txt")->expect("Error opening file");
- reader := io.reader_make(^file);
- defer os.close(^file);
+ reader := io.reader_make(&file);
+ defer os.close(&file);
nums: [..] i32;
- while !io.reader_empty(^reader) {
- nums << io.read_u32(^reader);
- io.skip_whitespace(^reader);
+ while !io.reader_empty(&reader) {
+ nums << io.read_u32(&reader);
+ io.skip_whitespace(&reader);
}
{ // Part 1
#if PART == 1 {
horizontal, vertical := 0, 0;
- while !io.reader_empty(^reader) {
- parts := string.split(io.read_line(^reader, inplace=true), #char " ");
- defer memory.free_slice(^parts);
+ while !io.reader_empty(&reader) {
+ parts := string.split(io.read_line(&reader, inplace=true), #char " ");
+ defer memory.free_slice(&parts);
value := cast(i32) conv.str_to_i64(parts[1]);
switch parts[0] {
#if PART == 2 {
horizontal, vertical, aim : i64;
horizontal, vertical, aim = 0, 0, 0;
- while !io.reader_empty(^reader) {
- parts := string.split(io.read_line(^reader, inplace=true), #char " ");
- defer memory.free_slice(^parts);
+ while !io.reader_empty(&reader) {
+ parts := string.split(io.read_line(&reader, inplace=true), #char " ");
+ defer memory.free_slice(&parts);
value := conv.str_to_i64(parts[1]);
switch parts[0] {
use package core
-read_binary :: (r: ^io.Reader) -> i32 {
+read_binary :: (r: &io.Reader) -> i32 {
n := 0;
io.skip_whitespace(r);
reader := io.reader_make(it);
nums: [..] i32;
- while !io.reader_empty(^reader) {
- nums << read_binary(^reader);
+ while !io.reader_empty(&reader) {
+ nums << read_binary(&reader);
}
num1 := 0;
printf("Part 1: {}\n", num1 * num2);
- oxygen_array := array.copy(^nums);
- co2_array := array.copy(^nums);
+ oxygen_array := array.copy(&nums);
+ co2_array := array.copy(&nums);
filter_array :: macro (arr: [..] i32, index: i32, comparison: Code) {
A := 0;
defer i += 1;
if (arr[i] & (1 << index)) != expected {
- array.fast_delete(^arr, i);
+ array.fast_delete(&arr, i);
i -= 1;
}
}
won_on : Cell;
}
-board_score :: (use b: ^Board) => {
+board_score :: (use b: &Board) => {
sum_of_unmarked := 0;
for 25 do if !marked[it] do sum_of_unmarked += ~~cells[it];
return sum_of_unmarked * ~~won_on;
for os.with_file("./tests/aoc-2021/input/day04.txt") {
reader := io.reader_make(it);
- numbers_line := io.read_line(^reader, inplace=true, consume_newline=true);
+ numbers_line := io.read_line(&reader, inplace=true, consume_newline=true);
numbers_str := string.split(numbers_line, #char ",");
numbers := memory.make_slice(Cell, numbers_str.count);
for numbers_str.count do numbers[it] = ~~ conv.parse_int(numbers_str[it]);
boards: [..] Board;
- while !io.reader_empty(^reader) {
- array.insert_empty(^boards, boards.count);
- board := ^boards[boards.count - 1];
+ while !io.reader_empty(&reader) {
+ array.insert_empty(&boards, boards.count);
+ board := &boards[boards.count - 1];
board.has_won = false;
- memory.set(^board.marked, 0, sizeof typeof board.marked);
+ memory.set(&board.marked, 0, sizeof typeof board.marked);
for 25 {
- board.cells[it] = ~~ io.read_u32(^reader);
+ board.cells[it] = ~~ io.read_u32(&reader);
}
- io.skip_whitespace(^reader);
+ io.skip_whitespace(&reader);
}
- winning_board: ^Board = null;
- worst_board : ^Board = null;
+ winning_board: &Board = null;
+ worst_board : &Board = null;
for called: numbers {
- for ^ board: boards {
+ for & board: boards {
if board.has_won do continue;
// Whatever the last board we touch is must be the worst one.
c.dx = 1 if l.x2 > l.x1 else -1 if l.x2 < l.x1 else 0;
c.dy = 1 if l.y2 > l.y1 else -1 if l.y2 < l.y1 else 0;
- next :: (use c: ^Context) -> (Point, bool) {
+ next :: (use c: &Context) -> (Point, bool) {
if curr_t > max_t do return .{0,0}, false;
defer curr_t += 1;
lines: [..] Line;
- while !io.reader_empty(^reader) {
- x1 := io.read_u32(^reader);
- io.skip_bytes(^reader, 1);
- y1 := io.read_u32(^reader);
- io.skip_bytes(^reader, 4);
+ while !io.reader_empty(&reader) {
+ x1 := io.read_u32(&reader);
+ io.skip_bytes(&reader, 1);
+ y1 := io.read_u32(&reader);
+ io.skip_bytes(&reader, 4);
- x2 := io.read_u32(^reader);
- io.skip_bytes(^reader, 1);
- y2 := io.read_u32(^reader);
- io.skip_whitespace(^reader);
+ x2 := io.read_u32(&reader);
+ io.skip_bytes(&reader, 1);
+ y2 := io.read_u32(&reader);
+ io.skip_whitespace(&reader);
lines << .{x1, y1, x2, y2};
}
point_count: Map(Point, u32);
- for ^line: lines {
+ for &line: lines {
for p: line_points(*line) {
if point_count->has(p) {
point_count[p] = point_count[p] + 1;
}
count := 0;
- for ^ point_count.entries {
+ for & point_count.entries {
if it.value >= 2 do count += 1;
}
main :: (args) => {
for file: os.with_file("./tests/aoc-2021/input/day06.txt") {
reader := io.reader_make(file);
- start_str := io.read_all(^reader);
+ start_str := io.read_all(&reader);
start_list := string.split(start_str, #char ",");
main :: (args) => {
for file: os.with_file("./tests/aoc-2021/input/day07.txt") {
reader := io.reader_make(file);
- nums := io.read_all(^reader)
+ nums := io.read_all(&reader)
|> string.split(#char ",")
|> iter.as_iter()
|> iter.map((x) => cast(i32) conv.parse_int(x))
solved_segments: Map(u8, u32);
left_segments := string.split(left, #char " ");
- defer memory.free_slice(^left_segments);
+ defer memory.free_slice(&left_segments);
// Look for 1.
one_data := *array.first(left_segments, (x) => x.count == 2);
sum := 0;
words := string.split(right, #char " ");
- defer memory.free_slice(^words);
- for^ words {
+ defer memory.free_slice(&words);
+ for& words {
string.strip_whitespace(it);
num_segments : [7] bool;
}
// Nicer way of printing a Map.
-#match io.write (w: ^io.Writer, x: ^Map($K, $V)) {
+#match io.write (w: &io.Writer, x: &Map($K, $V)) {
io.write(w, "{\n");
for e: x.entries {
io.write(w, " {} => {}\n", e.key, e.value);
reader := io.reader_make(file);
answer := 0;
- while !io.reader_empty(^reader) {
- line := io.read_line(^reader, consume_newline=true, inplace=true);
+ while !io.reader_empty(&reader) {
+ line := io.read_line(&reader, consume_newline=true, inplace=true);
left, right := do {
parts := string.split(line, #char "|");
- defer memory.free_slice(^parts);
+ defer memory.free_slice(&parts);
return parts[0], parts[1];
};
- string.strip_whitespace(^left);
- string.strip_whitespace(^right);
+ string.strip_whitespace(&left);
+ string.strip_whitespace(&right);
#if PART == 1 {
words := string.split(right, #char " ");
- for^ words {
+ for& words {
string.strip_whitespace(it);
switch it.count {
case 2, 3, 4, 7 do answer += 1;
use package core
Pos :: struct {x,y: i32;}
-#match hash.to_u32 (use p: Pos) => x * 13 ^ 17 * y;
+#match hash.to_u32 (use p: Pos) => x * 13 & 17 * y;
#operator == (p1, p2: Pos) => p1.x == p2.x && p1.y == p2.y;
Cell :: struct {
while queued.count != 0 {
t := queued[0];
- array.delete(^queued, 0);
+ array.delete(&queued, 0);
- top := ^heightmap[t];
+ top := &heightmap[t];
if top.height == 9 do continue;
included << t;
- array.clear(^potential);
+ array.clear(&potential);
if t.x < width - 1 && top.dx < 0 {
potential << Pos.{t.x+1,t.y};
}
return included.count;
}
-#match io.write (w: ^io.Writer, x: ^Map($K, $V)) {
+#match io.write (w: &io.Writer, x: &Map($K, $V)) {
io.write(w, "{\n");
for e: x.entries {
io.write(w, " {} => {}\n", e.key, e.value);
heightmap: Map(Pos, Cell);
height, width := 0, 0;
- while !io.reader_empty(^reader) {
- line := io.read_line(^reader, consume_newline=false, inplace=true);
- io.skip_whitespace(^reader);
+ while !io.reader_empty(&reader) {
+ line := io.read_line(&reader, consume_newline=false, inplace=true);
+ io.skip_whitespace(&reader);
for line.count {
heightmap[Pos.{it, height}] = Cell.{cast(u32) (line[it] - #char "0")};
}
for y: height - 1 do for x: width {
- map.update(^heightmap, .{x,y}) {
+ map.update(&heightmap, .{x,y}) {
it.dy = it.height - heightmap[Pos.{x,y+1}].height;
}
}
for x: width - 1 do for y: height {
- map.update(^heightmap, .{x,y}) {
+ map.update(&heightmap, .{x,y}) {
it.dx = it.height - heightmap[Pos.{x+1,y}].height;
}
}
lowest: [..] Pos;
risk_sum := 0;
for y: height do for x: width {
- h := ^heightmap[Pos.{x,y}];
+ h := &heightmap[Pos.{x,y}];
if x < width - 1 && h.dx >= 0 do continue;
if y < height - 1 && h.dy >= 0 do continue;
corrupted_score := 0;
completion_scores: [..] u64;
- while !io.reader_empty(^reader) {
- line := io.read_line(^reader, consume_newline=false, inplace=true);
- io.skip_whitespace(^reader);
+ while !io.reader_empty(&reader) {
+ line := io.read_line(&reader, consume_newline=false, inplace=true);
+ io.skip_whitespace(&reader);
char_stack: [..] u8;
- defer array.free(^char_stack);
+ defer array.free(&char_stack);
for ch: line {
switch ch {
case #char "(", #char "[", #char "<", #char "{" {
}
case #char ")", #char "]", #char ">", #char "}" {
- x := array.pop(^char_stack);
+ x := array.pop(&char_stack);
if x != ch {
// printf("Expected '{}', found '{}' instead.\n", x, ch);
corrupted_score += score_map[ch];
complete_score: u64 = 0;
while char_stack.count != 0 {
complete_score *= 5;
- switch array.pop(^char_stack) {
+ switch array.pop(&char_stack) {
case #char ")" do complete_score += 1;
case #char "]" do complete_score += 2;
case #char "}" do complete_score += 3;
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);
+ 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");
}
sync_step := 0;
while true {
step += 1;
- for ^o: octopuses do *o += 1;
+ for &o: octopuses do *o += 1;
#persist to_flash: Set(Pos);
for y: 10 do for x: 10 {
}
}
- for flash: iter.as_iter(^to_flash) {
+ for flash: iter.as_iter(&to_flash) {
for y: -1 .. 2 do for x: -1 .. 2 {
if y == 0 && x == 0 do continue;
}
}
- for flash: iter.as_iter(^to_flash) {
+ for flash: iter.as_iter(&to_flash) {
set_octopus(flash.x, flash.y, 0);
if step <= 100 do flash_count += 1;
}
verticies: Set(str);
edges: Set(Communative_Pair(str));
- while !io.reader_empty(^reader) {
- line := io.read_line(^reader, consume_newline=true);
+ while !io.reader_empty(&reader) {
+ line := io.read_line(&reader, consume_newline=true);
left, right := do {
parts := string.split(line, #char "-");
- defer memory.free_slice(^parts);
+ defer memory.free_slice(&parts);
return string.strip_whitespace(parts[0]), string.strip_whitespace(parts[1]);
};
node_stack: [..] Node;
node_stack << .{ "start", 0, false };
- children_of :: (edges: ^$T, name: str) -> Iterator(str) {
+ children_of :: (edges: &$T, name: str) -> Iterator(str) {
name_copy := make_temp(str);
*name_copy = name;
}
edge_map: Map(str, [] str);
- for v: iter.as_iter(^verticies) {
- edge_map[*v] = children_of(^edges, *v) |> iter.to_array();
+ for v: iter.as_iter(&verticies) {
+ edge_map[*v] = children_of(&edges, *v) |> iter.to_array();
}
paths_count := 0;
if cannot_visit_multiple(child) {
visit_count := 0;
- for^ node_stack {
+ for& node_stack {
if it.name == child do visit_count += 1;
}
else do node_stack << .{ child, 0, second_visit };
} else {
- array.pop(^node_stack);
+ array.pop(&node_stack);
}
}
return p1.y - p2.y;
}
-apply_fold :: (dots: ^[] Point, axis_name: str, axis_value: i32) {
- for^ *dots do switch axis_name {
+apply_fold :: (dots: &[] Point, axis_name: str, axis_value: i32) {
+ for& *dots do switch axis_name {
case "x" do if it.x > axis_value do it.x = 2 * axis_value - it.x;
case "y" do if it.y > axis_value do it.y = 2 * axis_value - it.y;
}
dots: [..] Point;
while true {
- line := io.read_line(^reader, consume_newline=true, inplace=true);
- string.strip_whitespace(^line);
+ line := io.read_line(&reader, consume_newline=true, inplace=true);
+ string.strip_whitespace(&line);
if line.count == 0 do break;
parts := string.split_iter(line, #char ",")
}
part_1_answer := -1;
- while !io.reader_empty(^reader) {
- line := io.read_line(^reader, consume_newline=true, inplace=true);
+ while !io.reader_empty(&reader) {
+ line := io.read_line(&reader, consume_newline=true, inplace=true);
- string.read_until(^line, #char " ", 1);
- string.advance(^line, 1);
+ string.read_until(&line, #char " ", 1);
+ string.advance(&line, 1);
- axis_name := string.read_until(^line, #char "=");
- string.advance(^line, 1);
+ axis_name := string.read_until(&line, #char "=");
+ string.advance(&line, 1);
axis_value := cast(i32) conv.str_to_i64(line);
- apply_fold(~~ ^dots, axis_name, axis_value);
+ apply_fold(~~ &dots, axis_name, axis_value);
if part_1_answer < 0 {
part_1_answer = dots.count;
}
}
// Nicer way of printing a Map.
-#match io.write (w: ^io.Writer, x: ^Map($K, $V)) {
+#match io.write (w: &io.Writer, x: &Map($K, $V)) {
io.write(w, "{\n");
for e: x.entries {
io.write(w, " {} => {}\n", e.key, e.value);
for file: os.with_file("./tests/aoc-2021/input/day14.txt") {
reader := io.reader_make(file);
- start_polymer := io.read_line(^reader, consume_newline=false);
- io.skip_whitespace(^reader);
+ start_polymer := io.read_line(&reader, consume_newline=false);
+ io.skip_whitespace(&reader);
rules: [..] Rule;
- while !io.reader_empty(^reader) {
+ while !io.reader_empty(&reader) {
r: Rule;
- io.read_bytes(^reader, .{cast(^u8) ^r.pair, 2});
- io.skip_bytes(^reader, 4);
- r.insert = io.read_byte(^reader);
+ io.read_bytes(&reader, .{cast(&u8) &r.pair, 2});
+ io.skip_bytes(&reader, 4);
+ r.insert = io.read_byte(&reader);
rules << r;
- io.skip_whitespace(^reader);
+ io.skip_whitespace(&reader);
}
polymer_state: Map(Pair(u8, u8), State);
add_to_state :: macro (pair: Pair(u8, u8), count: u64) {
if polymer_state->has(pair) {
- (^polymer_state[pair]).next += ~~count;
+ (&polymer_state[pair]).next += ~~count;
} else {
polymer_state[pair] = .{ 0, ~~count };
}
}
step_state :: macro () {
- for^ polymer_state.entries {
+ for& polymer_state.entries {
it.value.now = it.value.next;
it.value.next = 0;
}
step_state();
for 40 {
- for^ rule: rules {
- pair_count := ^polymer_state[rule.pair];
+ for& rule: rules {
+ pair_count := &polymer_state[rule.pair];
if pair_count != null {
if pair_count.now > 0 {
pair1 := Pair(u8, u8).{ rule.pair.first, rule.insert };
}
mode: Map(u8, u64);
- for^ polymer_state.entries {
+ for& polymer_state.entries {
mode[it.key.second] = mode[it.key.second] + it.value.now;
}
maximum := array.fold(mode.entries, cast(u64) 0, (x, y) => math.max(x.value, y));
minimum := array.fold(mode.entries, maximum, (x, y) => math.min(x.value, y));
- println(^mode);
+ println(&mode);
printf("Part 2: {}\n", maximum - minimum - 1);
}
}
cells: [..] u8;
height := 0;
- while !io.reader_empty(^reader) {
- line := io.read_line(^reader, consume_newline=false, inplace=true);
- io.skip_whitespace(^reader);
+ while !io.reader_empty(&reader) {
+ line := io.read_line(&reader, consume_newline=false, inplace=true);
+ io.skip_whitespace(&reader);
for line do cells << it;
height += 1;
minimum := 0;
while to_try.data.count != 0 {
- try := heap.remove_top(^to_try);
+ try := heap.remove_top(&to_try);
tried << .{try.x, try.y};
cell_value := cast(u32) (cells[(try.y % height) * width + (try.x % width)] - #char "0");
value_idx: u32;
bit_idx: i32;
- next :: (b: ^BitIterator) -> u32 {
+ next :: (b: &BitIterator) -> u32 {
v, _ := iter.take_one(b.iter, no_close=true);
return v;
}
}
-bit_iterator :: (values: [] u8) -> ^BitIterator {
+bit_iterator :: (values: [] u8) -> &BitIterator {
c := new(BitIterator);
c.iter = .{ c, next, cfree };
c.values = values;
c.value_idx = 0;
c.bit_idx = 7;
- next :: (use c: ^BitIterator) -> (u32, bool) {
+ next :: (use c: &BitIterator) -> (u32, bool) {
if value_idx >= values.count do return 0, false;
defer {
return c;
}
-read_bits :: (bit_provider: ^BitIterator, count: u32) -> u32 {
+read_bits :: (bit_provider: &BitIterator, count: u32) -> u32 {
res := 0;
for count {
res *= 2;
Packet_Operator :: struct {
use base: Packet;
- subpackets: [] ^Packet;
+ subpackets: [] &Packet;
}
-parse_packet :: (bit_provider: ^BitIterator) -> ^Packet {
+parse_packet :: (bit_provider: &BitIterator) -> &Packet {
version := read_bits(bit_provider, 3);
type := read_bits(bit_provider, 3);
}
case #default {
- packets: [..] ^Packet;
+ packets: [..] &Packet;
l_type := read_bits(bit_provider, 1);
if l_type == 0 {
}
}
-packet_sum_version :: (p: ^Packet) -> u64 {
+packet_sum_version :: (p: &Packet) -> u64 {
switch p.type {
case 4 do return ~~p.version;
case #default {
sum: u64;
- for (cast (^Packet_Operator) p).subpackets {
+ for (cast (&Packet_Operator) p).subpackets {
sum += packet_sum_version(it);
}
}
}
-packet_reduce :: (p: ^Packet) -> u64 {
- op := cast(^Packet_Operator) p;
+packet_reduce :: (p: &Packet) -> u64 {
+ op := cast(&Packet_Operator) p;
switch p.type {
case 0 {
return array.fold(op.subpackets, cast(u64) 0, (x, y) => y + packet_reduce(x));
}
case 4 {
- return (cast(^Packet_Literal) p).value;
+ return (cast(&Packet_Literal) p).value;
}
case 5 {
main :: (args) => {
for file: os.with_file("./tests/aoc-2021/input/day16.txt") {
reader := io.reader_make(file);
- line := io.read_line(^reader, consume_newline=false, inplace=true);
+ line := io.read_line(&reader, consume_newline=false, inplace=true);
transmission: [..] u8;
for i: range.{ 0, line.count, 2 } {
for file: os.with_file("./tests/aoc-2021/input/day17.txt") {
reader := io.reader_make(file);
- io.skip_bytes(^reader, 15);
- tx0 = io.read_i32(^reader);
- io.skip_bytes(^reader, 2);
- tx1 = io.read_i32(^reader);
- io.skip_bytes(^reader, 4);
- ty0 = io.read_i32(^reader);
- io.skip_bytes(^reader, 2);
- ty1 = io.read_i32(^reader);
+ io.skip_bytes(&reader, 15);
+ tx0 = io.read_i32(&reader);
+ io.skip_bytes(&reader, 2);
+ tx1 = io.read_i32(&reader);
+ io.skip_bytes(&reader, 4);
+ ty0 = io.read_i32(&reader);
+ io.skip_bytes(&reader, 2);
+ ty1 = io.read_i32(&reader);
}
max := 0;
num_allocator: Allocator;
SnailNum :: struct {
- parent: ^SnailNum;
- left, right: ^SnailNum;
+ parent: &SnailNum;
+ left, right: &SnailNum;
left_val, right_val: u32;
}
return n;
}
- clone :: (n: ^SnailNum) -> ^SnailNum {
+ clone :: (n: &SnailNum) -> &SnailNum {
if !n do return null;
new_num := SnailNum.make();
return new_num;
}
- add :: (a, b: ^SnailNum) => {
+ add :: (a, b: &SnailNum) => {
if !a do return b;
if !b do return a;
return new_root;
}
- reduce :: (use n: ^SnailNum) -> (reduced_something: bool) {
+ reduce :: (use n: &SnailNum) -> (reduced_something: bool) {
if r, _ := n->reduce_explodes(); r do return true;
if r := n->reduce_splits(); r do return true;
return false;
}
- reduce_explodes :: (use n: ^SnailNum, depth := 0) -> (reduced_something: bool, zero_node: bool) {
+ reduce_explodes :: (use n: &SnailNum, depth := 0) -> (reduced_something: bool, zero_node: bool) {
if depth <= 3 {
if left != null {
if did_reduce, zero_node := left->reduce_explodes(depth + 1); zero_node {
return true, true;
}
- reduce_splits :: (use n: ^SnailNum) -> (reduced_something: bool) {
+ reduce_splits :: (use n: &SnailNum) -> (reduced_something: bool) {
if left {
if left->reduce_splits() {
return true;
}
}
- set_left :: (parent, new_left: ^SnailNum) {
+ set_left :: (parent, new_left: &SnailNum) {
parent.left_val = 0;
parent.left = new_left;
if new_left do new_left.parent = parent;
}
- set_right :: (parent, new_right: ^SnailNum) {
+ set_right :: (parent, new_right: &SnailNum) {
parent.right_val = 0;
parent.right = new_right;
if new_right do new_right.parent = parent;
}
- number_to_left :: (n: ^SnailNum) -> ^u32 {
+ number_to_left :: (n: &SnailNum) -> &u32 {
while n.parent && n.parent.left == n {
n = n.parent;
}
if !n.parent do return null;
- if !n.parent.left do return ^n.parent.left_val;
+ if !n.parent.left do return &n.parent.left_val;
n = n.parent.left;
n = n.right;
}
- return ^n.right_val;
+ return &n.right_val;
}
- number_to_right :: (n: ^SnailNum) -> ^u32 {
+ number_to_right :: (n: &SnailNum) -> &u32 {
while n.parent && n.parent.right == n {
n = n.parent;
}
if !n.parent do return null;
- if !n.parent.right do return ^n.parent.right_val;
+ if !n.parent.right do return &n.parent.right_val;
n = n.parent.right;
n = n.left;
}
- return ^n.left_val;
+ return &n.left_val;
}
- magnitude :: (use n: ^SnailNum) => {
+ magnitude :: (use n: &SnailNum) => {
if !n {
return 0;
}
+ 2 * (right_val + right->magnitude());
}
- parse :: (line: ^str) -> ^SnailNum {
+ parse :: (line: &str) -> &SnailNum {
string.advance(line); // [
root := SnailNum.make();
return root;
}
- format :: (output: ^conv.Format_Output, s: ^conv.Format, use n: ^SnailNum) {
+ format :: (output: &conv.Format_Output, s: &conv.Format, use n: &SnailNum) {
if !left && !right {
conv.format(output, "[{},{}]", left_val, right_val);
}
main :: () {
num_arena := arena.make(context.allocator, 256 * 1024);
- SnailNum.allocator = alloc.as_allocator(^num_arena);
+ SnailNum.allocator = alloc.as_allocator(&num_arena);
conv.register_custom_formatter(SnailNum.format);
r := io.reader_make(file);
#if PART == 1 {
- s: ^SnailNum = null;
+ s: &SnailNum = null;
for line: r->lines() {
- n := SnailNum.parse(^line);
+ n := SnailNum.parse(&line);
s = s->add(n);
}
}
#if PART == 2 {
- nums := make([..] ^SnailNum);
+ nums := make([..] &SnailNum);
for line: r->lines() {
- nums << SnailNum.parse(^line);
+ nums << SnailNum.parse(&line);
}
maximum := 0;
return .{ 1, 0 };
}
- roll :: (d: ^Die) -> u32 {
+ roll :: (d: &Die) -> u32 {
defer d.value += 1;
defer d.rolls += 1;
return d.value;
}
#inject Player {
- move :: (p: ^Player, squares: u32) {
+ move :: (p: &Player, squares: u32) {
p.square += squares;
p.square %= 10;
_, s1 := string.bisect(l1, #char ":");
_, s2 := string.bisect(l2, #char ":");
- string.strip_whitespace(^s1);
- string.strip_whitespace(^s2);
+ string.strip_whitespace(&s1);
+ string.strip_whitespace(&s2);
p1.square = ~~ (conv.str_to_i64(s1) - 1);
p2.square = ~~ (conv.str_to_i64(s2) - 1);
losing_score: u32;
while true {
- play :: macro (player, opponent: ^Player) {
+ play :: macro (player, opponent: &Player) {
r1 := die->roll();
r2 := die->roll();
r3 := die->roll();
}
}
- play(^p1, ^p2);
- play(^p2, ^p1);
+ play(&p1, &p2);
+ play(&p2, &p1);
}
println(losing_score * die.rolls);
w1, w2: u64;
if player_1_turn {
- for^ cases {
+ for& cases {
n1, n2 := calc_wins_memo(
p1->move(it.spaces),
p2,
}
} else {
- for^ cases {
+ for& cases {
n1, n2 := calc_wins_memo(
p1,
p2->move(it.spaces),
_, s1 := string.bisect(l1, #char ":");
_, s2 := string.bisect(l2, #char ":");
- string.strip_whitespace(^s1);
- string.strip_whitespace(^s2);
+ string.strip_whitespace(&s1);
+ string.strip_whitespace(&s2);
p1.square = cast(i32) (conv.str_to_i64(s1) - 1);
p2.square = cast(i32) (conv.str_to_i64(s2) - 1);
Vec2 :: struct { x: i32; y: i32; }
// Overload print() to print Vec2's.
-#match io.write (use writer: ^io.Writer, use v: Vec2) {
+#match io.write (use writer: &io.Writer, use v: Vec2) {
io.write_format(writer, "Vec2({}, {})", x, y);
}
vecs : [4] Vec2;
i := n;
- for ^v: vecs {
+ for &v: vecs {
v.x = i * i;
v.y = i * i * i;
i += 1;
{
println("Array of structs on the stack.");
vecs : [8] Vec2;
- for ^v: vecs {
+ for &v: vecs {
v.x = 4039;
v.y = 7782;
}
{
println("Array of structs from function call.");
vecs := calc_vecs();
- for ^v: vecs do println(*v);
+ for &v: vecs do println(*v);
}
// Array of structs on a struct
nums : [100] u32;
i := 1;
- for ^n: nums {
+ for &n: nums {
*n = i * i;
i += 5;
}
println("2D-array from an allocation to a function.");
vecs_data: [2][4] Vec2;
- vecs := ^vecs_data; // equivalent of heap allocating it
+ vecs := &vecs_data; // equivalent of heap allocating it
- set_vecs :: (vecs: ^[2][4] Vec2) {
+ set_vecs :: (vecs: &[2][4] Vec2) {
i := 0;
- for ^row: *vecs {
- for ^v: *row {
+ for &row: *vecs {
+ for &v: *row {
*v = .{ 1000 + i, 2000 + i * i };
i += 1;
}
set_vecs(vecs);
- print_vecs :: (vecs: ^[4] Vec2) {
- for ^v: *vecs do println(*v);
+ print_vecs :: (vecs: &[4] Vec2) {
+ for &v: *vecs do println(*v);
}
- print_vecs(^(*vecs)[0]);
- print_vecs(^(*vecs)[1]);
+ print_vecs(&(*vecs)[0]);
+ print_vecs(&(*vecs)[1]);
}
}
add :: (v1, v2: V2) => V2.{v1.x+v2.x, v1.y+v2.y};
- add_inplace :: (v1: ^V2, v2: V2) {
+ add_inplace :: (v1: &V2, v2: V2) {
v1.x += v2.x;
v1.y += v2.y;
}
- format :: (output: ^conv.Format_Output, _: ^conv.Format, v: ^V2) {
+ format :: (output: &conv.Format_Output, _: &conv.Format, v: &V2) {
conv.format(output, "({}, {})", v.x, v.y);
}
}
print_from_other_thread :: (sd) => {
// Creating high contention for the shared resource
for i: 10000 {
- sync.scoped_mutex(^shared_mutex);
- for ^v: sd.arr {
+ sync.scoped_mutex(&shared_mutex);
+ for &v: sd.arr {
*v += 1;
}
}
}
main :: (args) => {
- sync.mutex_init(^shared_mutex);
+ sync.mutex_init(&shared_mutex);
test_var = 1234;
println(test_var);
array.fill(sd.arr, 0);
threads : [4] thread.Thread;
- for ^t: threads {
- thread.spawn(t, ^sd, print_from_other_thread);
+ for &t: threads {
+ thread.spawn(t, &sd, print_from_other_thread);
printf("Spawned thread {}\n", t.id);
}
- memory.alloc_slice(^partial_arr, 10);
+ memory.alloc_slice(&partial_arr, 10);
for i: 10 do partial_arr[i] = i;
printf("Waiting...\n");
- for ^t: threads {
+ for &t: threads {
thread.join(t);
printf("Thread {} joined!\n", t.id);
}
use package core
-dumb :: (it: Iterator, m: ^Map) {
+dumb :: (it: Iterator, m: &Map) {
println(it.Iter_Type);
println(__autopoly_var_0);
println(typeof *m);
vec_add :: (v1: Vector2, v2: typeof v1) => (typeof v1).{ v1.x + v2.x, v1.y + v2.y };
-mappable :: (k: m.Key_Type, m: ^Map, x: $T) {
+mappable :: (k: m.Key_Type, m: &Map, x: $T) {
printf("{} to {}\n", m.Key_Type, m.Value_Type);
println(k);
println(x);
value: type;
}
-f :: (thing: ^Thing, other: typeof thing) {
+f :: (thing: &Thing, other: typeof thing) {
printf("type of thing is {}\n", typeof thing);
printf("thing is {}\n", *thing);
}
main :: (args) => {
m: Map(i32, str);
- dumb(iter.as_iter(1 .. 10), ^m);
+ dumb(iter.as_iter(1 .. 10), &m);
V :: Vector2(i32)
v1 := V.{ 1, 0 };
v2 := V.{ 0, 1 };
println(vec_add(v1, v2));
- mappable(1234, ^m, "WOOT WOOT!!!");
+ mappable(1234, &m, "WOOT WOOT!!!");
t: Thing(f32);
- f(^t, ^t);
+ f(&t, &t);
for iter.as_iter(0 .. 100) |> iter.map(x => x * 2) |> iter.filter(x => x <= 10) {
println(it);
use package core
main :: () {
- tree: ^avl_tree.AVL_Tree(i32);
+ tree: &avl_tree.AVL_Tree(i32);
- avl_tree.insert(^tree, 20);
- avl_tree.insert(^tree, 5);
- avl_tree.insert(^tree, 8);
- avl_tree.insert(^tree, 25);
- avl_tree.insert(^tree, 0);
- avl_tree.insert(^tree, 15);
- avl_tree.insert(^tree, 10);
+ avl_tree.insert(&tree, 20);
+ avl_tree.insert(&tree, 5);
+ avl_tree.insert(&tree, 8);
+ avl_tree.insert(&tree, 25);
+ avl_tree.insert(&tree, 0);
+ avl_tree.insert(&tree, 15);
+ avl_tree.insert(&tree, 10);
avl_tree.print(tree);
print("\n");
}
alloc_slice :: ($T: type_expr, N: i32) -> [] T {
- data := cast(^T) calloc(sizeof T * N);
+ data := cast(&T) calloc(sizeof T * N);
return .{ data, N };
}
count_to(UP_TO);
sl := alloc_slice(i32, 10);
- for ^ sl do *it = 1234;
+ for & sl do *it = 1234;
for sl do println(it);
// This is so much cleaner than the alternative.
ages := map.make(str, u32, default=0);
- defer delete(^ages);
+ defer delete(&ages);
- map.put(^ages, "Dwight", 32);
- map.put(^ages, "Jim", 25);
- map.put(^ages, "Pam", 24);
+ map.put(&ages, "Dwight", 32);
+ map.put(&ages, "Jim", 25);
+ map.put(&ages, "Pam", 24);
- print_age :: (ages: ^map.Map(str, u32), name: str) {
+ print_age :: (ages: &map.Map(str, u32), name: str) {
age := map.get(ages, name);
printf("{}'s age is {}.\n", name, age);
}
- print_age(^ages, "Dwight");
- print_age(^ages, "Jim");
- print_age(^ages, "Pam");
- print_age(^ages, "Michael");
+ print_age(&ages, "Dwight");
+ print_age(&ages, "Jim");
+ print_age(&ages, "Pam");
+ print_age(&ages, "Michael");
}
sum += *it;
});
- for it: bucket_array.as_iter(^ba) {
+ for it: bucket_array.as_iter(&ba) {
printf("{}\n", it);
}
main :: (args) => {
v: S;
- array.init(^v.some_array);
+ array.init(&v.some_array);
- do_something_with_S(^v);
+ do_something_with_S(&v);
- printf("{*p}\n", ^v);
+ printf("{*p}\n", &v);
}
-do_something_with_S :: (s: ^S) {
+do_something_with_S :: (s: &S) {
s.some_array << .{ "Joe", 14 };
}
use core
-q :: (v: ^$C, g: (^C) -> $T) {
+q :: (v: &$C, g: (&C) -> $T) {
println(*v);
println(g(v));
println(T);
main :: () {
v := 5;
- q(^v, _ => false);
+ q(&v, _ => false);
}
S << Point.{ 2, 3 };
S << Point.{ 1, 2 };
- set.has(^S, Point.{ 1, 2 }) |> println();
+ set.has(&S, Point.{ 1, 2 }) |> println();
}
use core
Foo :: struct {
- use vtable: ^VTable;
+ use vtable: &VTable;
x: u32;
VTable :: struct {
- f: (^Foo) -> void;
+ f: (&Foo) -> void;
}
}
}
}
-make_foo :: () -> ^Foo {
+make_foo :: () -> &Foo {
foo := new(Foo);
- foo.vtable = ^good_foo;
+ foo.vtable = &good_foo;
foo.x = 1234;
printf("Made foo!\n");
main :: () {
make_foo()->f();
- Foo.{^good_foo, 5678}->f();
+ Foo.{&good_foo, 5678}->f();
}
Something :: struct {
member := cast(u32) 1234;
- method :: (use s: ^Something) {
+ method :: (use s: &Something) {
using_callsite(member);
}
}
Test_Thing :: struct {
- vt : ^Test_VTable;
+ vt : &Test_VTable;
data : i32;
}
main :: (args: [] cstr) {
dummy();
- t := Test_Thing.{ ^test_vtable1, 10 };
- println(do_a_thing(^t));
+ t := Test_Thing.{ &test_vtable1, 10 };
+ println(do_a_thing(&t));
- t.vt = ^test_vtable2;
- println(do_a_thing(^t));
+ t.vt = &test_vtable2;
+ println(do_a_thing(&t));
tmp_vt := Test_VTable.{ first = test_vtable1.second, second = test_vtable2.first, third = math.max_poly };
- t.vt = ^tmp_vt;
- println(do_a_thing(^t));
+ t.vt = &tmp_vt;
+ println(do_a_thing(&t));
println(t.vt.first == test_vtable1.second);
- do_a_thing :: (use t: ^Test_Thing) -> i32 {
+ do_a_thing :: (use t: &Test_Thing) -> i32 {
if vt.second == null_proc || vt.first == null_proc do return data;
res := data |> vt.second() |> vt.first();
}
Guy :: struct {
- best_friend: ^Guy;
+ best_friend: &Guy;
favorite_color: Color;
}
nightmare :: (x: (typeof(z.best_friend), $K) -> void,
y: (typeof(x)) -> $R,
- z: ^$SomeType,
+ z: &$SomeType,
w: R) {
k := y(x);
}
printf("dummy1 got T={}, typeof(a.favorite_color)={}\n", a, typeof(b));
}
- dummy2 :: (f: (a: ^Guy, b: $C) -> void) -> C {
+ dummy2 :: (f: (a: &Guy, b: $C) -> void) -> C {
printf("dummy2 got C={}\n", C);
other_guy := new(Guy);
main :: (args: [] cstr) {
imap : Map(i32, str);
- map.init(^imap, "");
+ map.init(&imap, "");
defer {
print("Freeing map\n");
- map.free(^imap);
+ map.free(&imap);
}
- map.put(^imap, 50, "Hello ");
+ map.put(&imap, 50, "Hello ");
imap->put(1234, "World!");
- println(map.has(^imap, 50));
- println(map.has(^imap, 51));
+ println(map.has(&imap, 50));
+ println(map.has(&imap, 51));
- printf("{}{}\n", map.get(^imap, 50), map.get(^imap, 1234));
+ printf("{}{}\n", map.get(&imap, 50), map.get(&imap, 1234));
- printf("{*p}\n", ^imap);
+ printf("{*p}\n", &imap);
- map.delete(^imap, 50);
- println(map.has(^imap, 50));
+ map.delete(&imap, 50);
+ println(map.has(&imap, 50));
- map.clear(^imap);
- println(map.has(^imap, 1234));
+ map.clear(&imap);
+ println(map.has(&imap, 1234));
}
{
arr: [..] i32;
- printf("{*p}\n", cast(^array.Untyped_Array) ^arr);
+ printf("{*p}\n", cast(&array.Untyped_Array) &arr);
people: Map(str, i32);
people["Joe"] = 12;
people["Jane"] = 34;
- printf("{*p}\n", ^people.entries);
+ printf("{*p}\n", &people.entries);
}
}
\ No newline at end of file
Complex :: struct {
x, y: f32;
- format :: (output: ^conv.Format_Output, format: ^conv.Format, c: ^Complex) {
+ format :: (output: &conv.Format_Output, format: &conv.Format, c: &Complex) {
conv.format(output, "{.2} + {.2}i", c.x, c.y);
}
}
// everything WAY more complicated.
consume_inner :: #match {
// Iterator over pointers get dereferenced
- macro (iter: Iterator(^$T)) -> [..] T {
+ macro (iter: Iterator(&$T)) -> [..] T {
arr := array.make(T);
for iter do arr << *it;
return arr;
// println(bit_math(3.0, 4.0));
a := consume(f32.[4,2,4,7,4]);
- defer array.free(^a);
+ defer array.free(&a);
println(a);
consume(1 .. 10) |> println();
count_iterator :: (lo: $T, hi: T, step: T = 1) -> Iterator(T) {
return iter.generator(
- ^.{ low = lo, high = hi, step = step, current = lo },
+ &.{ low = lo, high = hi, step = step, current = lo },
(ctx) => {
if ctx.current <= ctx.high {
array_print(farr);
array_print :: (arr: [$N] $T) {
- for ^e: arr {
+ for &e: arr {
print(*e);
print(" ");
}
member1 := get_a_value(f32);
member2 := get_a_value(i32);
- get_member2_plus_three :: (foo: ^Foo) -> i32 {
+ get_member2_plus_three :: (foo: &Foo) -> i32 {
return foo.member2 + 3;
}
age : i32;
}
- get_member2_plus_three :: (foo: ^$T) -> f32 {
+ get_member2_plus_three :: (foo: &$T) -> f32 {
return foo.member2 + 3;
}
};
for 40 {
- step_physics(^object, 0.01);
+ step_physics(&object, 0.01);
printf("{}\n", object);
}
}
sum := 0;
thread_data := .{
- sum = ^sum,
+ sum = &sum,
mutex = sync.Mutex.{},
};
- sync.mutex_init(^thread_data.mutex);
+ sync.mutex_init(&thread_data.mutex);
- iter.parallel_for(0 .. 10000, 4, ^thread_data) {
- sync.scoped_mutex(^thread_data.mutex);
+ iter.parallel_for(0 .. 10000, 4, &thread_data) {
+ sync.scoped_mutex(&thread_data.mutex);
*thread_data.sum += it;
}
cached_fib :: (n: u64) -> u64 {
#persist cache : map.Map(u64, u64);
- _ :: #init () { map.init(^cache); };
+ _ :: #init () { map.init(&cache); };
if n <= 1 do return n;
- if map.has(^cache, n) do return map.get(^cache, n);
+ if map.has(&cache, n) do return map.get(&cache, n);
res := cached_fib(n - 1) + cached_fib(n - 2);
- map.put(^cache, n, res);
+ map.put(&cache, n, res);
print_cache_size();
nps : NewPolyStruct(i32, 4);
- for ^x: nps.x do *x = 12345;
- for ^y: nps.y do *y = 67890;
+ for &x: nps.x do *x = 12345;
+ for &y: nps.y do *y = 67890;
for x: nps.x do println(x);
println(x);
- for iter.as_iter(^x) {
+ for iter.as_iter(&x) {
if it % 2 == 0 {
#remove;
}
S := set.make(i32);
- set.insert(^S, 5);
- set.insert(^S, 5);
- set.insert(^S, 6);
+ set.insert(&S, 5);
+ set.insert(&S, 5);
+ set.insert(&S, 6);
for entry: S.entries do println(entry.value);
- println(set.has(^S, 5));
+ println(set.has(&S, 5));
println("--------------");
- set.remove(^S, 5);
+ set.remove(&S, 5);
for entry: S.entries do println(entry.value);
- println(set.has(^S, 5));
+ println(set.has(&S, 5));
}
\ No newline at end of file
some_string := "This is a test string that can be read.\n\n\n\n\n\n1234 4567";
sstream := io.buffer_stream_make(some_string);
- sreader := io.reader_make(^sstream);
+ sreader := io.reader_make(&sstream);
for i: 0 .. 2 {
- word := io.read_word(^sreader, allocator=context.temp_allocator);
+ word := io.read_word(&sreader, allocator=context.temp_allocator);
println(word);
}
- println(io.read_line(^sreader, consume_newline=false, allocator=context.temp_allocator));
+ println(io.read_line(&sreader, consume_newline=false, allocator=context.temp_allocator));
- a := io.read_u32(^sreader);
- b := io.read_u32(^sreader);
+ a := io.read_u32(&sreader);
+ b := io.read_u32(&sreader);
println(a);
println(b);
use package core
Person_Vtable :: struct {
- greet: (^Person) -> void;
+ greet: (&Person) -> void;
}
Person :: struct {
- use vtable: ^Person_Vtable;
+ use vtable: &Person_Vtable;
name: str;
}
nice_person := Person_Vtable.{
- greet = (use p: ^Person) {
+ greet = (use p: &Person) {
printf("Hello, I am {}!\n", name);
}
}
mean_person := Person_Vtable.{
- greet = (use p: ^Person) {
+ greet = (use p: &Person) {
printf("Go away!! {}\n", typeof greet);
}
}
main :: (args) => {
- billy := Person.{ ^nice_person, "Billy" };
- charles := Person.{ ^mean_person, "Charles" };
+ billy := Person.{ &nice_person, "Billy" };
+ charles := Person.{ &mean_person, "Charles" };
billy->greet();
charles->greet();
// The right way to do it is this:
// x := * misc.any_as(va[i], i32);
- x := *cast(^i32, va[i].data);
+ x := *cast(&i32, va[i].data);
println(x);
}
}