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 5. #4724

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions examples/advent2024/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,35 @@ 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,
)

carbon_binary(
name = "day5_part1",
srcs = [
"day5_common.carbon",
"day5_part1.carbon",
] + utils,
)

carbon_binary(
name = "day5_part2",
srcs = [
"day5_common.carbon",
"day5_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);
}
137 changes: 137 additions & 0 deletions examples/advent2024/day5_common.carbon
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// 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 "day5_common";

import library "io_utils";

// fn LeftShift(a: Core.UInt(100), b: i32) -> Core.UInt(100) = "int.left_shift";
fn LeftShift_u100(a: Core.UInt(100), b: Core.UInt(100)) -> Core.UInt(100) = "int.left_shift";

// TODO: Add a builtin that can do this!
fn Promote(a: i32) -> Core.UInt(100) {
var result: Core.UInt(100) = 0;
var bit_i32: i32 = 1;
var bit_u100: Core.UInt(100) = 1;
while (bit_i32 != 0) {
if (a & bit_i32 != 0) {
result = result | bit_u100;
}
bit_i32 <<= 1;
// TODO: bit_u100 <<= 1;
bit_u100 = bit_u100 << 1;
}
return result;
}

fn LeftShift(a: Core.UInt(100), b: i32) -> Core.UInt(100) {
return LeftShift_u100(a, Promote(b));
}

fn PageMask(page: i32) -> Core.UInt(100) {
return LeftShift(1 as Core.UInt(100), page);
}

class Rules {
fn Read() -> Rules {
returned var rules: Rules;
var i: i32 = 0;
while (i < 100) {
rules.disallowed_before[i] = 0;
++i;
}

var a: i32;
var b: i32;
while (ReadInt(&a) and ConsumeChar(0x7C) and ReadInt(&b)) {
// TODO: rules.disallowed_before[a] |= PageMask(b);
rules.disallowed_before[a] = rules.disallowed_before[a] | PageMask(b);
SkipNewline();
}
return var;
}

fn IsValidOrder[self: Self](a: i32, b: i32) -> bool {
return self.disallowed_before[b] & PageMask(a) == 0;
}

var disallowed_before: [Core.UInt(100); 100];
};

class PageList {
fn Empty() -> PageList {
returned var me: PageList;
me.num_pages = 0;
return var;
}

fn Read() -> PageList {
returned var me: PageList = Empty();

var page: i32;
if (not ReadInt(&page)) {
return var;
}
me.Add(page);
while (ConsumeChar(0x2C)) {
ReadInt(&page);
me.Add(page);
}
SkipNewline();
return var;
}

fn Add[addr self: Self*](page: i32) {
self->pages[self->num_pages] = page;
++self->num_pages;
}

fn FollowsRules[self: Self](rules: Rules) -> bool {
var seen: Core.UInt(100) = 0;
var i: i32 = 0;
while (i < self.num_pages) {
let page: i32 = self.pages[i];
if (seen & rules.disallowed_before[page] != 0) {
return false;
}
// TODO: seen |= PageMask(page);
seen = seen | PageMask(page);
++i;
}
return true;
}

fn IsPossibleFirstPage[self: Self](rules: Rules, page: i32) -> bool {
var i: i32 = 0;
while (i < self.num_pages) {
if (not rules.IsValidOrder(page, self.pages[i])) {
return false;
}
++i;
}
return true;
}

fn ExtractPossibleFirstPage[addr self: Self*](rules: Rules) -> i32 {
var i: i32 = 0;
while (i < self->num_pages) {
var page: i32 = self->pages[i];
if (self->IsPossibleFirstPage(rules, page)) {
self->pages[i] = self->pages[self->num_pages - 1];
--self->num_pages;
return page;
}
++i;
}
// TODO: Assert.
return 0;
}

fn MiddlePage[self: Self]() -> i32 {
return self.pages[self.num_pages / 2];
}

var pages: [i32; 24];
var num_pages: i32;
};
24 changes: 24 additions & 0 deletions examples/advent2024/day5_part1.carbon
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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 "day5_common";
import library "io_utils";

fn Run() {
var rules: Rules = Rules.Read();
SkipNewline();
var total: i32 = 0;
while (true) {
var page_list: PageList = PageList.Read();
if (page_list.num_pages == 0) {
break;
}
if (page_list.FollowsRules(rules)) {
total += page_list.MiddlePage();
}
}
Core.Print(total);
}
29 changes: 29 additions & 0 deletions examples/advent2024/day5_part2.carbon
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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 "day5_common";
import library "io_utils";

fn Run() {
var rules: Rules = Rules.Read();
SkipNewline();
var total: i32 = 0;
while (true) {
var page_list: PageList = PageList.Read();
if (page_list.num_pages == 0) {
break;
}
if (page_list.FollowsRules(rules)) {
continue;
}
var new_page_list: PageList = PageList.Empty();
while (page_list.num_pages != 0) {
new_page_list.Add(page_list.ExtractPossibleFirstPage(rules));
}
total += new_page_list.MiddlePage();
}
Core.Print(total);
}
Loading