From e0c144c73ddd167540cb35b3c83bfa30b3f49133 Mon Sep 17 00:00:00 2001 From: Araozu Date: Fri, 8 Nov 2024 22:59:33 -0500 Subject: [PATCH] feat: day 01 part 1 --- main.clj | 6 ++++-- src/day01.clj | 48 ++++++++++++++++++++++++++++++++++++++++++++---- src/utils.clj | 7 +++++++ 3 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 src/utils.clj diff --git a/main.clj b/main.clj index cef0dc9..d8cf285 100644 --- a/main.clj +++ b/main.clj @@ -1,11 +1,13 @@ (ns main "Advent Of Code 2023 solved in Clojure" (:require - [day01 :as day01])) + [day01])) (defn aoc-23 [] (printf "Advent of Code 2023, solved in Clojure.\n\n") - (day01/part1)) + (println (str "Day 01 part 1: " (day01/part1))) + (println (str "Day 01 part 2: " (day01/part2)))) (aoc-23) + diff --git a/src/day01.clj b/src/day01.clj index 34afb97..81afb91 100644 --- a/src/day01.clj +++ b/src/day01.clj @@ -1,9 +1,49 @@ (ns day01 - "Day 01 funtions") + "Day 01 funtions" + (:require + [utils] + [clojure.string])) +; a specific calibration value +; +; On each line, the calibration value can be found by combining the first +; digit and the last digit (in that order) to form a single two-digit number. +; +; For example: +; +; 1abc2 +; pqr3stu8vwx +; a1b2c3d4e5f +; treb7uchet +; +; In this example, the calibration values of these four lines are 12, 38, 15, and 77. Adding these together produces 142. +; +; Consider your entire calibration document. What is the sum of all of the calibration values + +; String +(def contents (utils/read-file "01" false)) + +; Vector of String +(def lines "Vector of String" (clojure.string/split-lines contents)) + +; Char -> Bool +(defn is-digit? [c] + (let [cn (int c)] + (and (>= cn 48) (<= cn 57)))) + +; String -> String +(defn calibration-value-1 [input] + (let [letters (sequence input) + digits (filter is-digit? letters)] + (str (first digits) (last digits)))) + +; () -> String (defn part1 [] - (println "here will be the solution to day 1 part 1")) - - + (let [numbers (map (fn [line] (Integer/parseInt (calibration-value-1 line))) lines) + sum (reduce + 0 numbers)] + (str sum))) + +(defn part2 [] + (println "here will be the solution to day 1 part 2")) diff --git a/src/utils.clj b/src/utils.clj new file mode 100644 index 0000000..636fc37 --- /dev/null +++ b/src/utils.clj @@ -0,0 +1,7 @@ +(ns utils) + +(defn read-file + "Reads a file for advent of code." + ([day] (slurp (str "./input/" day ".txt"))) + ([day test?] (slurp (str "./input" (if test? "_test/" "/") day ".txt")))) +