Skip to content

Commit

Permalink
Update with use of String.
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Jan 7, 2023
1 parent 6c2a2d4 commit 0957bf6
Show file tree
Hide file tree
Showing 27 changed files with 97 additions and 97 deletions.
8 changes: 4 additions & 4 deletions day10.c3
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ fn void part2()
{
@pool()
{
char[] line = f.tgetline();
char[][] commands = str::tsplit(line, " ");
String line = f.tgetline();
String[] commands = str::tsplit(line, " ");
switch (commands[0])
{
case "noop":
Expand Down Expand Up @@ -76,8 +76,8 @@ fn void part1()
{
@pool()
{
char[] line = f.tgetline();
char[][] commands = str::tsplit(line, " ");
String line = f.tgetline();
String[] commands = str::tsplit(line, " ");
switch (commands[0])
{
case "noop":
Expand Down
10 changes: 5 additions & 5 deletions day11.c3
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@ fn Monkey[] load_monkeys(Monkey[] monkeys)
@pool()
{
// Very lazy parsing, assuming a lot about the format.
char[] line = f.tgetline();
String line = f.tgetline();
if (line.len == 0) continue;
char[][] commands = str::tsplit(line, " ");
String[] commands = str::tsplit(line, " ");
int monkey_num = str::to_int(commands[1][..^2])!!;
if (monkey_num > max_monkey) max_monkey = monkey_num;
Monkey* active_monkey = &monkeys[monkey_num];
line = f.tgetline();
char[][] items = str::tsplit(str::tsplit(line, ": ")[1], ", ");
foreach (char[] item : items)
String[] items = str::tsplit(str::tsplit(line, ": ")[1], ", ");
foreach (String item : items)
{
active_monkey.items[active_monkey.item_count++] = str::to_int(item)!!;
}
line = f.tgetline();
char[][] op = str::tsplit(str::tsplit(line, "= old ")[1], " ");
String[] op = str::tsplit(str::tsplit(line, "= old ")[1], " ");
active_monkey.op_is_mult = op[0][0] == '*';
active_monkey.other_op = op[1] == "old" ? -1 : str::to_int(op[1])!!;
line = f.tgetline();
Expand Down
4 changes: 2 additions & 2 deletions day12.c3
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import std::io;
import std::math;
import std::array::list;

define MapList = List<char[]>;
define MapList = List<String>;

struct Map
{
Expand Down Expand Up @@ -74,7 +74,7 @@ fn Map* load_heightmap()
{
@pool()
{
char[] line = f.getline().str();
String line = f.getline().str();
foreach (int i, &c : line)
{
if (*c == 'S')
Expand Down
4 changes: 2 additions & 2 deletions day13.c3
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn void PacketElement.print(PacketElement* element)
}
}

fn int parse_list(void* list1, char[] list_inner)
fn int parse_list(void* list1, String list_inner)
{
Packet* packet = list1;
int index = 0;
Expand Down Expand Up @@ -100,7 +100,7 @@ fn int parse_list(void* list1, char[] list_inner)
unreachable();
}

fn Packet* packet_from_line(char[] line)
fn Packet* packet_from_line(String line)
{
assert(line[0] == '[' && line[^1] == ']');
Packet* packet = mem::alloc(Packet);
Expand Down
6 changes: 3 additions & 3 deletions day14.c3
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ enum EnvType
INFINITY_CUTOFF
}

fn int[<2>]! str_to_coord(char[] str)
fn int[<2>]! str_to_coord(String str)
{
char[][] parts = str::tsplit(str, ",");
String[] parts = str::tsplit(str, ",");
return { str::to_int(parts[0]), str::to_int(parts[1]) };
}

Expand All @@ -35,7 +35,7 @@ fn Environment* load_environment(EnvType type)
{
@pool()
{
char[] line = f.tgetline();
String line = f.tgetline();
int[<2>] last = { 0, 0 };
foreach (part : str::tsplit(line, " -> "))
{
Expand Down
8 changes: 4 additions & 4 deletions day15.c3
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import std::priorityqueue;

define CoordPairList = List<int[<2>][2]>;

fn int[<2>]! parse_coord(char[] s)
fn int[<2>]! parse_coord(String s)
{
char[][] parts = str::tsplit(s, ", y=");
String[] parts = str::tsplit(s, ", y=");
return { str::to_int(parts[0]), str::to_int(parts[1]) };
}
fn CoordPairList load_sensors()
Expand All @@ -22,9 +22,9 @@ fn CoordPairList load_sensors()
{
@pool()
{
char[] line = f.tgetline();
String line = f.tgetline();
line = line["Sensor at x=".len..];
char[][] parts = str::tsplit(line, ": closest beacon is at x=");
String[] parts = str::tsplit(line, ": closest beacon is at x=");
int[<2>] sensor_at = parse_coord(parts[0])!!;
int[<2>] beacon_at = parse_coord(parts[1])!!;
list.append( { sensor_at, beacon_at });
Expand Down
14 changes: 7 additions & 7 deletions day16.c3
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import std::math;
import std::map;
import std::array::list;

define ValveMap = HashMap<char[], int>;
define ValveMap = HashMap<String, int>;
define IntPairList = List<int[<2>]>;
ValveMap v;

struct Valve
{
int id;
char[] name;
String name;
int rate;
int[10] tunnels;
int tunnel_count;
Expand Down Expand Up @@ -42,10 +42,10 @@ fn Valve[] load_valves(Valve[100]* available_slots)
{
@pool()
{
char[] line = f.tgetline();
char[][] parts = str::tsplit(line, " ");
char[] name = str::copy(parts[1]);
char[] rate_part = parts[4];
String line = f.tgetline();
String[] parts = str::tsplit(line, " ");
String name = str::copy(parts[1]);
String rate_part = parts[4];
int val = v.@get_or_set(name, (int)v.count);
Valve *valve_entry = &(*available_slots)[val];
assert(valve_entry.id == 0);
Expand All @@ -55,7 +55,7 @@ fn Valve[] load_valves(Valve[100]* available_slots)
*valve_entry = { .rate = rate, .id = val, .name = name };
for (int i = 9; i < parts.len; i++)
{
char[] valve = parts[i];
String valve = parts[i];
if (i < parts.len - 1)
{
valve = valve[..^2];
Expand Down
10 changes: 5 additions & 5 deletions day17.c3
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ macro bool StateCache.equals(StateCache* cache, StateCache other)
return true;
}

fn String load_jets()
fn VarString load_jets()
{
File f;
f.open("jets.txt", "rb")!!;
Expand Down Expand Up @@ -185,7 +185,7 @@ macro long adjust_lowest_bounds()
return bound;
}

macro @drop_rock(char[] winds, int &shape_index, isz &wind_index)
macro @drop_rock(String winds, int &shape_index, isz &wind_index)
{
int start = find_height() + 3;
Shape* current_shape = &shapes[shape_index];
Expand All @@ -195,7 +195,7 @@ macro @drop_rock(char[] winds, int &shape_index, isz &wind_index)
{
char jet = winds[wind_index];
wind_index = (wind_index + 1) % winds.len;
int[<2>] new_loc = location + int[<2>] { jet == '<' ? -1 : 1, 0 };
int[<2>] new_loc = location + { jet == '<' ? -1 : 1, 0 };
if (check_collision(current_shape, new_loc))
{
new_loc = location;
Expand All @@ -211,7 +211,7 @@ macro @drop_rock(char[] winds, int &shape_index, isz &wind_index)
}
}

fn void solve(char[] winds, long rocks)
fn void solve(String winds, long rocks)
{
game = {};
int shape_index = 0;
Expand Down Expand Up @@ -272,7 +272,7 @@ fn void solve(char[] winds, long rocks)

fn void main()
{
String s = load_jets();
VarString s = load_jets();
defer s.destroy();
solve(s.str(), 2022);
solve(s.str(), 1000000000000i64);
Expand Down
2 changes: 1 addition & 1 deletion day18.c3
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn void load_lava()
{
@pool()
{
char[][] parts = str::tsplit(f.tgetline(), ",");
String[] parts = str::tsplit(f.tgetline(), ",");
locations[str::to_int(parts[0])!!][str::to_int(parts[1])!!][str::to_int(parts[2])!!] = 7;
};
}
Expand Down
6 changes: 3 additions & 3 deletions day19.c3
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ fn void load_blueprints()
@pool()
{
Blueprint print;
char[] line = f.tgetline();
char[][] split = str::tsplit(line, "Each ore robot costs ");
String line = f.tgetline();
String[] split = str::tsplit(line, "Each ore robot costs ");
split = str::tsplit(split[1], " ore. Each clay robot costs ");
print.ore_cost = { [ORE] = str::to_int(split[0])!! };
split = str::tsplit(split[1], " ore. Each obsidian robot costs ");
print.clay_cost = { [ORE] = str::to_int(split[0])!! };
split = str::tsplit(split[1], " clay. Each geode robot costs ");
char[][] split2 = str::tsplit(split[0], " ore and ");
String[] split2 = str::tsplit(split[0], " ore and ");
print.obsidian_cost = {
[ORE] = str::to_int(split2[0])!!,
[CLAY] = str::to_int(split2[1])!!
Expand Down
2 changes: 1 addition & 1 deletion day1_1.c3
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn void main()
{
@pool()
{
char[] line = f.tgetline();
String line = f.tgetline();
if (!line.len)
{
current_entry = 0;
Expand Down
2 changes: 1 addition & 1 deletion day1_2.c3
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn void main()
{
@pool()
{
char[] line = f.tgetline();
String line = f.tgetline();
if (!line.len)
{
update_max(current_entry);
Expand Down
8 changes: 4 additions & 4 deletions day2.c3
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fault InvalidData
FAILED
}

fn Choice[2]! choicesFromString(char[] line)
fn Choice[2]! choicesFromString(String line)
{
if (line.len != 3) return InvalidData.FAILED!;
char c1 = line[0];
Expand All @@ -36,7 +36,7 @@ fn Choice[2]! choicesFromString(char[] line)
return { (Choice)(c1 - 'A'), (Choice)(c2 - 'X') };
}

fn Select! selectFromString(char[] line)
fn Select! selectFromString(String line)
{
if (line.len != 3) return InvalidData.FAILED!;
char c1 = line[0];
Expand All @@ -57,7 +57,7 @@ fn void part1()
{
@pool()
{
char[] line = f.tgetline();
String line = f.tgetline();
Choice[2] c = choicesFromString(line)!!;
points += c[1].ordinal + 1;
// Draw
Expand Down Expand Up @@ -87,7 +87,7 @@ fn void part2()
{
@pool()
{
char[] line = f.tgetline();
String line = f.tgetline();
Select s = selectFromString(line)!!;
switch (s.r)
{
Expand Down
2 changes: 1 addition & 1 deletion day20.c3
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn void load_crypto(LongList* list, LongList* pos, long key)
{
@pool()
{
char[] line = f.tgetline();
String line = f.tgetline();
list.append(key * str::to_long(line)!!);
pos.append((int)pos.len());
};
Expand Down
12 changes: 6 additions & 6 deletions day21.c3
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import std::map;
import std::array::list;

define MonkeyList = List<Monkey>;
define NameMap = HashMap<char[], MonkeyId>;
define NameMap = HashMap<String, MonkeyId>;

enum Action
{
Expand All @@ -21,14 +21,14 @@ define MonkeyId = distinct int;
struct Monkey
{
MonkeyId id;
char[] name;
String name;
Action action;
bool is_human;
bool had_human;
union
{
Monkey*[2] other_monkey;
char[][2] other_monkey_unresolved;
String[2] other_monkey_unresolved;
long value;
}
}
Expand All @@ -45,9 +45,9 @@ fn void load_monkeys(MonkeyList* list, MonkeyId* root_index, MonkeyId *humn_inde
{
@pool()
{
char[] line = f.tgetline();
char[][] parts = str::tsplit(line, ": ");
char[] name = str::copy(parts[0]);
String line = f.tgetline();
String[] parts = str::tsplit(line, ": ");
String name = str::copy(parts[0]);
if (name == "root")
{
*root_index = index;
Expand Down
4 changes: 2 additions & 2 deletions day22.c3
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ enum MapType : char
}

int[200][200] map;
char[] commands;
String commands;
int[<2>] start = { -1, -1 };
int side;
int width;
Expand All @@ -33,7 +33,7 @@ fn void load_map()
{
@pool()
{
char[] line = f.tgetline();
String line = f.tgetline();
if (line.len > width) width = line.len;
if (!line.len) break;
foreach (int x, c : line)
Expand Down
Loading

0 comments on commit 0957bf6

Please sign in to comment.