From 0fa699641aee542d90b4c109235c7c3468ac5ddd Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Thu, 19 Dec 2024 09:18:19 -0800 Subject: [PATCH] Solution for advent of code day 2. (#4708) --- examples/advent2024/BUILD | 16 ++++ examples/advent2024/day2_common.carbon | 13 +++ examples/advent2024/day2_part1.carbon | 64 +++++++++++++ examples/advent2024/day2_part2.carbon | 124 +++++++++++++++++++++++++ examples/advent2024/io_utils.carbon | 24 ++--- 5 files changed, 229 insertions(+), 12 deletions(-) create mode 100644 examples/advent2024/day2_common.carbon create mode 100644 examples/advent2024/day2_part1.carbon create mode 100644 examples/advent2024/day2_part2.carbon diff --git a/examples/advent2024/BUILD b/examples/advent2024/BUILD index c38b1cb61929..b1edc7021fa7 100644 --- a/examples/advent2024/BUILD +++ b/examples/advent2024/BUILD @@ -24,3 +24,19 @@ carbon_binary( "day1_part2.carbon", ] + utils, ) + +carbon_binary( + name = "day2_part1", + srcs = [ + "day2_common.carbon", + "day2_part1.carbon", + ] + utils, +) + +carbon_binary( + name = "day2_part2", + srcs = [ + "day2_common.carbon", + "day2_part2.carbon", + ] + utils, +) diff --git a/examples/advent2024/day2_common.carbon b/examples/advent2024/day2_common.carbon new file mode 100644 index 000000000000..89ff6d87c0eb --- /dev/null +++ b/examples/advent2024/day2_common.carbon @@ -0,0 +1,13 @@ +// 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 + +library "day2_common"; + +fn Abs(n: i32) -> i32 { + return if n < 0 then -n else n; +} + +fn IsSafeDelta(from: i32, to: i32) -> bool { + return from != to and Abs(from - to) <= 3; +} diff --git a/examples/advent2024/day2_part1.carbon b/examples/advent2024/day2_part1.carbon new file mode 100644 index 000000000000..58591d25c0cb --- /dev/null +++ b/examples/advent2024/day2_part1.carbon @@ -0,0 +1,64 @@ +// 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 + +import Core library "io"; + +import library "day2_common"; +import library "io_utils"; + +class ReportState { + fn Make() -> ReportState { + return {.levels_so_far = 0, .previous = 0, + .increasing = false, .safe_so_far = true}; + } + + fn Add[addr self: Self*](level: i32) { + ++self->levels_so_far; + if (self->levels_so_far >= 2) { + if (not IsSafeDelta(self->previous, level)) { + // Difference is zero or too large. + self->safe_so_far = false; + } + + var is_increase: bool = level > self->previous; + if (self->levels_so_far == 2) { + self->increasing = is_increase; + } else if (is_increase != self->increasing) { + // Not monotone. + self->safe_so_far = false; + } + } + self->previous = level; + } + + var levels_so_far: i32; + var previous: i32; + var increasing: bool; + var safe_so_far: bool; +} + +fn ReadReport() -> ReportState { + returned var report: ReportState = ReportState.Make(); + var n: i32; + while (ReadInt(&n)) { + report.Add(n); + SkipSpaces(); + } + return var; +} + +fn Run() { + var safe_reports: i32 = 0; + while (true) { + var report: ReportState = ReadReport(); + if (report.levels_so_far == 0) { + break; + } + if (report.safe_so_far) { + ++safe_reports; + } + SkipNewline(); + } + Core.Print(safe_reports); +} diff --git a/examples/advent2024/day2_part2.carbon b/examples/advent2024/day2_part2.carbon new file mode 100644 index 000000000000..bacacd37b346 --- /dev/null +++ b/examples/advent2024/day2_part2.carbon @@ -0,0 +1,124 @@ +// 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 + +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[addr self: Self*](n: i32) { + self->data[2] = self->data[1]; + self->data[1] = self->data[0]; + self->data[0] = n; + ++self->size; + } + + var data: [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[addr self: 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[addr self: 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: 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(&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); +} diff --git a/examples/advent2024/io_utils.carbon b/examples/advent2024/io_utils.carbon index ce26cbcfe7e6..e8cfe371a9be 100644 --- a/examples/advent2024/io_utils.carbon +++ b/examples/advent2024/io_utils.carbon @@ -6,24 +6,24 @@ library "io_utils"; import Core library "io"; -// TODO: Use Core.EOF() rather than 0 as a sentinel here. -// At the moment, 0 is the only value we can initialize a global to, -// so we store the value plus 1 here. -var push_back: i32 = 0; +// If non-zero, this is the most recently read character plus 2. +// The +2 is necessary to distinguish the case of no unread character +// from the case of unreading an EOF. +var unread_char: i32 = 0; fn ReadChar() -> i32 { - var next: i32 = push_back - 1; - push_back = 0; - // TODO: assert(push_back == Core.EOF()); - if (next == Core.EOF()) { - next = Core.ReadChar(); + if (unread_char != 0) { + var result: i32 = unread_char - 2; + unread_char = 0; + return result; } - return next; + + return Core.ReadChar(); } fn UnreadChar(c: i32) { - // TODO: assert(push_back == Core.EOF()); - push_back = c + 1; + // TODO: assert(unread_char == 0); + unread_char = c + 2; } fn ReadInt(p: i32*) -> bool {