aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOrfeas <38209077+0xfea5@users.noreply.github.com>2023-12-07 22:55:14 +0200
committerOrfeas <38209077+0xfea5@users.noreply.github.com>2025-10-28 23:20:45 +0200
commit8b0763bd257f79b86319cf762ba596a5c241eeab (patch)
tree4f3b6f19df571314a6e8ff0b37ef2166dd52b0c0
parentday5 (diff)
downloadaoc23-8b0763bd257f79b86319cf762ba596a5c241eeab.tar.gz
aoc23-8b0763bd257f79b86319cf762ba596a5c241eeab.zip
day6
-rw-r--r--day06/solution.zig85
1 files changed, 85 insertions, 0 deletions
diff --git a/day06/solution.zig b/day06/solution.zig
new file mode 100644
index 0000000..a66268d
--- /dev/null
+++ b/day06/solution.zig
@@ -0,0 +1,85 @@
1const std = @import("std");
2const print = std.debug.print;
3const assert = std.debug.assert;
4const ArrayList = std.ArrayList;
5const HashMap = std.HashMap;
6const mem = std.mem;
7
8const fin = mem.trim(u8, @embedFile("./input.txt"), &std.ascii.whitespace);
9var gpa = std.heap.GeneralPurposeAllocator(.{}){};
10const allocator = gpa.allocator();
11
12fn evalutate(rd: i64, rt: i64) u64 {
13 var timesBeat: u64 = 0;
14
15 for (1..@bitCast(rt + 1)) |tt| {
16 const t: i64 = @bitCast(tt);
17 const d = t * rt - t * t;
18
19 if (d > rd) {
20 timesBeat += 1;
21 }
22 }
23
24 assert(timesBeat != 0);
25 return timesBeat;
26}
27
28pub fn part1(recTimes: []const i64, recDists: []const i64) void {
29 var ans: u64 = 1;
30
31 for (recDists, recTimes) |rd, rt| {
32 ans *= evalutate(rd, rt);
33 }
34
35 print("{d}\n", .{ans});
36}
37
38pub fn part2(recTimes: []const []const u8, recDists: []const []const u8) void {
39 const rtStr = mem.concat(allocator, u8, recTimes) catch unreachable;
40 const rdStr = mem.concat(allocator, u8, recDists) catch unreachable;
41 defer {
42 allocator.free(rtStr);
43 allocator.free(rdStr);
44 }
45
46 const rt = std.fmt.parseInt(i64, rtStr, 10) catch unreachable;
47 const rd = std.fmt.parseInt(i64, rdStr, 10) catch unreachable;
48
49 const ans = evalutate(rd, rt);
50
51 print("{d}\n", .{ans});
52}
53
54pub fn main() !void {
55 var recTimes = ArrayList(i64).init(allocator);
56 var recDists = ArrayList(i64).init(allocator);
57 var recTimesStr = ArrayList([]const u8).init(allocator);
58 var recDistsStr = ArrayList([]const u8).init(allocator);
59 defer {
60 recTimes.deinit();
61 recDists.deinit();
62 recTimesStr.deinit();
63 recDistsStr.deinit();
64 }
65
66 var splitLines = mem.splitScalar(u8, fin, '\n');
67 var timesText = mem.splitScalar(u8, splitLines.next().?, ' ');
68 var distsText = mem.splitScalar(u8, splitLines.next().?, ' ');
69
70 _ = timesText.next().?;
71 _ = distsText.next().?;
72
73 while (timesText.next()) |time| {
74 try recTimes.append(std.fmt.parseInt(i64, time, 10) catch continue);
75 try recTimesStr.append(time);
76 }
77
78 while (distsText.next()) |dist| {
79 try recDists.append(std.fmt.parseInt(i64, dist, 10) catch continue);
80 try recDistsStr.append(dist);
81 }
82
83 part1(recTimes.items, recDists.items);
84 part2(recTimesStr.items, recDistsStr.items);
85}