-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
171 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Advent of Code 2024 | ||
|
||
<!-- | ||
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 | ||
--> | ||
|
||
This directory contains sample solutions written in Carbon for | ||
[Advent of Code 2024](https://adventofcode.com/2024/). | ||
|
||
The Carbon toolchain is in a very early state, so these samples frequently need | ||
to work around missing functionality and are not reflective of expected Carbon | ||
style and idioms. Instead, the purpose of these examples are to test the current | ||
state of the toolchain against larger code examples than those that are present | ||
in the toolchain's own tests, to find bugs in the toolchain, and to drive | ||
feature development in the toolchain by presenting somewhat realistic testcases. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// 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 "day1_common"; | ||
|
||
import library "io_utils"; | ||
|
||
// Read a sequence of lines each containing a pair of numbers into two arrays. | ||
// Returns the number of lines read. | ||
fn ReadInputs(ap: [i32; 1000]*, bp: [i32; 1000]*) -> i32 { | ||
var n: i32 = 0; | ||
var a: i32; | ||
var b: i32; | ||
while (n < 1000 and ReadInt(&a) and SkipSpaces() and ReadInt(&b) and SkipNewline()) { | ||
(*ap)[n] = a; | ||
(*bp)[n] = b; | ||
++n; | ||
} | ||
return n; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// 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 "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; | ||
|
||
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(); | ||
} | ||
return next; | ||
} | ||
|
||
fn UnreadChar(c: i32) { | ||
// TODO: assert(push_back == Core.EOF()); | ||
push_back = c + 1; | ||
} | ||
|
||
fn ReadInt(p: i32*) -> bool { | ||
var read_any_digits: bool = false; | ||
*p = 0; | ||
|
||
while (true) { | ||
var c: i32 = ReadChar(); | ||
if (c < 0x30 or c > 0x39) { | ||
UnreadChar(c); | ||
break; | ||
} | ||
// TODO: Check for overflow. | ||
*p *= 10; | ||
*p += c - 0x30; | ||
read_any_digits = true; | ||
} | ||
return read_any_digits; | ||
} | ||
|
||
fn SkipSpaces() -> bool { | ||
var skipped_any_spaces: bool = false; | ||
while (true) { | ||
var c: i32 = ReadChar(); | ||
if (c != 0x20) { | ||
UnreadChar(c); | ||
break; | ||
} | ||
skipped_any_spaces = true; | ||
} | ||
return skipped_any_spaces; | ||
} | ||
|
||
fn SkipNewline() -> bool { | ||
var c: i32 = ReadChar(); | ||
// Optional carriage return. | ||
if (c == 0x0D) { | ||
c = ReadChar(); | ||
} | ||
// Newline. | ||
if (c == 0x0A) { | ||
return true; | ||
} | ||
// TODO: Unread the CR? | ||
UnreadChar(c); | ||
return false; | ||
} |
Oops, something went wrong.