Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution for advent of code day 4. #4718

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions examples/advent2024/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,19 @@ carbon_binary(
"day3_part2.carbon",
] + utils,
)

carbon_binary(
name = "day4_part1",
srcs = [
"day4_common.carbon",
"day4_part1.carbon",
] + utils,
)

carbon_binary(
name = "day4_part2",
srcs = [
"day4_common.carbon",
"day4_part2.carbon",
] + utils,
)
56 changes: 56 additions & 0 deletions examples/advent2024/day4_common.carbon
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 "day4_common";

import library "io_utils";

class Wordsearch {
fn Read() -> Wordsearch {
returned var s: Wordsearch;
// TODO: Use for loops once they're implemented.
var y: i32 = 0;
while (y < 140) {
var x: i32 = 0;
while (x < 140) {
// TODO: Assert on failure.
s.grid[x][y] = ReadChar();
++x;
}
// TODO: Assert on failure.
SkipNewline();
++y;
}
return var;
}

fn At[self: Self](x: i32, y: i32) -> i32 {
return if x < 0 or x >= 140 or y < 0 or y >= 140 then -1 else self.grid[x][y];
}

// TODO: Make this generic in the length of the search query.
fn Check4[self: Self](xmas: [i32; 4], x: i32, y: i32, dx: i32, dy: i32) -> bool {
var i: i32 = 0;
while (i < 4) {
if (self.At(x + i * dx, y + i * dy) != xmas[i]) {
return false;
}
++i;
}
return true;
}

fn Check3[self: Self](mas: [i32; 3], x: i32, y: i32, dx: i32, dy: i32) -> bool {
var i: i32 = 0;
while (i < 3) {
if (self.At(x + i * dx, y + i * dy) != mas[i]) {
return false;
}
++i;
}
return true;
}

var grid: [[i32; 140]; 140];
}
36 changes: 36 additions & 0 deletions examples/advent2024/day4_part1.carbon
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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 "day4_common";
import library "io_utils";

fn Run() {
var search: Wordsearch = Wordsearch.Read();
var xmas: [i32; 4] = (0x58, 0x4D, 0x41, 0x53);
var found: i32 = 0;

// TODO: Use for loops once they're implemented.
var y: i32 = 0;
while (y < 140) {
var x: i32 = 0;
while (x < 140) {
var dy: i32 = -1;
while (dy <= 1) {
var dx: i32 = -1;
while (dx <= 1) {
if (search.Check4(xmas, x, y, dx, dy)) {
++found;
}
++dx;
}
++dy;
}
++x;
}
++y;
}
Core.Print(found);
}
31 changes: 31 additions & 0 deletions examples/advent2024/day4_part2.carbon
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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 "day4_common";
import library "io_utils";

fn Run() {
var search: Wordsearch = Wordsearch.Read();
var mas: [i32; 3] = (0x4D, 0x41, 0x53);
var found: i32 = 0;

// TODO: Use for loops once they're implemented.
var y: i32 = 1;
while (y < 139) {
var x: i32 = 1;
while (x < 139) {
if ((search.Check3(mas, x - 1, y - 1, 1, 1) or
search.Check3(mas, x + 1, y + 1, -1, -1)) and
(search.Check3(mas, x - 1, y + 1, 1, -1) or
search.Check3(mas, x + 1, y - 1, -1, 1))) {
++found;
}
++x;
}
++y;
}
Core.Print(found);
}
Loading