feat: day 01 part 1
This commit is contained in:
parent
239b7ff622
commit
e0c144c73d
6
main.clj
6
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)
|
||||
|
||||
|
||||
|
@ -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"))
|
||||
|
||||
|
||||
|
7
src/utils.clj
Normal file
7
src/utils.clj
Normal file
@ -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"))))
|
||||
|
Loading…
Reference in New Issue
Block a user