Created
December 1, 2023 17:34
-
-
Save maleghast/9bc7cd4851d0563de9686394b1debe37 to your computer and use it in GitHub Desktop.
AoC 2023 - Day 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns maleghast.aoc2023.day1 | |
(:require [clojure.string :as str] | |
[clojure.java.io :as io]) | |
(:gen-class)) | |
| |
(defn first-and-last | |
[vec-of-nums-as-strings] | |
(str (first vec-of-nums-as-strings) (last vec-of-nums-as-strings))) | |
| |
(defn numbers-as-text-to-digits | |
"Convert text expressions of numbers 1-9 into string digits" | |
[input-string] | |
(str/replace | |
input-string | |
#"oneight|threeight|fiveight|nineight|twone|eightwo|eighthree|sevenine|one|two|three|four|five|six|seven|eight|nine" | |
{"oneight" "18" | |
"threeeight" "38" | |
"fiveight" "58" | |
"nineight" "98" | |
"twone" "21" | |
"eightwo" "82" | |
"eighthree" "83" | |
"sevenine" "79" | |
"one" "1" | |
"two" "2" | |
"three" "3" | |
"four" "4" | |
"five" "5" | |
"six" "6" | |
"seven" "7" | |
"eight" "8" | |
"nine" "9"})) | |
| |
(defn get-coordinates-from-input-line | |
"Get a vector of individual ints from a string of mixed letters and numbers" | |
[input-string] | |
(->> input-string | |
(filter #(Character/isDigit %)) | |
(apply str) | |
(#(str/split % #"")) | |
(first-and-last) | |
(Integer/parseInt))) | |
| |
(defn get-coordinates-from-input-line-improved | |
"Get a vector of individual ints from a string of mixed letters and numbers" | |
[input-string] | |
(->> input-string | |
(numbers-as-text-to-digits) | |
(filter #(Character/isDigit %)) | |
(apply str) | |
(#(str/split % #"")) | |
(first-and-last) | |
(Integer/parseInt))) | |
| |
(defn puzzle-1 | |
"Callable entry point to the application." | |
[data-file] | |
(let [datastring (slurp (io/resource data-file)) | |
prepared-data (str/split-lines datastring)] | |
(->> prepared-data | |
(map get-coordinates-from-input-line) | |
(reduce +)) | |
) | |
) | |
| |
(defn puzzle-2 | |
"Callable entry point to the application." | |
[data-file] | |
(let [datastring (slurp (io/resource data-file)) | |
prepared-data (str/split-lines datastring)] | |
(->> prepared-data | |
(map get-coordinates-from-input-line-improved) | |
(reduce +)) | |
) | |
) | |
| |
(defn -main | |
[& args] | |
(let [soln1 (puzzle-1 "input.txt") | |
soln2 (puzzle-2 "input.txt")] | |
(println soln1) | |
(println soln2))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment