Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const std = @import("std");
- const Allocator = std.mem.Allocator;
- const data = @embedFile("input0.txt");
- // arena for ints
- var numsArr: [20]i64 = undefined;
- var numsCnt: u8 = 0;
- var testVal: i64 = 0;
- var cnt: i64 = 0;
- fn recursiveOp(op: u8, len: u8, accum: i64) bool {
- if (len == numsCnt) {
- if (accum == testVal)
- return true;
- return false;
- }
- var newval: i64 = undefined;
- if (op == '+') {
- newval = accum + numsArr[len];
- }
- if (op == '*') {
- newval = accum * numsArr[len];
- }
- if (op == '|') {
- newval = accum;
- var b = numsArr[len];
- while (b > 0) {
- newval *= 10;
- b = @divTrunc(b, 10);
- }
- newval += numsArr[len];
- }
- if (recursiveOp('+', len + 1, newval))
- return true;
- if (recursiveOp('*', len + 1, newval))
- return true;
- if (recursiveOp('|', len + 1, newval))
- return true;
- return false;
- }
- fn doProcessing() void {
- const accum = numsArr[0];
- if (recursiveOp('+', 1, accum)) {
- cnt += testVal;
- return;
- }
- if (recursiveOp('*', 1, accum)) {
- cnt += testVal;
- return;
- }
- if (recursiveOp('|', 1, accum)) {
- cnt += testVal;
- return;
- }
- }
- pub fn main() !void {
- var currNum: i64 = 0;
- for (data) |c| {
- if (c == '\r')
- continue;
- if (c == ':') {
- testVal = currNum;
- currNum = 0;
- continue;
- }
- if ((c == ' ') or (c == '\n')) {
- if (0 == currNum)
- continue;
- numsArr[numsCnt] = currNum;
- numsCnt += 1;
- currNum = 0;
- if (c == ' ')
- continue;
- }
- if (c == '\n') {
- doProcessing();
- currNum = 0;
- numsCnt = 0;
- continue;
- }
- currNum *= 10;
- currNum += c - '0';
- }
- std.debug.print("Result: {d}.\n", .{cnt});
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement