From 5ab4b7f78828d3640bfbd742a395de0fb678c4d1 Mon Sep 17 00:00:00 2001 From: Adam Perkowski Date: Mon, 16 Dec 2024 09:48:40 +0100 Subject: [PATCH] 01 part 1 done --- _2024/_01/01.jule | 47 ++++++++++++++++++++++++++++++++++++++++++-- _2024/_01/helper.hpp | 19 ++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 _2024/_01/helper.hpp diff --git a/_2024/_01/01.jule b/_2024/_01/01.jule index f2bba5e..d0f855f 100644 --- a/_2024/_01/01.jule +++ b/_2024/_01/01.jule @@ -2,8 +2,51 @@ // https://github.com/adamperkowski/aoc_jule // https://adventofcode.com/2024/day/1 -fn calculateMetrics(input: str) {} +use "std/math" +use "std/os" +use "std/slices" +use "std/strings" + +cpp use "helper.hpp" + +cpp fn readIntegers(line: str, do_right: bool): int + +fn calculateMetrics(input: []byte): str { + lines := strings::SplitAll(str(input), "\n") + + let mut list_left: []int + let mut list_right: []int + + for _, line_b in lines { + line := str(line_b) + list_left = append(list_left, cpp.readIntegers(line, false)) + list_right = append(list_right, cpp.readIntegers(line, true)) + } + + slices::Sort(list_left) + slices::Sort(list_right) + + let mut total: int = 0 + + for i, l in list_left { + total += int(math::Abs(f64(l - list_right[i]))) + } + + print("total: ") + println(total) + + ret "" +} fn Run() { - println("Hello, world!") + inputs := os::File.Read("_2024/_01/inputs.txt")! + println(calculateMetrics(inputs)) +} + +fn sum(values: ...int): int { + let mut total: int = 0 + for _, i in values { + total += i + } + ret total } \ No newline at end of file diff --git a/_2024/_01/helper.hpp b/_2024/_01/helper.hpp new file mode 100644 index 0000000..1a525d5 --- /dev/null +++ b/_2024/_01/helper.hpp @@ -0,0 +1,19 @@ +using namespace jule; + +#include +#include +#include + +int readIntegers(const std::string& line, bool do_right) { + std::istringstream iss(line); + int left, right; + if (iss >> left >> right) { + if (do_right) { + return right; + } else { + return left; + } + } else { + std::cerr << "Error reading line: " << line << std::endl; + } +}