From 9416c81c74366a84075f276127750fa6369b36df Mon Sep 17 00:00:00 2001 From: codingzorro <48889997+codingzorro@users.noreply.github.com> Date: Sun, 14 Jan 2024 21:14:49 +0100 Subject: [PATCH] prepare to loop --- doc/day01.md | 5 +++++ src/advent2023/day01.clj | 2 -- test/advent2023/day01_test.clj | 16 +++++++++++----- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/doc/day01.md b/doc/day01.md index da0a689..0b3d613 100644 --- a/doc/day01.md +++ b/doc/day01.md @@ -16,6 +16,9 @@ The calibration is a two-digit number "encrypted" in each line: * extract all digits contained in the line: 1, 3 and 5 in this case * take the first and the last one to get the calibration: 15 in this case +**Note:** We will start by implementing the calibration and only after that we +will implement the "main loop" to accumulate the sum of calibrations. + ### Step 1: Write the tests It is clear that we need at least two functions: * `(decode "a1b2c3d4e5f")` shall return `[1 3 5]` @@ -28,3 +31,5 @@ Note that they fail; i.e, they are "red" according to the TDD terminology. ### Step 2: Go from "green" to "red" Tag [morning-01-green](https://github.com/codingzorro/advent2023/releases/tag/morning-01-green) implements the functions needed to pass the existing tests + +### diff --git a/src/advent2023/day01.clj b/src/advent2023/day01.clj index 96f87ca..62ff793 100644 --- a/src/advent2023/day01.clj +++ b/src/advent2023/day01.clj @@ -5,8 +5,6 @@ [[a b]] (+ (* a 10) b)) - - (defn decode "extract digits in a string; e.g. \"a1b2\" --> [1 2]" [a-string] diff --git a/test/advent2023/day01_test.clj b/test/advent2023/day01_test.clj index 736512b..e473030 100644 --- a/test/advent2023/day01_test.clj +++ b/test/advent2023/day01_test.clj @@ -2,13 +2,19 @@ (:require [clojure.test :refer :all] [advent2023.day01 :refer :all])) + +(def sample-data + ["1abc2" + "pqr3stu8vwx" + "a1b2c3d4e5f" + "treb7uchet"]) + (deftest decode-test (testing "Extract the digits in a given string" - (is (= (decode "1abc2") [1 2])) - (is (= (decode "pqr3stu8vwx") [3 8])) - (is (= (decode "a1b2c3d4e5f") [1 2 3 4 5])) - (is (= (decode "treb7uchet") [7])) - )) + (is (= (decode (sample-data 0)) [1 2])) + (is (= (decode (sample-data 1)) [3 8])) + (is (= (decode (sample-data 2)) [1 2 3 4 5])) + (is (= (decode (sample-data 3)) [7])))) (deftest to-int-test (testing "Extract the digits in a given string"