From 1bdd46ddd0519335212020d7cc0ec77c5256730e Mon Sep 17 00:00:00 2001 From: Fernando Araoz Date: Mon, 11 Nov 2024 17:29:41 -0500 Subject: [PATCH] feat: begin day 3 --- input_test/03.txt | 10 ++++++ main.clj | 5 ++- src/day03.clj | 89 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 input_test/03.txt create mode 100644 src/day03.clj diff --git a/input_test/03.txt b/input_test/03.txt new file mode 100644 index 0000000..b20187f --- /dev/null +++ b/input_test/03.txt @@ -0,0 +1,10 @@ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. diff --git a/main.clj b/main.clj index fbfcd6a..4b879a5 100644 --- a/main.clj +++ b/main.clj @@ -2,7 +2,8 @@ "Advent Of Code 2023 solved in Clojure" (:require [day01] - [day02]) + [day02] + [day03]) (:import [java.time Instant Duration])) @@ -20,6 +21,8 @@ (benchmark day01/part2 "01" "2") (benchmark day02/part1 "02" "1") (benchmark day02/part2 "02" "2") + (benchmark day03/part1 "03" "1") + (benchmark day03/part2 "03" "2") (println "\nthe end.")) (aoc-23) diff --git a/src/day03.clj b/src/day03.clj new file mode 100644 index 0000000..e302c8c --- /dev/null +++ b/src/day03.clj @@ -0,0 +1,89 @@ +(ns day03 + "Day 02 funtions" + (:require + [utils] + [clojure.string :as str])) + +(def contents ":: String" (utils/read-file "03" true)) +(def lines ":: Vec of String" (str/split-lines contents)) + +(defn is-digit? + ":: Char -> Bool" + [c] + (if (nil? c) + false + (let [cn (int c)] + (and (>= cn 48) (<= cn 57))))) + +; :start is the start column number, :end is the end. 0-based +; Element :: {:value String :start Int :end Int} +; +; Index :: {Int Vec of Element} where Int is the row number + +(defn process + ":: ?String, Int, Vec of Element, Vec of Char, Int -> Vec of Element + :: Vec of Char -> Vec of Element + " + ([acc-str acc-pos acc-vector next-list pos] + (if (empty? next-list) + ; base case, return the vector of elements + ; TODO: append to vec if there is a last acc present + acc-vector + ; recurse + (let [c (first next-list) + c-is-digit (is-digit? c) + acc-exists (not (nil? acc-str)) + next-pos (inc pos) + next-list (rest next-list)] + ; + (cond + ; append to accumulator and continue + (and c-is-digit acc-exists) (process (str acc-str c) + acc-pos + acc-vector + next-list + next-pos) + ; create new accumulator string and pos + (and c-is-digit (not acc-exists)) (process (str c) + pos + acc-vector + next-list + next-pos) + ; add the current acc to the vec, reset acc + (and (not c-is-digit) acc-exists) (process nil + -1 + (conj acc-vector {:value acc-str :start acc-pos :end pos}) + next-list + next-pos) + ; continue + (and (not c-is-digit) (not acc-exists)) (process acc-str acc-pos acc-vector next-list next-pos))))) + + ([next-list] (process nil -1 [] next-list 0))) + +(defn parse-elements + ":: String -> Vec of Element" + [line] + + "") + +(defn parse-lines + " + :: Vec of String -> {:numbers Vec of Element :symbols Vec of element} + + Creates 2 maps from the input: A map of numbers and a map of symbols. + Each map stores a row number and elements. Each element stores its + column numbers. + " + [] + "") + +(defn part1 + ":: () -> String" + [] + "") + +(defn part2 + ":: () -> String" + [] + "") +