Files
carbon-lang/examples/advent2024/day2_part2.carbon
T
Chandler Carruthandjosh11b 7871237c15 Move self to the explicit () parameter list (proposal #7016) (#7272)
Implements proposal #7016: `self` moves from the deduced implicit list
(`fn F[self: Self]()`) to the front of the explicit list. Its type may
be written explicitly (`fn F(self: Self)`) or omitted, in which case it
defaults to `Self` (`fn F(self)`, `fn F(ref self)`); `self` in the
implicit list is rejected.

Throughout checking, `self` is modeled as the first explicit parameter.
Because a method is just a function whose first parameter is `self`, it
can also be called as an ordinary function with the receiver passed
explicitly (`Type.M(obj, ...)`), not only as `obj.M(...)`. A new
`SemIR::CallArgParamPatterns` helper chooses the parameters matched
against the explicit arguments, excluding a leading `self` only when it
is supplied as a method-call receiver; arity checking, conversion, and
generic deduction use it. The resulting SemIR and lowering are
unchanged: `self` is still `call_param0`, and witnesses, thunks, and
vtables are unaffected.

An omitted `self` type is parsed as a `SelfBindingPattern` node with no
type expression; checking synthesizes the `Self` type so it behaves
exactly like `self: Self`. However, the exact spelling used must match
between a forward declaration and a definition, following #3763's rules
around declaration matching.

Generated functions, thunks, and C++ interop import/export build `self`
as the first explicit parameter, and the `self`-type override (e.g.
Derived->Base for a virtual override) applies to the explicit `self`.
Placement is validated by new diagnostics: `SelfInImplicitParamList`,
`SelfNotFirstParam`, and `SelfOutsideParamList`. The benchmark source
generator and the documentation adopt the `(self)` shorthand; the
prelude, the examples, and the test data are migrated in the following
commits.

Assisted-by: Claude Code with Claude Opus 4.7

---------

Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com>
2026-06-10 15:30:43 +00:00

127 lines
3.5 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/2
import Core library "io";
import library "day2_common";
import library "io_utils";
// A three-element sliding window of recent levels.
class Window {
fn Make() -> Window { return {.data = (0,0,0), .size = 0}; }
fn Add(ref self, n: i32) {
self.data[2] = self.data[1];
self.data[1] = self.data[0];
self.data[0] = n;
++self.size;
}
var data: array(i32, 3);
var size: i32;
}
// Determines whether a transition from `from` to `to` is safe.
fn IsSafe(from: i32, to: i32, want_increase: bool) -> bool {
var is_increase: bool = to > from;
return IsSafeDelta(from, to) and is_increase == want_increase;
}
class ReportState {
fn Make(increasing: bool) -> ReportState {
return {.increasing = increasing, .bad_edges = 0,
.removable_single_bad_edge = false,
.removable_double_bad_edge = false};
}
fn OnAdd(ref self, window: Window) {
if (window.size < 2) {
return;
}
// We label the three most recent levels as `a b c`, with `c` the most
// recent. We might not have an `a`.
let b: i32 = window.data[1];
let c: i32 = window.data[0];
// Keep a count of the unsafe edges.
let b_to_c: bool = IsSafe(window.data[1], window.data[0], self.increasing);
if (not b_to_c) {
++self.bad_edges;
// We have a removable single bad edge if the first edge is unsafe.
if (window.size == 2) {
self.removable_single_bad_edge = true;
}
}
if (window.size >= 3) {
let a: i32 = window.data[2];
if (IsSafe(a, c, self.increasing)) {
let lhs: bool = IsSafe(a, b, self.increasing);
let rhs: bool = b_to_c;
if (not lhs and not rhs) {
// If a->b and b->c are both unsafe, but a->c is safe,
// then we have a removable double bad edge.
self.removable_double_bad_edge = true;
} else if (not lhs or not rhs) {
// If a->b or b->c is unsafe but a->c is safe, then we have a
// removable single bad edge.
self.removable_single_bad_edge = true;
}
}
}
}
fn OnFinish(ref self, window: Window) {
if (window.size >= 2 and
not IsSafe(window.data[1], window.data[0], self.increasing)) {
// We have a removable single bad edge if the last edge is unsafe.
self.removable_single_bad_edge = true;
}
}
fn IsSafeWithRemoval(self) -> bool {
return self.bad_edges == 0 or
(self.bad_edges == 1 and self.removable_single_bad_edge) or
(self.bad_edges == 2 and self.removable_double_bad_edge);
}
var increasing: bool;
var bad_edges: i32;
var removable_single_bad_edge: bool;
var removable_double_bad_edge: bool;
}
fn ReadAndCheckReport() -> bool {
var window: Window = Window.Make();
var increasing: ReportState = ReportState.Make(true);
var decreasing: ReportState = ReportState.Make(false);
var n: i32;
while (ReadInt(ref n)) {
window.Add(n);
increasing.OnAdd(window);
decreasing.OnAdd(window);
SkipSpaces();
}
increasing.OnFinish(window);
decreasing.OnFinish(window);
return window.size > 0 and
(increasing.IsSafeWithRemoval() or decreasing.IsSafeWithRemoval());
}
fn Run() {
var safe_reports: i32 = 0;
while (true) {
if (ReadAndCheckReport()) {
++safe_reports;
}
if (not SkipNewline()) {
break;
}
}
Core.Print(safe_reports);
}