feat: begin day 3

This commit is contained in:
Fernando Araoz 2024-11-11 17:29:41 -05:00
parent 1c7d4d92be
commit 1bdd46ddd0
3 changed files with 103 additions and 1 deletions

10
input_test/03.txt Normal file
View File

@ -0,0 +1,10 @@
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..

View File

@ -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)

89
src/day03.clj Normal file
View File

@ -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"
[]
"")