This gist is just a placeholder for your solution of the Clojure Dojo from the Amsterdam Clojurians meetup.
To share your solution:
- fork this gist
- add the files containing your code to the forked gist
Thanks!
This gist is just a placeholder for your solution of the Clojure Dojo from the Amsterdam Clojurians meetup.
To share your solution:
Thanks!
(ns meetup.first | |
(:require [clojure.string :refer [split split-lines]])) | |
; input-first.txt looks like this: | |
; 5 10 | |
; 4 | |
; 1 8 | |
; 5 8 | |
; 7 10 | |
; 8 9 | |
; | |
; timeline | |
; ======================== | |
; 1 2 3 4 | 5 6 7 8 | | |
; | 5 6 7 8 | | |
; | 7 8 9 10 | | |
; | 8 9 | | |
(defn parse-line | |
[line] | |
(map read-string (split line #" "))) | |
(defn parse-input | |
[input] | |
(let [parsed-lines (map parse-line (split-lines input)) | |
[[start end] _ & humans-start-end] parsed-lines] | |
{:start start :end end :humans-start-end humans-start-end})) | |
(defn mask-ranges | |
[mask & ranges] | |
(let [mask-min (first mask) | |
mask-max (last mask)] | |
(map | |
(fn [r] (range (max (first r) mask-min) (min (last r) mask-max))) | |
ranges))) | |
(defn min-max | |
[numbers] | |
[(apply min numbers) (apply max numbers)]) | |
(defn min-max-human-in-time-range | |
[{:keys [start end humans-start-end]}] | |
(let [human-ranges (apply mask-ranges | |
[start (inc end)] | |
(map (fn [[start end]] [start (inc end)]) humans-start-end))] | |
(->> human-ranges | |
flatten | |
frequencies | |
vals | |
min-max))) | |
(println | |
(min-max-human-in-time-range | |
(parse-input (slurp "./resources/input-first.txt")))) |
class First | |
def initialize(input) | |
lines = input.split($/).map { |l| l.split(' ').map(&:to_i) } | |
start_end, _, *@human_ranges = lines | |
@start, @end = start_end | |
end | |
def solve | |
@human_ranges | |
.flat_map { |(arrive, leave)| (([@start, arrive].max)..([@end, leave].min)).to_a } | |
.each_with_object(Hash.new(0)) { |time_frame, obj| obj[time_frame] += 1 } | |
.values | |
.minmax | |
end | |
end | |
p First.new(File.read('./resources/input-first.txt')).solve |