-
-
Save vanadium23/a471ba6ead7a6eaf6903b58c99ef5453 to your computer and use it in GitHub Desktop.
This file contains 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 advent-of-code.day03 | |
(:require [clojure.string :as str] | |
[clojure.java.io :as io])) | |
(defn read-input [file] | |
(str/trim (slurp file))) | |
(defn find-correct-mule [text] ( | |
re-seq #"mul\(\d+,\d+\)" text | |
)) | |
(defn cleanup-mule [text] | |
(str/replace text #"mul\((\d+),(\d+)\)" "$1,$2") | |
) | |
(defn calculate-mule [mule] | |
(let [nums (map #(Integer/parseInt %) (str/split mule #","))] | |
(* (first nums) (second nums)) | |
)) | |
(defn part1 [input] (reduce + (map calculate-mule (map cleanup-mule (find-correct-mule input))))) | |
(defn find-command [text] ( | |
re-seq #"(do\(\)|don't\(\)|mul\(\d+,\d+\))" text | |
)) | |
;; acc - :state - active/inactive, :mule - current sum | |
(defn calculate-activated-mule [acc mule] | |
(println acc) | |
(println (first mule)) | |
(cond | |
(= (first mule) "do()" ) (assoc acc :state 1) | |
(= (first mule) "don't()" ) (assoc acc :state 0) | |
(and (= (:state acc) 1) (= (subs (first mule) 0 3) "mul")) (assoc acc :mule (+ (:mule acc) (calculate-mule (cleanup-mule (first mule))))) | |
:else acc) | |
) | |
(defn part2 [input] (:mule (reduce calculate-activated-mule {:state 1 :mule 0} (find-command input)))) | |
(def answer1example (part1 (read-input "03-example.txt"))) | |
(assert (= answer1example 161) answer1example) | |
(def answer1 (part1 (read-input "03.txt"))) | |
(assert (= answer1 183788984) answer1) | |
(def answer2example (part2 (read-input "03-example2.txt"))) | |
(assert (= answer2example 48) answer2example) | |
(def answer2 (part2 (read-input "03.txt"))) | |
(assert (= answer2 0) answer2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment