Skip to content

Commit 2ceb1d6

Browse files
committed
Day 1 part 1
1 parent 6f82575 commit 2ceb1d6

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
Put the `input` file in the same directory as the code then compile and run it, e.g.:
88

99
```sh
10-
cd day01_part1
11-
zig run day01_part1.zig
10+
cd day01
11+
zig run part1.zig
1212
```

day01/part1.zig

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const std = @import("std");
2+
3+
pub fn main() !void {
4+
var file = try std.fs.cwd().openFile("input", .{});
5+
defer file.close();
6+
7+
var buf_reader = std.io.bufferedReader(file.reader());
8+
var in_stream = buf_reader.reader();
9+
10+
var buf: [1024]u8 = undefined;
11+
12+
const first = try in_stream.readUntilDelimiterOrEof(&buf, '\n');
13+
var prev: u32 = try std.fmt.parseInt(u32, first.?, 10);
14+
15+
var inc: u32 = 0;
16+
while (try in_stream.readUntilDelimiterOrEof(&buf, '\n')) |line| {
17+
if (line.len > 0) {
18+
var x: u32 = try std.fmt.parseInt(u32, line, 10);
19+
if (x > prev) {
20+
inc += 1;
21+
}
22+
prev = x;
23+
}
24+
}
25+
26+
const stdout = std.io.getStdOut().writer();
27+
try stdout.print("{d}\n", .{inc});
28+
}

0 commit comments

Comments
 (0)