Adding my Clojure solution here - will write another post at some point. I'm oncall this week and got paged at 3am for something out of my control, so I figured "hey, I'm up, might as well." get-overlap solves part 1, while find-no-overlap solves part 2. Second part is a little brute-force, but still worked.
(nsaoc.aoc3(:require[clojure.string:ass][clojure.set:asset]))(defrecordClaim[claim-numbersquares])(defnconvert-to-grid"Converts a claim into a sequence of 'taken' squares."[claimgrid-width];; Named groups would be great here, but Clojure doesn't support this natively.(let[matcher#"#(?<claim>\d+)\s@\s(?<x>\d+),(?<y>\d+):\s(?<width>\d+)x(?<height>\d+)$"matches(re-findmatcherclaim)[_claimhorizvertwidthheight]matchesx(Integer.horiz)y(Integer.vert)w(Integer.width)h(Integer.height)rows(takeh(iterateincy))](->>(map#(range(+x(*grid-width%))(+(+xw)(*grid-width%)))rows)(flatten)(set)(Claim.(Integer.claim)))))(defnget-overlap"returns the amount of overlap based on calculated inputs.
Answer provided in square units matching the units entered
(for the case of the problem, square inches)."[claims];; Perform intersection to find any matches, then union to combine; repeat through the list.(loop[mapped-area(map#(convert-to-grid%1000)claims)shared-fabric#{}intersections#{}](if(empty?mapped-area)intersections(let[intersect(set/intersectionshared-fabric(:squares(firstmapped-area)))union(set/unionshared-fabric(:squares(firstmapped-area)))](recur(restmapped-area)union(set/unionintersectionsintersect))))))(defnoverlapping-claim[c1c2](cond(=(:claim-numberc1)(:claim-numberc2))nil(not-empty(set/intersection(:squaresc1)(:squaresc2)))c2))(defnfind-no-overlap"given a set of input claims, find the claim that has no overlap
with any other claims."[claims](let[grid-claims(map#(convert-to-grid%1000)claims)](loop[idx0ignores#{}](if-not(contains?(:claim-id(nthgrid-claimsidx))ignores)(if-let[overlap(some#(overlapping-claim(nthgrid-claimsidx)%)grid-claims)](recur(incidx)(conjignores(:claim-numberoverlap)))(:claim-number(nthgrid-claimsidx)))(recur(incidx)ignores)))))
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.
Adding my Clojure solution here - will write another post at some point. I'm oncall this week and got paged at 3am for something out of my control, so I figured "hey, I'm up, might as well."
get-overlap
solves part 1, whilefind-no-overlap
solves part 2. Second part is a little brute-force, but still worked.