finally finished question 5.
This commit is contained in:
1 parent
20203ea195
commit
1f1243d4df
2 files changed
+51
-1
No files matched your search
@@ -341,4 +341,6 @@
|
||||
@@..@@@@.@.@.@@@@@@@@.@@@@@...@@.@@@@@@@.@@@@@@@.@@@@@.@@@.....@.@@@@....@.@.@@@.@.@.@.@@@@@@....@.@@@...@@@@.@@@@...@@@@@@@.@@.@@@....@.@@
|
||||
@@@@@.@@.@...@@....@..@@@..@@.@@@...@.@@@@@.@.@@@@@.@@@@@@.@@@@@.@.@@@@..@.@@.@@@@@@.@.@@@@@@@@.@@...@.@@..@@..@.@.@@@.@@@@@@@@@@.@....@@.@
|
||||
@@@@.@.@..@.@..@.@.@@@@@.@@@.@@@@...@@@..@.@@.@...@.@@.@@.@@@@..@@@@@@@@@@@@.@@@@@.@.@@@@.@@@@.@.@.@....@@@@@.@.@.@@.@@@@@@@@@..@@@@@..@@.@
|
||||
@@.@@@@.@.@....@@@.@@@@.@..@@@@@@@.@@@@@@@....@@...@@@@@@...@@@.@@.@@@@.@@@@@@@@@@@.@.@@@@.@@@@@@@@@.@.@@@.@@@.@....@@.@.@..@@@@@@@@@.@.@@@")
|
||||
@@.@@@@.@.@....@@@.@@@@.@..@@@@@@@.@@@@@@@....@@...@@@@@@...@@@.@@.@@@@.@@@@@@@@@@@.@.@@@@.@@@@@@@@@.@.@@@.@@@.@....@@.@.@..@@@@@@@@@.@.@@@")
|
||||
|
||||
(def question-5 "213743501161200-213743501161200\n221359789392478-222960432116258\n553905700373062-561794342755365\n388351876706917-388578461376678\n263685821321516-270464997549766\n384957836075964-385057316204755\n86932924454422-88726115602910\n64713316693400-65189382496202\n68179610157787-68454649435444\n143154545648377-145980913110898\n390250299542523-390843409397977\n392162789636707-394167531415509\n467795551515047-469145316638624\n73412860995201-78319783604609\n174577844186025-180222947242739\n34889394659118-37852045203256\n506719917120116-506719917120116\n64999889315593-65189382496202\n62511033442122-62882231057991\n169629548448978-169629548448978\n195991726717561-200347594718591\n80800364897743-82410540424990\n157966143122749-158625789879543\n414893276002289-420734433214551\n468854880571347-470178150823398\n293047529057561-293047529057561\n526293793744803-526800435871324\n154610263545675-155018832807006\n85676500485010-87494001551296\n404052468013478-411059540873663\n529805857574316-530354749652142\n282126028703430-287392631951184\n536612716581558-542001885354658\n224389325429033-225956850054114\n389327354686290-389941649666180\n226458176996215-227998751964573\n466681105242973-468035461798180\n132182495488136-136864510598769\n158625789879543-158943957000511\n222498723695457-223722626496659\n24396986945805-29009989981082\n387070084421471-387699738663307\n463761550427134-465101663712718\n244484473250009-249824444617715\n235682381324440-239428507922589\n356542431952285-357218504633271\n528809160802420-529163494010802\n203275761047276-207893046967514\n65189382496202-65740304733466\n84564501332038-86342507935742\n528809160802420-529739126171581\n430824287436458-430824287436458\n270464997549767-270464997549767\n73412860995201-73412860995201\n203275761047276-207893046967514\n465765531974637-467339424840641\n88308683283200-90314897336701\n506719917120116-509378158464998\n154610263545675-155018832807006\n436351058276905-441549428537929\n390250299542523-390501802123222\n464766089052130-465995972804612\n153723748856595-154247928476913\n524957458539564-525444885761624\n193355047594087-195991726717559\n68057027039766-68454649435444\n114292754379846-114292754379846\n476176387291714-479287347759176\n68727093315072-69054421292865\n374010991378735-378095042001312\n396702787271742-398555495412596\n92985559435331-99083983384255\n524317721420090-524645436296520\n470916081729043-472406223184007\n307193970359996-309957481846034\n83208072709351-85336482970086\n61908006035935-62767933777530\n12440214380075-19413829756369\n158268094987535-158625789879543\n323855611951188-327225842492665\n232121351950005-235682381324439\n104580961113313-109652180451852\n155719620755384-156378640409574\n225242642256272-226838958939168\n126345620041269-129628037616258\n272979306019768-272979306019768\n229571449179868-230955409924473\n15572428843108-19413829756369\n486438625661094-489174884685392\n399063478386186-400626043814074\n61434917885505-61908006035935\n442807002499434-447467590937981\n153470274802250-153891356149723\n548528649335253-552132613556960\n302406550101741-307193970359994\n284836416410676-290905691766532\n134736737710827-138821293926430\n524645436296520-525444885761624\n347299925469182-350731461622425\n333121507005172-333121507005172\n257633973479609-259311991697339\n418350619486212-420734433214551\n523873015780814-524032209716334\n424331863324814-430824287436457\n143154545648377-148152467497972\n156770859353106-157228370844559\n523401105399726-524032209716334\n3672218268426-9069958349825\n41190120499524-48710502110828\n171431709160547-176780839877720\n65740304733466-66283299959280\n3672218268426-9069958349825\n157444284967581-157743465985657\n99083983384256-99083983384256\n114292754379847-118122243442587\n32783805912506-34889394659117\n495774309849622-495774309849622\n385597213338484-386130309524989\n66422129062863-66711994098586\n390501802123222-390843409397977\n227420995218497-229137292369316\n518765954978807-520809013033287\n81872566000969-83813851796454\n272979306019768-280055074059068\n394469512488834-396279958092492\n469546011173175-471346897231698\n442807002499434-451270889383530\n184738598610317-187210601572675\n364359970290004-369109081626630\n327225842492665-327225842492665\n353087698181504-361406479744402\n293047529057561-299710639297794\n486438625661094-489174884685392\n472788183962691-480272823246663\n152000130191943-152486238796869\n386858817856261-387699738663307\n462503261552047-464039809812541\n183175626133351-189172211000972\n163807315460788-169629548448978\n393534536834350-394918686174827\n344389995827602-347299925469181\n159774306485562-160419960771595\n544514849402253-552132613556960\n65400483278322-66283299959280\n63641646788533-63945948546021\n228387323493940-230123402570745\n223334527687062-225118219054282\n398001413033472-399528430973758\n389120143291068-389327354686290\n251886663550811-259311991697339\n69054421292865-69216943568303\n109652180451852-109652180451852\n53011962076696-56344039055008\n40016Line truncated
|
||||
@@ -0,0 +1,48 @@
|
||||
(ns question-5
|
||||
(:require [clojure.string :as strings]
|
||||
[inputs]))
|
||||
|
||||
(def mock-input "3-5\n10-14\n16-20\n12-18\n\n1\n5\n8\n11\n17\n32")
|
||||
|
||||
(defn parse-filter [s]
|
||||
(let [[start end] (map Long/parseLong (strings/split s #"-"))]
|
||||
#(<= start % end)))
|
||||
|
||||
(defn parse-input-a [document]
|
||||
(let [[filter-part ids-part] (strings/split document #"\n\n")
|
||||
ids (map Long/parseLong (strings/split-lines ids-part))
|
||||
filters (mapv parse-filter (strings/split-lines filter-part))]
|
||||
[filters ids]))
|
||||
|
||||
(defn parse-input-b [document]
|
||||
(let [[filter-part & _] (strings/split document #"\n\n")
|
||||
filter-strings (map #(strings/split % #"-") (strings/split-lines filter-part))
|
||||
ret (mapv #(mapv Long/parseLong %) filter-strings)]
|
||||
(mapv (fn [[a b]] [a (inc b)]) ret)))
|
||||
|
||||
(defn question-5-a [document]
|
||||
(let [[filters ids] (parse-input-a document)
|
||||
any-filter? (fn [n] (some #(% n) filters))]
|
||||
(count (filter any-filter? ids))))
|
||||
|
||||
(defn is-part-of [[ss se] [bs be]]
|
||||
(and (>= ss bs) (< ss be)
|
||||
(>= se bs) (< se be)))
|
||||
|
||||
(defn add-range [ranges [s e]]
|
||||
(let [starts-in (filter #(<= (first %) s (second %)) ranges)
|
||||
ends-in (filter #(<= (first %) e (second %)) ranges)
|
||||
new-start (apply max (conj (mapv #(second %) starts-in) s))
|
||||
new-end (apply min (conj (mapv first ends-in) e))]
|
||||
(if (< new-start new-end)
|
||||
(conj (remove #(is-part-of % [new-start new-end]) ranges) [new-start new-end])
|
||||
ranges)))
|
||||
|
||||
(defn question-5-b [document]
|
||||
(let [ranges (parse-input-b document)
|
||||
distinct-ranges (reduce add-range [] ranges)]
|
||||
(reduce + (map (fn [[s e]] (- e s)) distinct-ranges))))
|
||||
|
||||
(defn -main [& args]
|
||||
(println "The solution to problem 5-a is " (question-5-a inputs/question-5))
|
||||
(println "The solution to problem 5-a is " (question-5-b inputs/question-5)))
|
||||
Reference in new issue
Block a user