Skip to content

Instantly share code, notes, and snippets.

@YannickFricke
Created September 29, 2025 20:00
Show Gist options
  • Select an option

  • Save YannickFricke/7ef4e9ebbb54e490c8ef09b7fccd24c2 to your computer and use it in GitHub Desktop.

Select an option

Save YannickFricke/7ef4e9ebbb54e490c8ef09b7fccd24c2 to your computer and use it in GitHub Desktop.
Yang first syntax idea
from "std/collections" import {List};
from "std/os" import {File};
export enum Result<SuccessType, ErrorType> {
Success(SuccessType),
Error(ErrorType),
}
export enum Maybe<T> {
Some(T),
None,
}
export function main(): uint8 {
const input_file_name = "./input.txt";
match File.read_file(input_file_name, encoding: "utf-8", as: string) {
Result::Success(file_contents) => {
const trimmed_file_contents = file_contents.trim();
const chars = trimmed_file_contents.chars();
const mapped_chars = List.map(chars, match {
'(' => +1,
')' => -1,
_ => 0
});
const last_floor = get_last_floor(mapped_chars);
Logger.info(`Santas last floor is: $last_floor`);
return 0;
},
Result::Error(reason) => {
Logger.error(`Could not read "$input_file_name": $reason`);
return 1;
}
}
}
function get_last_floor(levels) => List.sum(levels);
function get_basement_index(levels): Maybe<usize> =>
levels
|> List.reduce(Tuple(Maybe::None, 0), function(accumulator: Tuple<Maybe<usize>, int32>, entry: int32, index: usize): Tuple<Maybe<usize>, int32> {
match accumulator {
Tuple(Maybe::Some(_found_position), _current_level) => accumulator
Tuple(Maybe::None, current_level) => {
return match current_level + entry {
-1 => Tuple(Maybe::Some(index), -1),
new_level => Tuple(Maybe::None, new_level),
};
}
}
})
|> then(& &.0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment