use package core
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day1.txt";
contents_orig := contents;
defer cfree(contents_orig.data);
- nums: [..] u32;
- array.init(^nums, 128);
+ nums := array.make(u32, 128);
defer array.free(^nums);
while num := 1; num > 0 {
use package core
use package core.string.reader as reader
-count_ending_paths :: proc (nums: [..] u32) -> u64 {
- tally: [..] u64;
- array.init(^tally, nums.count);
+count_ending_paths :: (nums: [..] u32) -> u64 {
+ tally := array.make(u64, nums.count);
defer array.free(^tally);
for ^t: tally do *t = 0;
return tally[0];
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day10.txt";
file := reader.make(contents);
- nums: [..] u32;
- array.init(^nums);
+ nums := array.make(u32);
defer array.free(^nums);
while !reader.empty(^file) {
SeatState :: enum (u8) { OOB; Floor; Empty; Occupied; }
-gos_get_seat :: proc (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 :: proc (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 :: proc (use gos: ^GameOfSeats) -> bool {
+gos_iter :: (use gos: ^GameOfSeats) -> bool {
for i: 0 .. seats.count do temp[i] = seats[i];
changed := false;
fy : i32 = 0;
}
-rotate_left :: proc (ship: ^Ship) {
+rotate_left :: (ship: ^Ship) {
ofx := ship.fx;
ofy := ship.fy;
ship.fy = -ofx;
}
-rotate_right :: proc (ship: ^Ship) {
+rotate_right :: (ship: ^Ship) {
ofx := ship.fx;
ofy := ship.fy;
ship.fy = ofx;
}
-turn_around :: proc (ship: ^Ship) {
+turn_around :: (ship: ^Ship) {
ship.fx = -ship.fx;
ship.fy = -ship.fy;
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day12.txt";
file := reader.make(contents);
use package core
use package core.string.reader as reader
-inv_mod :: proc (a_: i64, m_: i64) -> i64 {
+inv_mod :: (a_: i64, m_: i64) -> i64 {
if m_ <= 1 do return 0;
a := a_; m := m_;
return y;
}
-chinese_remainder_theorem :: proc (mods: [..] i64, rems: [..] i64) -> i64 {
+chinese_remainder_theorem :: (mods: [..] i64, rems: [..] i64) -> i64 {
N: i64 = 1;
for n: mods do N *= n;
return res % N;
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day13.txt";
file := reader.make(contents);
est := reader.read_u32(^file);
reader.advance_line(^file);
- buses: [..] i64;
- array.init(^buses);
+ buses := array.make(i64);
defer array.free(^buses);
- rems: [..] i64;
- array.init(^rems);
+ rems := array.make(i64);
defer array.free(^rems);
offset: i64 = 0;
Bitmask :: #type [MASK_SIZE] u8;
// Part 1
-bitmask_p1 :: proc (mask: Bitmask, val: u64) -> u64 {
+bitmask_p1 :: (mask: Bitmask, val: u64) -> u64 {
res: u64 = 0;
bit: u64 = 1;
for m: mask {
done : bool;
}
-bitmask_iter_make :: proc (mask: Bitmask, val: u64) -> BitmaskIter {
+bitmask_iter_make :: (mask: Bitmask, val: u64) -> BitmaskIter {
bmi : BitmaskIter;
bmi.done = false;
return bmi;
}
-bitmask_iter_free :: proc (use bmi: ^BitmaskIter) {
+bitmask_iter_free :: (use bmi: ^BitmaskIter) {
array.free(^floating_indicies);
}
-bitmask_iter_done :: proc (use bmi: ^BitmaskIter) -> bool do return done;
+bitmask_iter_done :: (use bmi: ^BitmaskIter) -> bool do return done;
-bitmask_iter_next :: proc (use bmi: ^BitmaskIter) -> u64 {
+bitmask_iter_next :: (use bmi: ^BitmaskIter) -> u64 {
{
for ind: floating_indicies {
is_set := (val & (1 << cast(u64) ind)) != 0;
return val;
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day14.txt";
file := reader.make(contents);
- mem : map.Map(u64, u64);
- map.init(^mem, 0, 257);
+ mem := map.make(u64, u64, default=0, hash_count=257);
defer map.free(^mem);
mask : Bitmask;
previous : u32 = 0;
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
// The current implementation of Map is rather slow at a large scale.
// 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.Map(u32, spoken_times);
- map.init(^nums, spoken_times.{}, 32767);
+ nums := map.make(u32, spoken_times, .{}, 32767);
defer map.free(^nums);
turn := 1;
last_num := 0;
for n: initial_numbers {
- map.put(^nums, n, spoken_times.{ recent = turn });
+ map.put(^nums, n, .{ recent = turn });
turn += 1;
last_num = n;
}
upper1 : u32 = 0;
}
-field_valid :: proc (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 :: proc (file: ^reader.StringReader, fields: [..] Field, ticket_store: ^u32) -> bool {
+read_ticket_and_validate :: (file: ^reader.StringReader, fields: [..] Field, ticket_store: ^u32) -> bool {
retval := true;
for i: 0 .. fields.count {
return retval;
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day16.txt";
file := reader.make(contents);
- fields : [..] Field;
- array.init(^fields, 20);
+ fields := array.make(Field, 20);
defer array.free(^fields);
// Read until the first empty line
for i: 0 .. 2 do reader.advance_line(^file);
- my_ticket : [..] u32;
- array.init(^my_ticket, fields.count);
+ my_ticket := array.make(u32, fields.count);
defer array.free(^my_ticket);
read_ticket_and_validate(^file, fields, my_ticket.data);
for i: 0 .. 2 do reader.advance_line(^file);
- ticket_data : [..] u32;
- array.init(^ticket_data, 1024);
+ ticket_data := array.make(u32, 1024);
defer array.free(^ticket_data);
while !reader.empty(^file) {
printf("Scanning error: %i\n", total_scanning_error);
- cols_to_assign : [..] u32;
- array.init(^cols_to_assign, fields.count);
+ cols_to_assign := array.make(u32, fields.count);
defer array.free(^cols_to_assign);
for i: 0 .. fields.count do array.push(^cols_to_assign, i);
use package core.string.reader as reader
CubePos :: struct {
- x : i32;
- y : i32;
- z : i32;
- w : i32;
+ x, y, z, w : i32;
}
CubeState :: struct {
- alive : bool = false;
- next : bool = false;
+ alive := false;
+ next := false;
}
proc (c: CubePos) -> u32 #add_overload map.hash_function {
&& (a.w == b.w);
}
-get_neighbor_count :: proc (cubes: ^map.Map(CubePos, CubeState), pos: CubePos) -> u32 {
+get_neighbor_count :: (cubes: ^map.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 := reader.make(contents);
- cubes : map.Map(CubePos, CubeState);
- map.init(^cubes, CubeState.{}, 1021);
+ cubes := map.make(CubePos, CubeState, .{}, 1021);
defer map.free(^cubes);
z := 0;
x := 0;
for ch: line {
- if ch == #char "#" do map.put(^cubes, CubePos.{ x, 0, z, 0 }, CubeState.{ alive = true });
+ if ch == #char "#" do map.put(^cubes, .{ x, 0, z, 0 }, .{ alive = true });
x += 1;
}
z += 1;
}
- cubes_to_consider : [..] CubePos;
- array.init(^cubes_to_consider);
+ cubes_to_consider := array.make(CubePos);
defer array.free(^cubes_to_consider);
for i: 0 .. 6 {
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, CubePos.{
+ array.push(^cubes_to_consider, .{
cube_entry.key.x + x,
cube_entry.key.y + y,
cube_entry.key.z + z,
use package core
use package core.string.reader as reader
-parse_factor :: proc (file: ^reader.StringReader) -> u64 {
+parse_factor :: (file: ^reader.StringReader) -> u64 {
reader.skip_whitespace(file);
switch *file.data {
return 0;
}
-parse_expression_add :: proc (file: ^reader.StringReader) -> u64 {
+parse_expression_add :: (file: ^reader.StringReader) -> u64 {
reader.skip_whitespace(file);
left := parse_factor(file);
return left;
}
-parse_expression_mul :: proc (file: ^reader.StringReader) -> u64 {
+parse_expression_mul :: (file: ^reader.StringReader) -> u64 {
reader.skip_whitespace(file);
left := parse_expression_add(file);
return left;
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day18.txt";
file := reader.make(contents);
max_terminal : u32;
}
-grammar_init :: proc (use g: ^Grammar) {
+grammar_init :: (use g: ^Grammar) {
array.init(^terminate_rules);
array.init(^unit_rules);
array.init(^production_rules);
max_terminal = 0;
}
-grammar_free :: proc (use g: ^Grammar) {
+grammar_free :: (use g: ^Grammar) {
array.free(^terminate_rules);
array.free(^unit_rules);
array.free(^production_rules);
}
-grammar_prepare :: proc (use g: ^Grammar) {
+grammar_prepare :: (use g: ^Grammar) {
// Not full-proof, but good enough for AOC.
for ^solo: unit_rules {
for ^prod: production_rules {
}
}
- array.sort(^terminate_rules, proc (a: Term, b: Term) -> i32 {
+ array.sort(^terminate_rules, (a: Term, b: Term) -> i32 {
return (cast(i32) a.nt) - (cast(i32) b.nt);
});
- array.sort(^production_rules, proc (a: Prod, b: Prod) -> i32 {
+ array.sort(^production_rules, (a: Prod, b: Prod) -> i32 {
return (cast(i32) a.nt0) - (cast(i32) b.nt0);
});
terminate_rules[terminate_rules.count - 1].nt) + 1;
}
-cyk_algorithm :: proc (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;
return T[(input.count - 1) * dim_0];
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day19.txt";
file := reader.make(contents);
use package core
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day2.txt";
valid := 0;
lo : u32;
hi : u32;
ch : u8;
- pw : str = "";
+ pw := "";
while true {
string.read_u32(^contents, ^lo);
pos_x : u32 = 0;
pos_y : u32 = 0;
- edges_match : Sides = Sides.{};
+ edges_match : Sides = .{};
}
Sides :: struct {
- top : SideRelation = SideRelation.{};
- right : SideRelation = SideRelation.{};
- bottom : SideRelation = SideRelation.{};
- left : SideRelation = SideRelation.{};
+ top : SideRelation = .{};
+ right : SideRelation = .{};
+ bottom : SideRelation = .{};
+ left : SideRelation = .{};
}
SideRelation :: struct {
// TOT[t0][t1] = t1 * t0; t1 after t0
tile_orientation_table := (#type [8] TO).[
- // N R90 R180 R270 F FR90 FR180 FR270
+ // N R90 R180 R270 F FR90 FR180 FR270
/* N */ TO.[ TO.N, TO.R90, TO.R180, TO.R270, TO.F, TO.FR90, TO.FR180, TO.FR270 ],
/* R90 */ TO.[ TO.R90, TO.R180, TO.R270, TO.N, TO.FR270, TO.F, TO.FR90, TO.FR180,],
/* R180 */ TO.[ TO.R180, TO.R270, TO.N, TO.R90, TO.FR180, TO.FR270, TO.F, TO.FR90, ],
/* FR270 */ TO.[ TO.FR270, TO.F, TO.FR90, TO.FR180, TO.R90, TO.R180, TO.R270, TO.N ],
];
-reverse_binary :: proc (n_: u32, digits := TILE_DATA_WIDTH) -> u32 {
+reverse_binary :: (n_: u32, digits := TILE_DATA_WIDTH) -> u32 {
res := 0;
n := n_;
for _: 0 .. digits {
return res;
}
-build_edges :: proc (tile: [] bool, a := context.allocator) -> [] u32 {
+build_edges :: (tile: [] bool, a := context.allocator) -> [] u32 {
edges : [..] u32;
#context_scope {
context.allocator = a;
TO.[ TO.R90, TO.FR270, TO.F, TO.R180, ],
];
-has_matching_edges :: proc (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 {
match = true;
switch e_idx {
- case 0 do t1.edges_match.top = SideRelation.{ t2.id, side_relations[e2_idx][0] };
- case 1 do t1.edges_match.bottom = SideRelation.{ t2.id, side_relations[e2_idx][1] };
- case 2 do t1.edges_match.left = SideRelation.{ t2.id, side_relations[e2_idx][2] };
- case 3 do t1.edges_match.right = SideRelation.{ t2.id, side_relations[e2_idx][3] };
+ case 0 do t1.edges_match.top = .{ t2.id, side_relations[e2_idx][0] };
+ case 1 do t1.edges_match.bottom = .{ t2.id, side_relations[e2_idx][1] };
+ case 2 do t1.edges_match.left = .{ t2.id, side_relations[e2_idx][2] };
+ case 3 do t1.edges_match.right = .{ t2.id, side_relations[e2_idx][3] };
}
}
}
}
// This assumes the `t` was in NORMAL orientation to begin with.
-apply_orientation :: proc (t: ^Tile, ori: TO) {
- new_sides := t.edges_match;
+apply_orientation :: (t: ^Tile, ori: TO) {
+ new_sides := t.edges_match;
switch ori {
case TO.R90 {
t.orientation = ori;
}
-index_square_with_orientation :: proc (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];
#char" ",#char"#",#char" ",#char" ",#char"#",#char" ",#char" ",#char"#",#char" ",#char" ",#char"#",#char" ",#char" ",#char"#",#char" ",#char" ",#char"#",#char" ",#char" ",#char" ",
];
-scan_for_monsters :: proc (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 {
return found_monsters;
}
-tile_pos_state :: struct {
- match : SideRelation;
- pos_x : i32;
- pos_y : i32;
-}
-
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day20.txt";
file := reader.make(contents);
- tiles : [..] Tile;
- array.init(^tiles);
+ tiles := array.make(Tile);
defer array.free(^tiles);
- tile_map : map.Map(u32, ^Tile);
- map.init(^tile_map, null, 67);
+ tile_map := map.make(u32, #type ^Tile, null, 67);
defer map.free(^tile_map);
tile_data := calloc(200 * sizeof TileData);
tile_data := td[0 .. TILE_DATA_HEIGHT * TILE_DATA_WIDTH];
edges := build_edges(tile_data, tile_allocator);
- array.push(^tiles, Tile.{
+ array.push(^tiles, .{
id = id,
orientation = TO.N,
data = tile_data,
grid : [12 * 12] u32;
memory.set(^grid, 0, sizeof [12 * 12] u32);
- to_process : [..] tile_pos_state;
- array.init(^to_process);
+ tile_pos_state :: struct {
+ match : SideRelation;
+ pos_x : i32;
+ pos_y : i32;
+ }
+ to_process := array.make(tile_pos_state);
defer array.free(^to_process);
- array.push(^to_process, tile_pos_state.{ SideRelation.{ 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);
apply_orientation(tile_ptr, tid.match.ori);
- if tile_ptr.edges_match.top.tile != 0 do array.push(^to_process, tile_pos_state.{ 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_pos_state.{ 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_pos_state.{ 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_pos_state.{ 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;
Ingredient :: struct {
// This will just be a pointer into the file contents.
- name : str = str.{ null, 0 };
- appears_on : [..] u32 = (#type [..] u32).{ null, 0, 0 };
+ name : str = .{ null, 0 };
+ appears_on : [..] u32 = .{ null, 0, 0 };
- allergen : str = str.{ null, 0 };
+ allergen : str = .{ null, 0 };
}
Allergen :: struct {
- name : str = str.{ null, 0 };
- appears_on : [..] u32 = (#type [..] u32).{ null, 0, 0 };
+ name : str = .{ null, 0 };
+ appears_on : [..] u32 = .{ null, 0, 0 };
}
Food :: struct {
ingredient_map : map.Map(str, Ingredient);
allergen_map : map.Map(str, Allergen);
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day21.txt";
file := reader.make(contents);
- map.init(^ingredient_map, Ingredient.{}, 127);
- map.init(^allergen_map, Allergen.{});
+ map.init(^ingredient_map, .{}, 127);
+ map.init(^allergen_map, .{});
defer {
map.free(^ingredient_map);
map.free(^allergen_map);
}
- foods : [..] Food;
- array.init(^foods);
+ foods := array.make(Food);
defer array.free(^foods);
line_num := 0;
line_num += 1;
}
- definitely_safe : [..] str;
- array.init(^definitely_safe);
+ definitely_safe := array.make(str);
defer array.free(^definitely_safe);
for ^ingredient_entry: ingredient_map.entries {
- potential_allergens : [..] str;
- array.init(^potential_allergens);
+ potential_allergens := array.make(str);
defer array.free(^potential_allergens);
for food_num: ingredient_entry.value.appears_on {
printf("Total safe: %i\n", total_safe);
- matched_ingredients : [..] Ingredient;
- array.init(^matched_ingredients);
+ matched_ingredients := array.make(Ingredient);
defer array.free(^matched_ingredients);
while !map.empty(^ingredient_map) {
}
}
- array.sort(^matched_ingredients, proc (i1: Ingredient, i2: Ingredient) -> i32 {
+ array.sort(^matched_ingredients, (i1: Ingredient, i2: Ingredient) -> i32 {
return string.compare(i1.allergen, i2.allergen);
});
println("\n(Don't copy the last ','!)");
}
-array_count_contains :: proc (arr: ^[..] $T, x: T, equal: proc (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;
return count;
key_arena : alloc.arena.ArenaState;
key_alloc : Allocator;
-score_player :: proc (p: ^[..] u32) -> u32 {
+score_player :: (p: ^[..] u32) -> u32 {
count := 0;
mul := p.count;
for c: *p {
return count;
}
-combat :: proc (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 :: proc (alloc: Allocator, p1: ^[..] u32, p2: ^[..] u32) -> str {
+encode_hands :: (alloc: Allocator, p1: ^[..] u32, p2: ^[..] u32) -> str {
use package core.string.builder as b
builder := b.make(256, alloc);
return b.to_str(^builder);
}
-recursive_combat :: proc (player1: ^[..] u32, player2: ^[..] u32) -> u32 {
- hand_seen : map.Map(str, bool);
- map.init(^hand_seen, false, 31);
+recursive_combat :: (player1: ^[..] u32, player2: ^[..] u32) -> u32 {
+ hand_seen := map.make(str, bool, false, 31);
defer map.free(^hand_seen);
while player1.count > 0 && player2.count > 0 {
return 1;
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day22.txt";
file := reader.make(contents);
- player1: [..] u32;
- player2: [..] u32;
- array.init(^player1);
- array.init(^player2);
+ player1 := array.make(u32);
+ player2 := array.make(u32);
defer array.free(^player1);
defer array.free(^player2);
// Using this as a wrapping function, since the WASM modulus
// operator (%) doesn't appear to do the correct thing.
-w :: proc (idx: $T, mod := cups.count) -> T {
+w :: (idx: $T, mod := cups.count) -> T {
res := idx;
while res < 0 do res += ~~mod;
while res >= ~~mod do res -= ~~mod;
return res;
}
-get_idx :: proc (cups: [] i32, cup: i32) -> i32 {
+get_idx :: (cups: [] i32, cup: i32) -> i32 {
for i: 0 .. cups.count do if cups[i] == cup do return i;
return -1;
}
-range_by :: proc (lo: i32, hi: i32) -> range {
- if lo < hi do return range.{ lo, hi, 1 };
- if hi < lo do return range.{ hi, lo, 1 };
- return 0 .. 0;
-}
-
-simulate :: proc (cups: [] i32, moves := 100) {
- cw : [] i32;
- memory.alloc_slice(^cw, cups.count);
+simulate :: (cups: [] i32, moves := 100) {
+ cw := memory.make_slice(i32, cups.count);
defer cfree(cw.data);
for i: 0 .. cups.count do cw[cups[i]] = cups[w(i + 1)];
}
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
cups_data := i32.[ 9, 6, 2, 7, 1, 3, 8, 5, 4 ];
// Easier think about in zero-indexed
next : bool = false;
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day24.txt";
file := reader.make(contents);
- grid : map.Map(Vec2, Cell); // `true` is black
- map.init(^grid, Cell.{}, 1021);
+ grid := map.make(Vec2, Cell, .{}, 1021); // `true` is black
defer map.free(^grid);
while !reader.empty(^file) {
curr := map.get(^grid, loc);
- map.put(^grid, loc, Cell.{ alive = !curr.alive });
+ map.put(^grid, loc, .{ alive = !curr.alive });
}
// Part 1
printf("Black count: %i\n", black_count);
// Part 2
- cells_to_consider : [..] Vec2;
- array.init(^cells_to_consider);
+ cells_to_consider := array.make(Vec2);
defer array.free(^cells_to_consider);
for i: 0 .. 100 {
array.push(^cells_to_consider, cell.key);
for ^dir: Hex_Directions {
- array.push(^cells_to_consider, Vec2.{
+ array.push(^cells_to_consider, .{
x = cell.key.x + dir.x,
y = cell.key.y + dir.y,
});
}
for ^cell: cells_to_consider {
- map.update(^grid, *cell, proc (state: ^Cell) { state.alive = state.next; });
+ map.update(^grid, *cell, (state: ^Cell) { state.alive = state.next; });
}
array.clear(^cells_to_consider);
printf("GOL black count: %i\n", black_count);
}
-get_neighbor_count :: proc (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 {
use package core
use package core.string.reader as reader
-power_mod :: proc (base: u32, exp: u32, mod: u32) -> u32 {
+power_mod :: (base: u32, exp: u32, mod: u32) -> u32 {
t: u64 = 1;
e: u64 = ~~exp;
b: u64 = ~~base;
return ~~(t % m);
}
-dlp_bsgs :: proc (n: u32, a: u32, b: u32) -> u32 {
+dlp_bsgs :: (n: u32, a: u32, b: u32) -> u32 {
use package core.intrinsics.wasm { ceil_f64, sqrt_f64 }
-
m := cast(u32) ceil_f64(sqrt_f64(~~n));
- t : map.Map(u32, u32);
- map.init(^t, 0, m / 2);
+ t := map.make(u32, u32, default=0, hash_count=m/2);
defer map.free(^t);
tmp: u64 = 1;
return 0;
}
-transform_subject :: proc (subject: u32, loop_size: u32) -> u32 {
+transform_subject :: (subject: u32, loop_size: u32) -> u32 {
value: u64 = 1;
for i: 0 .. loop_size do value = (value * ~~subject) % 20201227;
return cast(u32) value;
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day25.txt";
file := reader.make(contents);
use package core
Point :: struct {
- x : i32;
- y : i32;
-};
+ x, y : i32;
+}
LineInterp :: struct {
- ox : i32;
- oy : i32;
- dx : i32;
- dy : i32;
+ ox, oy : i32;
+ dx, dy : i32;
}
-line_interp_at :: proc (use li: LineInterp, t: i32) -> Point {
- return Point.{ ox + dx * t, oy + dy * t };
+line_interp_at :: (use li: LineInterp, t: i32) -> Point {
+ return .{ ox + dx * t, oy + dy * t };
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day3.txt";
- forest: [..] u8;
- array.init(^forest, 1024);
+ forest := array.make(u8, 1024);
defer array.free(^forest);
width := 0;
}
lis : [5] LineInterp;
- lis[0] = LineInterp.{ 0, 0, 1, 1 };
- lis[1] = LineInterp.{ 0, 0, 3, 1 };
- lis[2] = LineInterp.{ 0, 0, 5, 1 };
- lis[3] = LineInterp.{ 0, 0, 7, 1 };
- lis[4] = LineInterp.{ 0, 0, 1, 2 };
+ lis[0] = .{ 0, 0, 1, 1 };
+ lis[1] = .{ 0, 0, 3, 1 };
+ lis[2] = .{ 0, 0, 5, 1 };
+ lis[3] = .{ 0, 0, 7, 1 };
+ lis[4] = .{ 0, 0, 1, 2 };
tree_prod: u64 = 1;
use package core
// Returns the number of fields
-process_passport :: proc (contents: ^str) -> u32 {
+process_passport :: (contents: ^str) -> u32 {
field_count := 0;
while true {
// This does not include part 2 because it is gross and
// not worth the effort to implement it.
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day4.txt";
valid_passports := 0;
use package core
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day5.txt";
- vals: [..] u32;
- array.init(^vals);
+ vals := array.make(u32);
defer array.free(^vals);
max_val := 0;
use package core
-part_1 :: proc (contents: ^str) -> u32 {
+part_1 :: (contents: ^str) -> u32 {
chars : [26] bool;
for ^ch: chars do *ch = false;
return sum;
}
-part_2 :: proc (contents: ^str) -> u32 {
+part_2 :: (contents: ^str) -> u32 {
chars : [26] u32;
for ^ch: chars do *ch = 0;
return sum;
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day6.txt";
unique_sum := 0;
use package core.string.reader as reader
BagGraph :: struct {
- nodes : [..] ^BagNode;
+ nodes : [..] ^BagNode;
node_map : map.Map(str, ^BagNode);
}
count : u32;
}
-bg_init :: proc (use graph: ^BagGraph) {
+bg_init :: (use graph: ^BagGraph) {
array.init(^nodes, 16);
map.init(^node_map, null);
}
-bg_free :: proc (use graph: ^BagGraph) {
+bg_free :: (use graph: ^BagGraph) {
array.free(^nodes);
map.free(^node_map);
}
-bg_get_node :: proc (use graph: ^BagGraph, name: str) -> ^BagNode {
+bg_get_node :: (use graph: ^BagGraph, name: str) -> ^BagNode {
node := map.get(^node_map, name);
if node == null {
return node;
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day7.txt";
file := reader.make(contents);
// });
// Part 2
- array.push(^container.contain, BagContainment.{
- bag = contained,
- count = count
- });
+ array.push(^container.contain, .{ bag = contained, count = count });
bag_word := reader.read_until(^file, 1, #char " ", #char "\n");
if bag_word[bag_word.count - 1] == #char "." do break;
// printf("Count: %i\n", processed_bags.count - 1);
// Part 2
- to_process : [..] ^BagNode;
- multiplier : [..] u32;
-
- array.init(^to_process);
- array.init(^multiplier);
+ to_process := array.make(#type ^BagNode);
+ multiplier := array.make(u32);
array.push(^to_process, bg_get_node(^graph, "shiny gold"));
array.push(^multiplier, 1);
}
// Returns if the program successfully exited.
-get_acc_value :: proc (instrs: [..] Instruction, ret_acc: ^i32) -> bool {
- already_been: map.Map(i32, bool);
- map.init(^already_been, false);
+get_acc_value :: (instrs: [..] Instruction, ret_acc: ^i32) -> bool {
+ already_been := map.make(i32, bool, false);
defer map.free(^already_been);
ip := 0;
return succ;
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day8.txt";
file := reader.make(contents);
- instrs: [..] Instruction;
- array.init(^instrs, 32);
+ instrs := array.make(Instruction, 32);
defer array.free(^instrs);
while !reader.empty(^file) {
elseif string.equal(word, "acc") do opcode = OpCode.Acc;
elseif string.equal(word, "jmp") do opcode = OpCode.Jmp;
- array.push(^instrs, Instruction.{
- opcode = opcode,
- operand = ~~val,
- });
+ array.push(^instrs, .{ opcode, ~~val });
}
acc: i32;
use package core
use package core.string.reader as reader
-StartEnd :: struct { start: i32; end: i32; }
-
-find_contiguous_subarray_with_sum :: proc (nums: [..] u64, sum: u64) -> StartEnd {
+find_contiguous_subarray_with_sum :: (nums: [..] u64, sum: u64) -> (i32, i32) {
start := 0;
end := 0;
con_sum: u64 = nums[0];
}
}
- return StartEnd.{ start, end };
+ return start, end;
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day9.txt";
file := reader.make(contents);
- nums: [..] u64;
- array.init(^nums, 32);
+ nums := array.make(u64, 32);
defer array.free(^nums);
while !reader.empty(^file) {
printf("Invalid number: %l\n", invalid);
- se := find_contiguous_subarray_with_sum(nums, invalid);
+ start, end := find_contiguous_subarray_with_sum(nums, invalid);
- max := array.fold_slice(nums.data[se.start .. se.end + 1], cast(u64) 0, math.max_poly);
- min := array.fold_slice(nums.data[se.start .. se.end + 1], cast(u64) max, math.min_poly);
+ max := array.fold_slice(nums.data[start .. end + 1], cast(u64) 0, math.max_poly);
+ min := array.fold_slice(nums.data[start .. end + 1], cast(u64) max, math.min_poly);
printf("Extrema sum: %l\n", min + max);
}