-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
} |