|
| 1 | +const std = @import("std"); |
| 2 | +const Allocator = std.mem.Allocator; |
| 3 | +const string = []const u8; |
| 4 | + |
| 5 | +/// Task 1 - Calculate the sum over all multiplication instructions. |
| 6 | +/// |
| 7 | +/// Arguments: |
| 8 | +/// - `contents`: Input file contents. |
| 9 | +/// |
| 10 | +/// Returns: |
| 11 | +/// - Sum of all multiplications. |
| 12 | +pub fn sum_of_multiplication_instructions(contents: string) !u32 { |
| 13 | + const min_instruction_len: usize = 8; |
| 14 | + |
| 15 | + var product_sum: u32 = 0; |
| 16 | + var i: usize = 0; |
| 17 | + while (i <= contents.len - min_instruction_len) { |
| 18 | + product_sum += parse_mul_instruction(contents, &i) catch { |
| 19 | + continue; |
| 20 | + }; |
| 21 | + } |
| 22 | + |
| 23 | + return product_sum; |
| 24 | +} |
| 25 | + |
| 26 | +/// Task 2 - Calculate the sum over the multiplication instructions which are |
| 27 | +/// activated via the do() instructions. |
| 28 | +/// |
| 29 | +/// Arguments: |
| 30 | +/// - `contents`: Input file contents. |
| 31 | +/// |
| 32 | +/// Returns: |
| 33 | +/// - Sum of all active multiplications. |
| 34 | +pub fn conditional_sum_of_multiplication_instructions(contents: string) !u32 { |
| 35 | + const min_instruction_len: usize = 8; |
| 36 | + |
| 37 | + var product_sum: u32 = 0; |
| 38 | + var do: bool = true; |
| 39 | + var i: usize = 0; |
| 40 | + while (i <= contents.len - min_instruction_len) { |
| 41 | + do = parse_conditional_instructions(contents, &i) catch do; |
| 42 | + if (do) { |
| 43 | + product_sum += parse_mul_instruction(contents, &i) catch { |
| 44 | + continue; |
| 45 | + }; |
| 46 | + } else { |
| 47 | + i += 1; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return product_sum; |
| 52 | +} |
| 53 | + |
| 54 | +// -------------------------------------------------------------------------- \\ |
| 55 | + |
| 56 | +/// Parse a single multiplication instruction. |
| 57 | +/// |
| 58 | +/// Arguments: |
| 59 | +/// - `contents`: The instruction string. |
| 60 | +/// - `i`: Pointer to the current position in the instruction string. |
| 61 | +/// |
| 62 | +/// Returns: |
| 63 | +/// - The result of the multiplication instruction if one was found. |
| 64 | +fn parse_mul_instruction(contents: string, i: *usize) !u32 { |
| 65 | + if (!std.mem.eql(u8, contents[(i.*)..(i.* + 4)], "mul(")) { |
| 66 | + i.* += 1; |
| 67 | + return error.InvalidInstruction; |
| 68 | + } |
| 69 | + i.* += 4; |
| 70 | + |
| 71 | + const multiplier = try parse_three_digit_number(contents[(i.*)..(i.* + 3)], i); |
| 72 | + if (contents[i.*] != ',') { |
| 73 | + return error.InvalidInstruction; |
| 74 | + } |
| 75 | + i.* += 1; |
| 76 | + |
| 77 | + const multiplicand = try parse_three_digit_number(contents[(i.*)..(i.* + 3)], i); |
| 78 | + if (contents[i.*] != ')') { |
| 79 | + return error.InvalidInstruction; |
| 80 | + } |
| 81 | + i.* += 1; |
| 82 | + |
| 83 | + return multiplier * multiplicand; |
| 84 | +} |
| 85 | + |
| 86 | +/// Parse a single integer up to three digits. |
| 87 | +/// |
| 88 | +/// Arguments: |
| 89 | +/// - `contents`: The instruction string. |
| 90 | +/// - `i`: Pointer to the current position in the instruction string. |
| 91 | +/// |
| 92 | +/// Returns: |
| 93 | +/// - The integer if one was found. |
| 94 | +fn parse_three_digit_number(str: string, i: *usize) !u32 { |
| 95 | + var num = try std.fmt.parseInt(u32, str[0..1], 10); |
| 96 | + i.* += 1; |
| 97 | + |
| 98 | + if (str[1] != ',' and str[1] != ')') { |
| 99 | + num *= 10; |
| 100 | + num += try std.fmt.parseInt(u32, str[1..2], 10); |
| 101 | + } else { |
| 102 | + return num; |
| 103 | + } |
| 104 | + i.* += 1; |
| 105 | + |
| 106 | + if (str[2] != ',' and str[2] != ')') { |
| 107 | + num *= 10; |
| 108 | + num += try std.fmt.parseInt(u32, str[2..3], 10); |
| 109 | + } else { |
| 110 | + return num; |
| 111 | + } |
| 112 | + i.* += 1; |
| 113 | + |
| 114 | + return num; |
| 115 | +} |
| 116 | + |
| 117 | +/// Parse a single conditional instruction: do() or don't(). |
| 118 | +/// |
| 119 | +/// Arguments: |
| 120 | +/// - `contents`: The instruction string. |
| 121 | +/// - `i`: Pointer to the current position in the instruction string. |
| 122 | +/// |
| 123 | +/// Returns: |
| 124 | +/// - The true if do() was found and false if don't() was found. |
| 125 | +fn parse_conditional_instructions(contents: string, i: *usize) !bool { |
| 126 | + if (std.mem.eql(u8, contents[(i.*)..(i.* + 4)], "do()")) { |
| 127 | + i.* += 4; |
| 128 | + return true; |
| 129 | + } |
| 130 | + |
| 131 | + if (std.mem.eql(u8, contents[(i.*)..(i.* + 7)], "don't()")) { |
| 132 | + i.* += 7; |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + return error.InvalidInstruction; |
| 137 | +} |
0 commit comments