From: Brendan Hansen Date: Mon, 1 Mar 2021 19:02:55 +0000 (-0600) Subject: cleaned up AOC test cases for anyone using them to learn X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=abfdc5449c877b06cb1b01332c9d169c1e2db72b;p=onyx.git cleaned up AOC test cases for anyone using them to learn --- diff --git a/bin/onyx b/bin/onyx index c2858465..d29c4069 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/tests/aoc-2020/day1.onyx b/tests/aoc-2020/day1.onyx index 161401cd..e0cf261f 100644 --- a/tests/aoc-2020/day1.onyx +++ b/tests/aoc-2020/day1.onyx @@ -2,13 +2,12 @@ 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 { diff --git a/tests/aoc-2020/day10.onyx b/tests/aoc-2020/day10.onyx index 63165330..13420ee7 100644 --- a/tests/aoc-2020/day10.onyx +++ b/tests/aoc-2020/day10.onyx @@ -3,9 +3,8 @@ 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; @@ -24,13 +23,12 @@ count_ending_paths :: proc (nums: [..] u32) -> u64 { 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) { diff --git a/tests/aoc-2020/day11.onyx b/tests/aoc-2020/day11.onyx index b8a8b327..5375be4b 100644 --- a/tests/aoc-2020/day11.onyx +++ b/tests/aoc-2020/day11.onyx @@ -12,12 +12,12 @@ GameOfSeats :: struct { 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 { @@ -38,7 +38,7 @@ gos_neighbors :: proc (use gos: ^GameOfSeats, x: i32, y: i32, state := SeatState 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; diff --git a/tests/aoc-2020/day12.onyx b/tests/aoc-2020/day12.onyx index bd632b98..63e42642 100644 --- a/tests/aoc-2020/day12.onyx +++ b/tests/aoc-2020/day12.onyx @@ -11,7 +11,7 @@ Ship :: struct { fy : i32 = 0; } -rotate_left :: proc (ship: ^Ship) { +rotate_left :: (ship: ^Ship) { ofx := ship.fx; ofy := ship.fy; @@ -19,7 +19,7 @@ rotate_left :: proc (ship: ^Ship) { ship.fy = -ofx; } -rotate_right :: proc (ship: ^Ship) { +rotate_right :: (ship: ^Ship) { ofx := ship.fx; ofy := ship.fy; @@ -27,12 +27,12 @@ rotate_right :: proc (ship: ^Ship) { 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); diff --git a/tests/aoc-2020/day13.onyx b/tests/aoc-2020/day13.onyx index 5ebc6287..c6154bb0 100644 --- a/tests/aoc-2020/day13.onyx +++ b/tests/aoc-2020/day13.onyx @@ -3,7 +3,7 @@ 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_; @@ -23,7 +23,7 @@ inv_mod :: proc (a_: i64, m_: i64) -> i64 { 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; @@ -36,7 +36,7 @@ chinese_remainder_theorem :: proc (mods: [..] i64, rems: [..] i64) -> i64 { return res % N; } -main :: proc (args: [] cstr) { +main :: (args: [] cstr) { contents := #file_contents "./tests/aoc-2020/input/day13.txt"; file := reader.make(contents); @@ -44,12 +44,10 @@ main :: proc (args: [] cstr) { 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; diff --git a/tests/aoc-2020/day14.onyx b/tests/aoc-2020/day14.onyx index 55e77d3c..3903638f 100644 --- a/tests/aoc-2020/day14.onyx +++ b/tests/aoc-2020/day14.onyx @@ -7,7 +7,7 @@ MASK_SIZE :: 36 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 { @@ -29,7 +29,7 @@ BitmaskIter :: struct { done : bool; } -bitmask_iter_make :: proc (mask: Bitmask, val: u64) -> BitmaskIter { +bitmask_iter_make :: (mask: Bitmask, val: u64) -> BitmaskIter { bmi : BitmaskIter; bmi.done = false; @@ -52,13 +52,13 @@ bitmask_iter_make :: proc (mask: Bitmask, val: u64) -> BitmaskIter { 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; @@ -74,13 +74,12 @@ bitmask_iter_next :: proc (use bmi: ^BitmaskIter) -> u64 { 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; diff --git a/tests/aoc-2020/day15.onyx b/tests/aoc-2020/day15.onyx index cfb09564..379be8ac 100644 --- a/tests/aoc-2020/day15.onyx +++ b/tests/aoc-2020/day15.onyx @@ -10,19 +10,18 @@ spoken_times :: struct { 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; } diff --git a/tests/aoc-2020/day16.onyx b/tests/aoc-2020/day16.onyx index 3e385ded..3ce7c0f2 100644 --- a/tests/aoc-2020/day16.onyx +++ b/tests/aoc-2020/day16.onyx @@ -14,13 +14,13 @@ Field :: struct { 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 { @@ -44,13 +44,12 @@ read_ticket_and_validate :: proc (file: ^reader.StringReader, fields: [..] Field 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 @@ -77,15 +76,13 @@ main :: proc (args: [] cstr) { 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) { @@ -97,8 +94,7 @@ main :: proc (args: [] cstr) { 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); diff --git a/tests/aoc-2020/day17.onyx b/tests/aoc-2020/day17.onyx index b6580591..6d8e833a 100644 --- a/tests/aoc-2020/day17.onyx +++ b/tests/aoc-2020/day17.onyx @@ -4,15 +4,12 @@ use package core 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 { @@ -26,7 +23,7 @@ proc (a: CubePos, b: CubePos) -> bool #add_overload map.cmp_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 { @@ -44,8 +41,7 @@ main :: proc (args: [] cstr) { 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; @@ -54,7 +50,7 @@ main :: proc (args: [] cstr) { 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; } @@ -62,15 +58,14 @@ main :: proc (args: [] cstr) { 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, diff --git a/tests/aoc-2020/day18.onyx b/tests/aoc-2020/day18.onyx index e48da308..0b38252f 100644 --- a/tests/aoc-2020/day18.onyx +++ b/tests/aoc-2020/day18.onyx @@ -3,7 +3,7 @@ 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 { @@ -27,7 +27,7 @@ parse_factor :: proc (file: ^reader.StringReader) -> u64 { 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); @@ -45,7 +45,7 @@ parse_expression_add :: proc (file: ^reader.StringReader) -> u64 { 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); @@ -63,7 +63,7 @@ parse_expression_mul :: proc (file: ^reader.StringReader) -> u64 { return left; } -main :: proc (args: [] cstr) { +main :: (args: [] cstr) { contents := #file_contents "./tests/aoc-2020/input/day18.txt"; file := reader.make(contents); diff --git a/tests/aoc-2020/day19.onyx b/tests/aoc-2020/day19.onyx index 6183a30b..4e105e9a 100644 --- a/tests/aoc-2020/day19.onyx +++ b/tests/aoc-2020/day19.onyx @@ -33,7 +33,7 @@ Grammar :: struct { 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); @@ -41,13 +41,13 @@ grammar_init :: proc (use g: ^Grammar) { 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 { @@ -63,11 +63,11 @@ grammar_prepare :: proc (use g: ^Grammar) { } } - 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); }); @@ -76,7 +76,7 @@ grammar_prepare :: proc (use g: ^Grammar) { 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; @@ -110,7 +110,7 @@ cyk_algorithm :: proc (use grammar: ^Grammar, input: str) -> bool { 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); diff --git a/tests/aoc-2020/day2.onyx b/tests/aoc-2020/day2.onyx index 7f4044e8..8e2be91b 100644 --- a/tests/aoc-2020/day2.onyx +++ b/tests/aoc-2020/day2.onyx @@ -4,14 +4,14 @@ package main 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); diff --git a/tests/aoc-2020/day20.onyx b/tests/aoc-2020/day20.onyx index c91593e5..e1e8c110 100644 --- a/tests/aoc-2020/day20.onyx +++ b/tests/aoc-2020/day20.onyx @@ -20,14 +20,14 @@ Tile :: struct { 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 { @@ -42,7 +42,7 @@ TO :: enum (u8) { // 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, ], @@ -53,7 +53,7 @@ tile_orientation_table := (#type [8] TO).[ /* 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 { @@ -65,7 +65,7 @@ reverse_binary :: proc (n_: u32, digits := TILE_DATA_WIDTH) -> u32 { 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; @@ -110,7 +110,7 @@ side_relations := (#type [4] TO).[ 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 { @@ -118,10 +118,10 @@ has_matching_edges :: proc (t1: ^Tile, t2: ^Tile) -> bool { 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] }; } } } @@ -130,8 +130,8 @@ has_matching_edges :: proc (t1: ^Tile, t2: ^Tile) -> bool { } // 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 { @@ -183,7 +183,7 @@ apply_orientation :: proc (t: ^Tile, ori: TO) { 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]; @@ -206,7 +206,7 @@ sea_monster := u8.[ #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 { @@ -241,23 +241,15 @@ scan_for_monsters :: proc (forest: ^u8, ori: TO, width: u32, height: u32) -> boo 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); @@ -289,7 +281,7 @@ main :: proc (args: [] cstr) { 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, @@ -329,11 +321,15 @@ main :: proc (args: [] cstr) { 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); @@ -347,10 +343,10 @@ main :: proc (args: [] cstr) { 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; diff --git a/tests/aoc-2020/day21.onyx b/tests/aoc-2020/day21.onyx index 82f01c17..dc27e090 100644 --- a/tests/aoc-2020/day21.onyx +++ b/tests/aoc-2020/day21.onyx @@ -12,15 +12,15 @@ use package core.string.reader as reader 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 { @@ -31,20 +31,19 @@ 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; @@ -95,13 +94,11 @@ main :: proc (args: [] cstr) { 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 { @@ -134,8 +131,7 @@ main :: proc (args: [] cstr) { 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) { @@ -166,7 +162,7 @@ main :: proc (args: [] cstr) { } } - 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); }); @@ -175,7 +171,7 @@ main :: proc (args: [] cstr) { 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; diff --git a/tests/aoc-2020/day22.onyx b/tests/aoc-2020/day22.onyx index 29912ed5..ca2038ef 100644 --- a/tests/aoc-2020/day22.onyx +++ b/tests/aoc-2020/day22.onyx @@ -6,7 +6,7 @@ use package core.string.reader as reader 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 { @@ -16,7 +16,7 @@ score_player :: proc (p: ^[..] u32) -> u32 { 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]; @@ -42,7 +42,7 @@ combat :: proc (player1: ^[..] u32, player2: ^[..] u32) -> u32 { // 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); @@ -59,9 +59,8 @@ encode_hands :: proc (alloc: Allocator, p1: ^[..] u32, p2: ^[..] u32) -> str { 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 { @@ -101,15 +100,13 @@ recursive_combat :: proc (player1: ^[..] u32, player2: ^[..] u32) -> u32 { 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); diff --git a/tests/aoc-2020/day23.onyx b/tests/aoc-2020/day23.onyx index 55c634de..c40b3bb1 100644 --- a/tests/aoc-2020/day23.onyx +++ b/tests/aoc-2020/day23.onyx @@ -6,27 +6,20 @@ cups : [..] i32; // 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)]; @@ -59,7 +52,7 @@ simulate :: proc (cups: [] i32, moves := 100) { } } -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 diff --git a/tests/aoc-2020/day24.onyx b/tests/aoc-2020/day24.onyx index 2fca89f6..57c4b38a 100644 --- a/tests/aoc-2020/day24.onyx +++ b/tests/aoc-2020/day24.onyx @@ -27,13 +27,12 @@ Cell :: struct { 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) { @@ -68,7 +67,7 @@ main :: proc (args: [] cstr) { curr := map.get(^grid, loc); - map.put(^grid, loc, Cell.{ alive = !curr.alive }); + map.put(^grid, loc, .{ alive = !curr.alive }); } // Part 1 @@ -79,8 +78,7 @@ main :: proc (args: [] cstr) { 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 { @@ -89,7 +87,7 @@ main :: proc (args: [] cstr) { 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, }); @@ -111,7 +109,7 @@ main :: proc (args: [] cstr) { } 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); @@ -124,7 +122,7 @@ main :: proc (args: [] cstr) { 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 { diff --git a/tests/aoc-2020/day25.onyx b/tests/aoc-2020/day25.onyx index eda4d688..0ca1507b 100644 --- a/tests/aoc-2020/day25.onyx +++ b/tests/aoc-2020/day25.onyx @@ -3,7 +3,7 @@ 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; @@ -19,13 +19,11 @@ power_mod :: proc (base: u32, exp: u32, mod: u32) -> u32 { 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; @@ -49,13 +47,13 @@ dlp_bsgs :: proc (n: u32, a: u32, b: u32) -> u32 { 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); diff --git a/tests/aoc-2020/day3.onyx b/tests/aoc-2020/day3.onyx index 3e378ea2..e845be75 100644 --- a/tests/aoc-2020/day3.onyx +++ b/tests/aoc-2020/day3.onyx @@ -3,26 +3,22 @@ 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; @@ -39,11 +35,11 @@ main :: proc (args: [] cstr) { } 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; diff --git a/tests/aoc-2020/day4.onyx b/tests/aoc-2020/day4.onyx index 7f68201a..30e541e3 100644 --- a/tests/aoc-2020/day4.onyx +++ b/tests/aoc-2020/day4.onyx @@ -3,7 +3,7 @@ use package core // Returns the number of fields -process_passport :: proc (contents: ^str) -> u32 { +process_passport :: (contents: ^str) -> u32 { field_count := 0; while true { @@ -29,7 +29,7 @@ process_passport :: proc (contents: ^str) -> u32 { // 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; diff --git a/tests/aoc-2020/day5.onyx b/tests/aoc-2020/day5.onyx index 0f88f886..2348531f 100644 --- a/tests/aoc-2020/day5.onyx +++ b/tests/aoc-2020/day5.onyx @@ -2,11 +2,10 @@ 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; diff --git a/tests/aoc-2020/day6.onyx b/tests/aoc-2020/day6.onyx index b2d6d976..d816149d 100644 --- a/tests/aoc-2020/day6.onyx +++ b/tests/aoc-2020/day6.onyx @@ -2,7 +2,7 @@ use package core -part_1 :: proc (contents: ^str) -> u32 { +part_1 :: (contents: ^str) -> u32 { chars : [26] bool; for ^ch: chars do *ch = false; @@ -20,7 +20,7 @@ part_1 :: proc (contents: ^str) -> u32 { return sum; } -part_2 :: proc (contents: ^str) -> u32 { +part_2 :: (contents: ^str) -> u32 { chars : [26] u32; for ^ch: chars do *ch = 0; @@ -40,7 +40,7 @@ part_2 :: proc (contents: ^str) -> u32 { return sum; } -main :: proc (args: [] cstr) { +main :: (args: [] cstr) { contents := #file_contents "./tests/aoc-2020/input/day6.txt"; unique_sum := 0; diff --git a/tests/aoc-2020/day7.onyx b/tests/aoc-2020/day7.onyx index 3839db3f..2fe27d12 100644 --- a/tests/aoc-2020/day7.onyx +++ b/tests/aoc-2020/day7.onyx @@ -4,7 +4,7 @@ use package core use package core.string.reader as reader BagGraph :: struct { - nodes : [..] ^BagNode; + nodes : [..] ^BagNode; node_map : map.Map(str, ^BagNode); } @@ -22,17 +22,17 @@ BagContainment :: struct { 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 { @@ -50,7 +50,7 @@ bg_get_node :: proc (use graph: ^BagGraph, name: str) -> ^BagNode { return node; } -main :: proc (args: [] cstr) { +main :: (args: [] cstr) { contents := #file_contents "./tests/aoc-2020/input/day7.txt"; file := reader.make(contents); @@ -83,10 +83,7 @@ main :: proc (args: [] cstr) { // }); // 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; @@ -118,11 +115,8 @@ main :: proc (args: [] cstr) { // 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); diff --git a/tests/aoc-2020/day8.onyx b/tests/aoc-2020/day8.onyx index 8acf6f04..139345a9 100644 --- a/tests/aoc-2020/day8.onyx +++ b/tests/aoc-2020/day8.onyx @@ -13,9 +13,8 @@ Instruction :: struct { } // 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; @@ -44,13 +43,12 @@ get_acc_value :: proc (instrs: [..] Instruction, ret_acc: ^i32) -> bool { 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) { @@ -69,10 +67,7 @@ main :: proc (args: [] cstr) { 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; diff --git a/tests/aoc-2020/day9.onyx b/tests/aoc-2020/day9.onyx index c6278208..967dbb33 100644 --- a/tests/aoc-2020/day9.onyx +++ b/tests/aoc-2020/day9.onyx @@ -3,9 +3,7 @@ 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]; @@ -24,16 +22,15 @@ find_contiguous_subarray_with_sum :: proc (nums: [..] u64, sum: u64) -> StartEnd } } - 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) { @@ -65,10 +62,10 @@ main :: proc (args: [] cstr) { 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); }