diff options
Diffstat (limited to 'src/main.zig')
-rw-r--r-- | src/main.zig | 169 |
1 files changed, 157 insertions, 12 deletions
diff --git a/src/main.zig b/src/main.zig index 56c15fe..38799a8 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const ArrayList = std.ArrayList; const print = std.debug.warn; fn p1() void { @@ -239,14 +240,6 @@ fn p9() void { } } -//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; @@ -274,19 +267,170 @@ fn p10() void { print("{}\n", .{sum}); } +fn strtoint(str: []u8) usize { + var sum: usize = 0; + var str_len: usize = len(str); + + for (str) |c, j| { + sum += (str[str_len - (j + 1)] - 48) * (exp(10, j)); + } + + return sum; +} + +fn exp(x: usize, y: usize) usize { + var i: usize = 0; + var total: usize = 1; + while (i < y) : (i += 1) { + total *= x; + } + return total; +} + fn p11() void { var array: [20][20]usize = undefined; + var i: usize = 0; + var j: usize = 0; + var k: usize = 0; + if (std.fs.cwd().openFile("p11", .{ .read = true })) |file| { var str: [4096]u8 = undefined; - if (file.read(&str)) |val| { - print("{}", .{str[0]}); + if (file.read(&str)) |str_len| { + while (i < str_len) : (i += 3) { + array[j][k] = strtoint(str[i .. i + 2]); + if (j == 19) { + j = 0; + k += 1; + } else { + j += 1; + } + } } else |err| { print("{}\n", .{err}); } } else |err| { print("{}\n", .{err}); } + + var largest: usize = 0; + var product: usize = 0; + i = 0; + while (i < 16) : (i += 1) { + j = 0; + while (j < 16) : (j += 1) { + product = array[i][j] * array[i + 1][j] * array[i + 2][j] * array[i + 3][j]; + if (product > largest) { + largest = product; + } + product = array[i][j] * array[i][j + 1] * array[i][j + 2] * array[i][j + 3]; + if (product > largest) { + largest = product; + } + product = array[i][j] * array[i + 1][j + 1] * array[i + 2][j + 2] * array[i + 3][j + 3]; + if (product > largest) { + largest = product; + } + product = array[i + 3][j] * array[i + 2][j + 1] * array[i + 1][j + 2] * array[i][j + 3]; + if (product > largest) { + largest = product; + } + } + } + print("{}\n", .{largest}); +} + +fn gen_primes() ArrayList(usize) { + var bool_primes = ArrayList(bool).init(std.heap.page_allocator); + const size: usize = 10_000_000; + var i: usize = 0; + while (i < size) : (i += 1) { + if (bool_primes.append(true)) |val| {} else |err| { + print("{}", .{"allocation error"}); + } + } + + i = 2; + while (i < size) : (i += 1) { + if (bool_primes.items[i]) { + var j: usize = 2; + while (j * i < size) : (j += 1) { + bool_primes.items[j * i] = false; + } + } + } + + i = 2; + var primes = ArrayList(usize).init(std.heap.page_allocator); + while (i < size) : (i += 1) { + if (bool_primes.items[i]) { + if (primes.append(i)) |val| {} else |err| { + print("{}", .{"allocation error"}); + } + } + } + + bool_primes.deinit(); + + return primes; +} + +const Prime = struct { n: usize, count: usize }; +const my_primes_size = 10; + +fn p12() void { + var i: usize = 1; + var triangle: usize = 0; + var largest: usize = 0; + + var primes = gen_primes(); + + while (i < 20000) : (i += 1) { + triangle += i; + var my_primes: [my_primes_size]Prime = undefined; + var count: usize = 0; + + var j: usize = 0; + while (j < my_primes_size) : (j += 1) { + my_primes[j].count = 0; + } + + j = 0; + while (j < primes.items.len) : (j += 1) { + if (triangle % primes.items[j] == 0) { + var prime: Prime = .{ .n = primes.items[j], .count = 0 }; + my_primes[count] = prime; + count += 1; + } else if (primes.items[j] > triangle) { + break; + } + } + + var t: usize = triangle; + while (t != 1) { + j = 0; + while (j < count) : (j += 1) { + if (t % my_primes[j].n == 0) { + t /= my_primes[j].n; + my_primes[j].count += 1; + } + } + } + + j = 0; + var divisor_amount: usize = 1; + while (j < my_primes_size) : (j += 1) { + if (my_primes[j].count == 0) { + break; + } + divisor_amount *= my_primes[j].count + 1; + } + + if (divisor_amount > 500) { + print("{},{}\n", .{ triangle, divisor_amount }); + break; + } + } } pub fn main() !void { @@ -297,8 +441,9 @@ pub fn main() !void { //p5(); //p6(); //p7(); - p8(); - p9(); + //p8(); + //p9(); //p10(); //p11(); + p12(); } |