Files
carbon-lang/examples/advent2024/day7_common.carbon
T
Dana Jansensandzygoloid 24bde46181 Change array syntax from [T; N] to array(T, N) (#4981)
In line with the proposal in #4682, this changes the array syntax to be
array(T, N). `array` is a builtin keyword which must be followed by
parens containing two expressions and a separating comma.

The array type expression is still fully builtin, it does not forward to
a Core.Array library type yet. It merely adds the `ArrayType`
instruction, as was done with the previous syntax.

Followup work will change the instruction to reference to Core.Array,
once the library type exists and can be used directly.

---------

Co-authored-by: zygoloid <richard@metafoo.co.uk>
2025-02-20 22:42:47 +00:00

59 lines
1.4 KiB
Plaintext

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// https://adventofcode.com/2024/day/7
library "day7_common";
import Core library "io";
import library "io_utils";
fn Concat(a_val: i64, b_val: i64) -> i64 {
var a: i64 = a_val;
var b: i64 = b_val;
if (b == 0) {
return a * 10;
}
while (b != 0) {
a = a * 10;
b = b / 10;
}
return a + b_val;
}
class Equation {
fn Read() -> Equation {
returned var me: Equation;
me.num_operands = 0;
ReadInt64(&me.result);
ConsumeChar(0x3A);
while (ConsumeChar(0x20)) {
ReadInt64(&me.operands[me.num_operands]);
++me.num_operands;
}
while (SkipNewline()) {}
return var;
}
fn SolveFrom[self: Self](start: i32, value: i64, concat: bool) -> bool {
if (value > self.result) {
return false;
}
if (start == self.num_operands) {
return value == self.result;
}
return self.SolveFrom(start + 1, value + self.operands[start], concat) or
self.SolveFrom(start + 1, value * self.operands[start], concat) or
(concat and self.SolveFrom(start + 1, Concat(value, self.operands[start]), true));
}
fn Solve[self: Self](concat: bool) -> bool {
return self.SolveFrom(1, self.operands[0], concat);
}
var operands: array(i64, 16);
var num_operands: i32;
var result: i64;
}