Skip to content

Commit

Permalink
Update with changes to the file API and other minor fixups.
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Mar 11, 2023
1 parent d7752bd commit e8a7973
Show file tree
Hide file tree
Showing 27 changed files with 94 additions and 126 deletions.
12 changes: 5 additions & 7 deletions day10.c3
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn void draw_crt(bool[40][6]* crt)
{
io::print(pixel ? "#" : " ");
}
io::println();
io::printn();
}
}

Expand All @@ -34,8 +34,7 @@ fn void update_crt(bool[40][6]* crt, int cycle, int x)

fn void part2()
{
File f;
f.open("code.txt", "rb")!!;
File f = file::open("code.txt", "rb")!!;
defer (void)f.close();
bool[40][6] crt;
int cycle = 0;
Expand All @@ -45,7 +44,7 @@ fn void part2()
@pool()
{
String line = f.tgetline();
String[] commands = str::tsplit(line, " ");
String[] commands = line.tsplit(" ");
switch (commands[0])
{
case "noop":
Expand All @@ -66,8 +65,7 @@ fn void part2()
}
fn void part1()
{
File f;
f.open("code.txt", "rb")!!;
File f = file::open("code.txt", "rb")!!;
defer (void)f.close();
int cycle = 0;
int x = 1;
Expand All @@ -77,7 +75,7 @@ fn void part1()
@pool()
{
String line = f.tgetline();
String[] commands = str::tsplit(line, " ");
String[] commands = line.tsplit(" ");
switch (commands[0])
{
case "noop":
Expand Down
15 changes: 7 additions & 8 deletions day11.c3
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ fn void print_monkeys(Monkey[] monkeys)

fn Monkey[] load_monkeys(Monkey[] monkeys)
{
File f;
f.open("monkey.txt", "rb")!!;
File f = file::open("monkey.txt", "rb")!!;
defer (void)f.close();
int max_monkey = 0;
while (!f.eof())
Expand All @@ -36,26 +35,26 @@ fn Monkey[] load_monkeys(Monkey[] monkeys)
// Very lazy parsing, assuming a lot about the format.
String line = f.tgetline();
if (line.len == 0) continue;
String[] commands = str::tsplit(line, " ");
String[] commands = line.tsplit(" ");
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();
String[] items = str::tsplit(str::tsplit(line, ": ")[1], ", ");
String[] items = line.tsplit(": ")[1].tsplit(", ");
foreach (String item : items)
{
active_monkey.items[active_monkey.item_count++] = str::to_int(item)!!;
}
line = f.tgetline();
String[] op = str::tsplit(str::tsplit(line, "= old ")[1], " ");
String[] op = line.tsplit("= old ")[1].tsplit(" ");
active_monkey.op_is_mult = op[0][0] == '*';
active_monkey.other_op = op[1] == "old" ? -1 : str::to_int(op[1])!!;
line = f.tgetline();
active_monkey.test = str::to_int(str::tsplit(line, "by ")[1])!!;
active_monkey.test = str::to_int(line.tsplit("by ")[1])!!;
line = f.tgetline();
active_monkey.on_true = str::to_int(str::tsplit(line, "key ")[1])!!;
active_monkey.on_true = str::to_int(line.tsplit("key ")[1])!!;
line = f.tgetline();
active_monkey.on_false = str::to_int(str::tsplit(line, "key ")[1])!!;
active_monkey.on_false = str::to_int(line.tsplit("key ")[1])!!;
};
}
return monkeys[..max_monkey];
Expand Down
9 changes: 4 additions & 5 deletions day12.c3
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn WalkedMap* Map.create_temp_empty_walked_map(Map* map)
{
usz height = map.map.len();
usz width = map.map.get(0).len;
WalkedMap *w = talloc(WalkedMap);
WalkedMap *w = tmalloc(WalkedMap);
w.walked = tcalloc(int.sizeof * height * width);
w.size = { (int)width, (int)height };
return w;
Expand All @@ -50,7 +50,7 @@ fn bool WalkedMap.walk(WalkedMap* this, int[<2>] loc, int steps)

fn void WalkedMap.show(WalkedMap* this)
{
io::println("-----------------");
io::printn("-----------------");
for (int y = 0; y < this.size[1]; y++)
{
io::printf("|");
Expand All @@ -59,14 +59,13 @@ fn void WalkedMap.show(WalkedMap* this)
int val = this.walked[x + y * this.size[0]];
io::printf("%c", val ? val + '0' - 1 : ' ');
}
io::println("|");
io::printn("|");
}
}

fn Map* load_heightmap()
{
File f;
f.open("heightmap.txt", "rb")!!;
File f = file::open("heightmap.txt", "rb")!!;
defer (void)f.close();
Map* map = malloc(Map);
map.map.init();
Expand Down
3 changes: 1 addition & 2 deletions day13.c3
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ fn Packet* packet_from_line(String line)
}
fn PacketPairs* load_packets()
{
File f;
f.open("packets.txt", "rb")!!;
File f = file::open("packets.txt", "rb")!!;
defer (void)f.close();
PacketPairs* packet = malloc(PacketPairs);
packet.init();
Expand Down
7 changes: 3 additions & 4 deletions day14.c3
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ enum EnvType

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

fn Environment* load_environment(EnvType type)
{
File f;
f.open("cave.txt", "rb")!!;
File f = file::open("cave.txt", "rb")!!;
defer (void)f.close();
Environment* env = calloc(Environment.sizeof);
int max_y = 0;
Expand All @@ -37,7 +36,7 @@ fn Environment* load_environment(EnvType type)
{
String line = f.tgetline();
int[<2>] last = { 0, 0 };
foreach (part : str::tsplit(line, " -> "))
foreach (part : line.tsplit(" -> "))
{
int[<2>] next = str_to_coord(part)!!;
if (next[1] > max_y) max_y = next[1];
Expand Down
9 changes: 4 additions & 5 deletions day15.c3
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ define CoordPairList = List<int[<2>][2]>;

fn int[<2>]! parse_coord(String s)
{
String[] parts = str::tsplit(s, ", y=");
String[] parts = s.tsplit(", y=");
return { str::to_int(parts[0]), str::to_int(parts[1]) };
}
fn CoordPairList load_sensors()
{
File f;
f.open("sensor.txt", "rb")!!;
File f = file::open("sensor.txt", "rb")!!;
defer (void)f.close();
CoordPairList list;
list.init();
Expand All @@ -24,7 +23,7 @@ fn CoordPairList load_sensors()
{
String line = f.tgetline();
line = line["Sensor at x=".len..];
String[] parts = str::tsplit(line, ": closest beacon is at x=");
String[] parts = line.tsplit(": 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 Expand Up @@ -118,7 +117,7 @@ fn void part2(CoordPairList list)
}
}
}
io::println("No beacon found.");
io::printn("No beacon found.");
}

fn void main()
Expand Down
9 changes: 4 additions & 5 deletions day16.c3
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,16 @@ fn void populate_tunnels(Valve[] valves, int i, Valve* v, int tunnel, int cost)

fn Valve[] load_valves(Valve[100]* available_slots)
{
File f;
f.open("valves.txt", "rb")!!;
File f = file::open("valves.txt", "rb")!!;
defer (void)f.close();
int count = 0;
while (!f.eof())
{
@pool()
{
String line = f.tgetline();
String[] parts = str::tsplit(line, " ");
String name = str::copy(parts[1]);
String[] parts = line.tsplit(" ");
String name = parts[1].copy();
String rate_part = parts[4];
int val = v.@get_or_set(name, (int)v.count);
Valve *valve_entry = &(*available_slots)[val];
Expand All @@ -60,7 +59,7 @@ fn Valve[] load_valves(Valve[100]* available_slots)
{
valve = valve[..^2];
}
val = v.@get_or_set(str::copy(valve), (int)v.count);
val = v.@get_or_set(valve.copy(), (int)v.count);
valve_entry.tunnels[valve_entry.tunnel_count++] = val;
}
};
Expand Down
11 changes: 5 additions & 6 deletions day17.c3
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ macro bool StateCache.equals(StateCache* cache, StateCache other)

fn DString load_jets()
{
File f;
f.open("jets.txt", "rb")!!;
File f = file::open("jets.txt", "rb")!!;
defer (void)f.close();
return f.getline();
}
Expand Down Expand Up @@ -156,17 +155,17 @@ fn void land_shape(Shape* shape, int[<2>] location)

fn void draw_field()
{
io::println("+-----+");
io::printn("+-----+");
for (int i = find_height(); i >= 0; i--)
{
GameRow row = game[i];
for (int j = 0; j < 7; j++)
{
io::putchar(row.bit(j) ? '#' : '.');
}
io::println();
io::printn();
}
io::println("+-----+");
io::printn("+-----+");
}

macro long adjust_lowest_bounds()
Expand Down Expand Up @@ -273,7 +272,7 @@ fn void solve(String winds, long rocks)
fn void main()
{
DString s = load_jets();
defer s.destroy();
defer s.free();
solve(s.str(), 2022);
solve(s.str(), 1000000000000i64);
}
5 changes: 2 additions & 3 deletions day18.c3
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ int[MAX][MAX][MAX] locations;

fn void load_lava()
{
File f;
f.open("lava.txt", "rb")!!;
File f = file::open("lava.txt", "rb")!!;
defer (void)f.close();
while (!f.eof())
{
@pool()
{
String[] parts = str::tsplit(f.tgetline(), ",");
String[] parts = f.tgetline().tsplit(",");
locations[str::to_int(parts[0])!!][str::to_int(parts[1])!!][str::to_int(parts[2])!!] = 7;
};
}
Expand Down
15 changes: 7 additions & 8 deletions day19.c3
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ struct GameState

fn void load_blueprints()
{
File f;
f.open("blueprint.txt", "rb")!!;
File f = file::open("blueprint.txt", "rb")!!;
blueprints.init();
defer (void)f.close();
while (!f.eof())
Expand All @@ -41,18 +40,18 @@ fn void load_blueprints()
{
Blueprint print;
String line = f.tgetline();
String[] split = str::tsplit(line, "Each ore robot costs ");
split = str::tsplit(split[1], " ore. Each clay robot costs ");
String[] split = line.tsplit("Each ore robot costs ");
split = split[1].tsplit(" ore. Each clay robot costs ");
print.ore_cost = { [ORE] = str::to_int(split[0])!! };
split = str::tsplit(split[1], " ore. Each obsidian robot costs ");
split = split[1].tsplit(" ore. Each obsidian robot costs ");
print.clay_cost = { [ORE] = str::to_int(split[0])!! };
split = str::tsplit(split[1], " clay. Each geode robot costs ");
String[] split2 = str::tsplit(split[0], " ore and ");
split = split[1].tsplit(" clay. Each geode robot costs ");
String[] split2 = split[0].tsplit(" ore and ");
print.obsidian_cost = {
[ORE] = str::to_int(split2[0])!!,
[CLAY] = str::to_int(split2[1])!!
};
split2 = str::tsplit(split[1], " ore and ");
split2 = split[1].tsplit(" ore and ");
print.geode_cost = {
[ORE] = str::to_int(split2[0])!!,
[OBSIDIAN] = str::to_int(split2[1][..^11])!!
Expand Down
3 changes: 1 addition & 2 deletions day1_1.c3
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import libc;

fn void main()
{
File f;
f.open("calories.txt", "rb")!!;
File f = file::open("calories.txt", "rb")!!;
defer (void)f.close();
usz current_entry = 0;
usz max_entry = 0;
Expand Down
3 changes: 1 addition & 2 deletions day1_2.c3
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ fn void update_max(int entry)

fn void main()
{
File f;
f.open("calories.txt", "rb")!!;
File f = file::open("calories.txt", "rb")!!;
defer (void)f.close();
int current_entry = 0;
while (!f.eof())
Expand Down
6 changes: 2 additions & 4 deletions day2.c3
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ fn Select! selectFromString(String line)

fn void part1()
{
File f;
f.open("solution.txt", "rb")!!;
File f = file::open("solution.txt", "rb")!!;
defer (void)f.close();
int points = 0;
while (!f.eof())
Expand Down Expand Up @@ -79,8 +78,7 @@ fn void part1()

fn void part2()
{
File f;
f.open("solution.txt", "rb")!!;
File f = file::open("solution.txt", "rb")!!;
defer (void)f.close();
int points = 0;
while (!f.eof())
Expand Down
7 changes: 3 additions & 4 deletions day20.c3
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ define LongList = List<long>;

fn void load_crypto(LongList* list, LongList* pos, long key)
{
File f;
f.open("crypto.txt", "rb")!!;
File f = file::open("crypto.txt", "rb")!!;
defer (void)f.close();
while (!f.eof())
{
Expand Down Expand Up @@ -37,7 +36,7 @@ macro print_order(LongList pos)
{
io::printf("%s ", x);
}
io::println();
io::printn();
}

fn void print_list(LongList list, LongList pos)
Expand All @@ -48,7 +47,7 @@ fn void print_list(LongList list, LongList pos)
{
io::printf("%s ", value_at_pos(list, pos, i));
}
io::println();
io::printn();
}


Expand Down
Loading

0 comments on commit e8a7973

Please sign in to comment.