-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.zig
62 lines (51 loc) Β· 1.39 KB
/
part1.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const std = @import("std");
const fs = std.fs;
const io = std.io;
const stdout = io.getStdOut().writer();
const allocator = std.heap.page_allocator;
const String = std.ArrayList(u8);
const StringArray = std.ArrayList(String);
pub fn rollRock(board: *StringArray, x: usize, y: usize) void {
var fallY = y;
while (fallY > 0 and board.items[fallY - 1].items[x] == '.') {
board.items[fallY - 1].items[x] = 'O';
board.items[fallY].items[x] = '.';
fallY -= 1;
}
}
pub fn rollRocks(board: *StringArray) void {
const h = board.items.len;
const w = board.items.len;
for (0 .. w) |x| {
for (1 .. h) |y| {
if (board.items[y].items[x] == 'O')
rollRock(board, x, y);
}
}
}
pub fn boardNorthLoad(board: StringArray) usize {
var sum: usize = 0;
for (board.items, 0..) |row, i| {
for (row.items) |tile| {
if (tile == 'O')
sum += board.items.len - i;
}
}
return sum;
}
pub fn main() !void {
var file = try fs.cwd().openFile("input.txt", .{});
defer file.close();
var reader = io.bufferedReader(file.reader());
var stream = reader.reader();
var buf: [256]u8 = undefined;
var board = StringArray.init(allocator);
defer board.deinit();
while (try stream.readUntilDelimiterOrEof(&buf, '\n')) |line| {
var row = String.init(allocator);
try row.appendSlice(line);
try board.append(row);
}
rollRocks(&board);
try stdout.print("{d}\n", .{boardNorthLoad(board)});
}