-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env bb | ||
|
||
(ns y2024.d02 | ||
(:require [babashka.cli :as cli] | ||
[clojure.string :as str])) | ||
|
||
|
||
(def cli-options {:input {:default "../../inputs/2024/02.input" :type :string}}) | ||
|
||
(def opts (cli/parse-opts *command-line-args* {:spec cli-options})) | ||
|
||
(def input | ||
(->> (slurp (get opts :input)) | ||
(str/split-lines) | ||
(map #(str/split % #" ")) | ||
(map #(map parse-long %)) | ||
(map #(into [] %)))) | ||
|
||
(defn one-way-safe? [xs] | ||
(let [pairs (partition 2 1 xs)] | ||
(every? (fn [[lhs rhs]] | ||
(< lhs rhs (+ lhs 4))) | ||
pairs))) | ||
|
||
(defn safe? [xs] | ||
(or (one-way-safe? xs) | ||
(one-way-safe? (reverse xs)))) | ||
|
||
(defn part1 [data] | ||
(count (filter safe? data))) | ||
|
||
(defn lenient-safe? [xs] | ||
(or (safe? xs) | ||
(let [n (count xs)] | ||
(->> (range n) | ||
(map #(into [] (concat (take % xs) (drop (inc %) xs)))) | ||
(some safe?))))) | ||
|
||
(defn part2 [data] | ||
(count (filter lenient-safe? data))) | ||
|
||
(prn {:part1 (part1 input) | ||
:part2 (part2 input)}) |