-
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.
Solution for advent of code day 3, both parts.
- Loading branch information
Showing
5 changed files
with
135 additions
and
16 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,45 @@ | ||
// 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 "day3_common"; | ||
|
||
import library "io_utils"; | ||
|
||
// Reads "mul(a,b)", and returns (true, a, b). | ||
// On error, stops before the first invalid character and returns (false, 0, 0). | ||
// TODO: -> Optional((i32, i32)) | ||
fn ReadMul() -> (bool, i32, i32) { | ||
var a: i32; | ||
var b: i32; | ||
if (ConsumeChar(0x6D) and ConsumeChar(0x75) and ConsumeChar(0x6C) and | ||
ConsumeChar(0x28) and ReadInt(&a) and ConsumeChar(0x2C) and | ||
ReadInt(&b) and ConsumeChar(0x29)) { | ||
return (true, a, b); | ||
} | ||
return (false, 0, 0); | ||
} | ||
|
||
// Reads "do()" or "don't()", and returns (true, was_do). | ||
// On error, stops before the first invalid character and returns (false, false). | ||
fn ReadDoOrDont() -> (bool, bool) { | ||
// "do" | ||
if (not ConsumeChar(0x64) or not ConsumeChar(0x6F)) { | ||
return (false, false); | ||
} | ||
|
||
var do: bool = true; | ||
// "n't" | ||
if (ConsumeChar(0x6E)) { | ||
if (not ConsumeChar(0x27) or not ConsumeChar(0x74)) { | ||
return (false, false); | ||
} | ||
do = false; | ||
} | ||
|
||
// "()" | ||
if (not ConsumeChar(0x28) or not ConsumeChar(0x29)) { | ||
return (false, false); | ||
} | ||
return (true, do); | ||
} |
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,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 "day3_common"; | ||
import library "io_utils"; | ||
|
||
fn Run() { | ||
var total: i32 = 0; | ||
while (PeekChar() != Core.EOF()) { | ||
if (PeekChar() == 0x6D) { | ||
// TODO: Use `if let` when available. | ||
let result: (bool, i32, i32) = ReadMul(); | ||
if (result.0) { | ||
total += result.1 * result.2; | ||
} | ||
} else { | ||
ReadChar(); | ||
} | ||
} | ||
Core.Print(total); | ||
} |
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,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 "day3_common"; | ||
import library "io_utils"; | ||
|
||
fn Run() { | ||
var total: i32 = 0; | ||
var enabled: bool = true; | ||
while (PeekChar() != Core.EOF()) { | ||
if (PeekChar() == 0x6D) { | ||
// TODO: Use `if let` when available. | ||
let result: (bool, i32, i32) = ReadMul(); | ||
if (result.0 and enabled) { | ||
total += result.1 * result.2; | ||
} | ||
} else if (PeekChar() == 0x64) { | ||
// TODO: Use `if let` when available. | ||
let result: (bool, bool) = ReadDoOrDont(); | ||
if (result.0) { | ||
enabled = result.1; | ||
} | ||
} else { | ||
ReadChar(); | ||
} | ||
} | ||
Core.Print(total); | ||
} |
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