mirror of
https://codeberg.org/andyscott/advent-of-code.git
synced 2025-01-04 22:55:54 -05:00
2015: Day 1 complete
This commit is contained in:
parent
1a2e2eb9ad
commit
ebaffe961f
2 changed files with 56 additions and 0 deletions
1
2015/day1.txt
Normal file
1
2015/day1.txt
Normal file
File diff suppressed because one or more lines are too long
55
2015/day1.zig
Normal file
55
2015/day1.zig
Normal file
|
@ -0,0 +1,55 @@
|
|||
const std = @import("std");
|
||||
|
||||
fn part1(directions: std.ArrayList(u8), floor: *i16) void {
|
||||
for (directions.items) |direction| {
|
||||
if (direction == '(') {
|
||||
floor.* += 1;
|
||||
} else if (direction == ')') {
|
||||
floor.* -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn part2(directions: std.ArrayList(u8), pos: *i16) void {
|
||||
var floor: i16 = 0;
|
||||
for (directions.items) |direction| {
|
||||
if (direction == '(') {
|
||||
floor += 1;
|
||||
} else if (direction == ')') {
|
||||
floor -= 1;
|
||||
}
|
||||
pos.* += 1;
|
||||
|
||||
if (floor == -1) break;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
const file = try std.fs.cwd().openFile("day1.txt", .{});
|
||||
defer file.close();
|
||||
|
||||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
|
||||
var buf = std.io.bufferedReader(file.reader());
|
||||
|
||||
var directions = std.ArrayList(u8).init(alloc);
|
||||
defer directions.deinit();
|
||||
|
||||
while (true) {
|
||||
buf.reader().streamUntilDelimiter(directions.writer(), '\n', null) catch |err| switch (err) {
|
||||
error.EndOfStream => break,
|
||||
else => return err,
|
||||
};
|
||||
}
|
||||
|
||||
var floor: i16 = 0;
|
||||
var position: i16 = 0;
|
||||
|
||||
part1(directions, &floor);
|
||||
part2(directions, &position);
|
||||
|
||||
std.debug.print("Ending floor: {d}\n", .{floor});
|
||||
std.debug.print("Position when first entering basement: {d}\n", .{position});
|
||||
}
|
Loading…
Reference in a new issue