summaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig166
1 files changed, 154 insertions, 12 deletions
diff --git a/src/main.zig b/src/main.zig
index 8966363..56c15fe 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -77,7 +77,7 @@ fn p3() void {
std.debug.warn("{}\n", .{biggest});
}
-fn p4() !void {
+fn p4() void {
var i: usize = 0;
var j: usize = 0;
var str: [6]u8 = undefined;
@@ -85,13 +85,16 @@ fn p4() !void {
while (i < 1000) : (i += 1) {
j = 0;
while (j < 1000) : (j += 1) {
- _ = try std.fmt.bufPrint(&str, "{}", .{i * j});
- if (i * j > largest and
- str[0] == str[5] and
- str[1] == str[4] and
- str[2] == str[3])
- {
- largest = i * j;
+ if (std.fmt.bufPrint(&str, "{}", .{i * j})) |val| {
+ if (i * j > largest and
+ str[0] == str[5] and
+ str[1] == str[4] and
+ str[2] == str[3])
+ {
+ largest = i * j;
+ }
+ } else |err| {
+ print("{}\n", .{err});
}
}
}
@@ -111,6 +114,7 @@ fn p5() void {
while (j != 0) : (j -= 1) {
if (i % j != 0) {
found = true;
+ break;
}
}
@@ -146,17 +150,155 @@ fn p7() void {
print("{}\n", .{i - 1});
}
+fn remove(str: []u8, c: u8) void {
+ var tmp: [1024]u8 = undefined;
+ var counter: u16 = 0;
+ for (str) |char| {
+ if (char != c) {
+ tmp[counter] = char;
+ counter += 1;
+ }
+ }
+
+ insert(str, &tmp);
+}
+
+fn insert(output: []u8, input: []const u8) void {
+ for (input) |_, i| {
+ output[i] = input[i];
+ }
+}
+
+fn len(str: []u8) usize {
+ var i: usize = 0;
+ for (str) |c| {
+ if (c < 128) {
+ i += 1;
+ } else {
+ break;
+ }
+ }
+
+ return i;
+}
+
fn p8() void {
- print("{}\n", .{1});
+ var largest: usize = 0;
+ if (std.fs.cwd().openFile("p8", .{ .read = true })) |file| {
+ var str: [1024]u8 = undefined;
+ if (file.read(&str)) |val| {
+ remove(&str, '\n');
+ var i: usize = 0;
+ while (i < len(&str) - 12) : (i += 1) {
+ var product: usize = 1;
+ for (str[i .. i + 13]) |c| {
+ var n: usize = c - 48;
+ if (n < 10) {
+ product *= c - 48;
+ } else {
+ break;
+ }
+ }
+ if (product > largest) {
+ largest = product;
+ }
+ }
+ } else |err| {
+ print("{}\n", .{err});
+ }
+ } else |err| {
+ print("{}\n", .{err});
+ }
+ print("{}\n", .{largest});
+}
+
+fn sqrt(x: f64) f64 {
+ var z: f64 = 1;
+ var i: u8 = 1;
+ while (i <= 10) : (i += 1) {
+ z -= (z * z - x) / (2 * z);
+ }
+ return z;
+}
+
+fn p9() void {
+ var c: usize = 1;
+ main: while (c <= 1000) : (c += 1) {
+ //var c2: usize = c*c;
+ //while (b
+ var b: usize = 1;
+ while (b < c) : (b += 1) {
+ var a: usize = 1;
+ while (a < b) : (a += 1) {
+ if (a * a + b * b == c * c and a + b + c == 1000) {
+ print("{}\n", .{a * b * c});
+ break :main;
+ }
+ }
+ }
+ }
+}
+
+//fn sum(array: []usize) usize {
+// var counter: usize = 0;
+// for (array) |i| {
+// counter += i;
+// }
+// return counter;
+//}
+
+fn p10() void {
+ const size: usize = 2_000_001;
+ var primes: [size]bool = undefined;
+ for (primes) |prime, i| {
+ primes[i] = true;
+ }
+
+ var i: usize = 2;
+ while (i < size) : (i += 1) {
+ if (primes[i]) {
+ var j: usize = 2;
+ while (j * i < size) : (j += 1) {
+ primes[j * i] = false;
+ }
+ }
+ }
+
+ var sum: usize = 0;
+
+ for (primes) |prime, num| {
+ if (prime and num != 1) {
+ sum += num;
+ }
+ }
+ print("{}\n", .{sum});
+}
+
+fn p11() void {
+ var array: [20][20]usize = undefined;
+
+ if (std.fs.cwd().openFile("p11", .{ .read = true })) |file| {
+ var str: [4096]u8 = undefined;
+ if (file.read(&str)) |val| {
+ print("{}", .{str[0]});
+ } else |err| {
+ print("{}\n", .{err});
+ }
+ } else |err| {
+ print("{}\n", .{err});
+ }
}
pub fn main() !void {
//p1();
//p2();
//p3();
- //_ = try p4();
+ //p4();
//p5();
//p6();
- p7();
- //p8();
+ //p7();
+ p8();
+ p9();
+ //p10();
+ //p11();
}