Skip to content

Commit

Permalink
[2018] Day 1 in python and Zig
Browse files Browse the repository at this point in the history
  • Loading branch information
agagniere committed Dec 3, 2023
1 parent 7680d46 commit dc86c75
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
22 changes: 22 additions & 0 deletions 2018/01/both.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def lines():
while True:
try:
yield input()
except:
break

def find_duplicate(changes: list[int]):
seen = set()
freq = 0
while True:
for change in changes:
if freq in seen:
return freq
seen.add(freq)
freq += change


changes = [int(line) for line in lines()]

print('Resulting frequency:', sum(changes))
print('First repeated frequency:', find_duplicate(changes))
26 changes: 26 additions & 0 deletions 2018/01/first.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const std = @import("std");

var buffered_in = std.io.bufferedReader(std.io.getStdIn().reader());
var source = buffered_in.reader();

const LineIterator = struct {
buffer: [1024]u8 = undefined,

pub fn next(self: *LineIterator) ?[]u8 {
return source.readUntilDelimiterOrEof(&self.buffer, '\n') catch null;
}
};

pub fn main() !void {
var lines = LineIterator{};
var total: i64 = 0;

while (lines.next()) |line| {
const value = std.fmt.parseInt(i32, line, 10) catch |err| {
std.debug.print("Unable to parse into an int: {s}\n", .{line});
return err;
};
total += value;
}
try std.fmt.format(std.io.getStdOut().writer(), "Resulting frequency: {}\n", .{total});
}
50 changes: 50 additions & 0 deletions 2018/01/second.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const std = @import("std");

var buffered_in = std.io.bufferedReader(std.io.getStdIn().reader());
var source = buffered_in.reader();

const LineIterator = struct {
buffer: [1024]u8 = undefined,

pub fn next(self: *LineIterator) ?[]u8 {
return source.readUntilDelimiterOrEof(&self.buffer, '\n') catch null;
}
};

fn asIntArrayList(comptime T: type, lines: *LineIterator) !std.ArrayList(T) {
var result = try std.ArrayList(T).initCapacity(std.heap.page_allocator, 1000);

while (lines.next()) |line| {
const value = std.fmt.parseInt(T, line, 10) catch {
std.debug.print("Unable to parse into an int: {s}\n", .{line});
continue;
};
result.append(value) catch break;
}
return result;
}

fn findDuplicate(changes: std.ArrayList(i32)) !i64 {
var frequency: i64 = 0;
var seen = std.AutoHashMap(i64, bool).init(std.heap.page_allocator);

while (true) {
for (changes.items) |change| {
if (seen.contains(frequency)) {
return frequency;
}
try seen.put(frequency, true);
frequency += change;
}
}
}

pub fn main() !void {
var iterator = LineIterator{};
const changes = try asIntArrayList(i32, &iterator);
defer changes.deinit();

const duplicate: i64 = try findDuplicate(changes);

try std.fmt.format(std.io.getStdOut().writer(), "Repeated frequency: {}\n", .{duplicate});
}

0 comments on commit dc86c75

Please sign in to comment.