Back again with more Clojure. Cool thing to point out on this one: the frequencies function. It takes as input a collection, and then returns a map of the values present in that collection, and the count of the times they appear. Made part two real easy.
(nsaoc2019.day4(:require[clojure.string:asst]))(defnsatisfies-criteria?"checks for increasing values, with a flag for
at least one double."[num](let[digits(st/split(strnum)#"")pairs(->>digits(map#(Integer/parseInt%))(partition21))](and(some(fn[[ab]](=ab))pairs)(every?(fn[[ab]](>=ba))pairs))))(defnp2019-04-part1[input](let[[low-boundhigh-bound](map#(Integer/parseInt%)(st/splitinput#"-"))all-values(rangelow-boundhigh-bound)](->>all-values(filtersatisfies-criteria?)(count))))(defnexplicit-double?[num](let[digits(map#(Integer/parseInt%)(st/split(strnum)#""))](some(fn[[kv]](=v2))(frequenciesdigits))))(defnp2019-04-part2[input](let[[low-boundhigh-bound](map#(Integer/parseInt%)(st/splitinput#"-"))all-values(rangelow-boundhigh-bound)](->>all-values(filtersatisfies-criteria?)(filterexplicit-double?)(count))))(defnrun"Runs the Day 4 solutions."[](let[input"108457-562041"](println(str"Part 1: "(p2019-04-part1input)))(println(str"Part 2: "(p2019-04-part2input)))))
I'd agree that it's not strictly necessary, as (group-by identity "112233") would solve the same problem; however, if you give me a convenience function, I'll take it. :D
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Back again with more Clojure. Cool thing to point out on this one: the
frequencies
function. It takes as input a collection, and then returns a map of the values present in that collection, and the count of the times they appear. Made part two real easy.repl.it: repl.it/@ballpointcarrot/AoC-Clojure
github: github.com/ballpointcarrot/AoC/blo...
I had to write my own in kotlin! Their design philosophy states that GroupBy->MapValues(Count) is good enough, and I... kind of agree?
Still handy to do it in one step though.
I'd agree that it's not strictly necessary, as
(group-by identity "112233")
would solve the same problem; however, if you give me a convenience function, I'll take it. :D