-
-
Save vanadium23/d32cde3528bbe11cd5ae999365b21fab 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.utils | |
(:require [clojure.string :as str] | |
[clojure.java.io :as io])) | |
(defn read-input [file] | |
(str/trim (slurp file))) | |
(defn to-lines | |
"Turn a blob or block into a seq of lines" | |
[input] | |
(str/split-lines input)) | |
(defn parse-line | |
"Parse a line into a seq of strings" | |
[line] | |
(map #(Integer/parseInt %) (str/split line #"\s+"))) | |
(defn insert-heap [heap value] | |
(let [new-heap (conj heap value)] | |
(loop [h new-heap idx (dec (count new-heap))] | |
(let [parent (quot (dec idx) 2)] | |
(if (and (>= idx 1) (< (new-heap idx) (new-heap parent))) | |
(recur (assoc h idx (new-heap parent) parent (new-heap idx)) parent) | |
h))))) | |
(defn sorted-insert [v x] | |
(let [pos (count (take-while #(<= % x) v))] | |
(vec (concat (subvec v 0 pos) [x] (subvec v pos))))) | |
(defn part1 [input] ((do | |
(def left_heap []) | |
(def right_heap []) | |
(let [num_pairs (map parse-line (to-lines input))] | |
(println (count num_pairs)) | |
(doseq [pair num_pairs] | |
(let [left (first pair) | |
right (second pair)] | |
(println left right) | |
(def left_heap (sorted-insert left_heap left)) | |
(def right_heap (sorted-insert right_heap right)))) | |
(println left_heap) | |
(println right_heap)) | |
(def total_distance 0) | |
(let [cnt (count left_heap)] | |
(loop [left left_heap right right_heap idx 0] | |
(if (= idx cnt) | |
(println "done") | |
(let [left_val (nth left idx) | |
right_val (nth right idx)] | |
(println left_val right_val) | |
(def total_distance (+ total_distance (abs (- right_val left_val)))) | |
(recur left right (inc idx)))))) | |
(println total_distance) | |
(fn [] total_distance)))) | |
(defn counter-insert [hashmap value] (assoc hashmap value (inc (get hashmap value 0)))) | |
(defn part2 [input] ((do | |
(def left_heap []) | |
(def counter {}) | |
(let [num_pairs (map parse-line (to-lines input))] | |
(println (count num_pairs)) | |
(doseq [pair num_pairs] | |
(let [left (first pair) | |
right (second pair)] | |
(println left right) | |
(def left_heap (sorted-insert left_heap left)) | |
(def counter (counter-insert counter right)))) | |
(println left_heap) | |
(println counter)) | |
(def total_distance 0) | |
(let [cnt (count left_heap)] | |
(loop [left left_heap idx 0] | |
(if (= idx cnt) | |
(println "done") | |
(let [left_val (nth left idx) | |
right_val (get counter left_val 0)] | |
(println left_val right_val) | |
(def total_distance (+ total_distance (* right_val left_val))) | |
(recur left (inc idx)))))) | |
(println total_distance) | |
(fn [] total_distance)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment