day 3
This commit is contained in:
parent
8d6c8aa40d
commit
b25ace69c5
140
inputs/day03
Normal file
140
inputs/day03
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
...........441.................367................296........................................567..47.....45.................947.............
|
||||||
|
...606..........888.....................508..........*892................+..=138.381..967...............*....%......926...........218.......
|
||||||
|
....*......116..*..............747............-....................777..460..........*.......549......127...595.......*..290........*.968...
|
||||||
|
..902..........425................+..........730..........#...........*.....196.............-.......................512.*....@...994..%.....
|
||||||
|
..........................924.....................%...*...139..............*............/......458......................10..155.............
|
||||||
|
...318.......825.......+.....*201................793.143.......522.%...568.....&.........558....*.......583....792..........................
|
||||||
|
....*................971............448....653..............79..@..792....*.105....115.........200.....+.........*.............*........380.
|
||||||
|
.850....269................775*........*......@.......%................921........#........................812....793....22.460.305.........
|
||||||
|
..........&..414@...824........129.....816......*..560...421......955/................141......336........*.................................
|
||||||
|
.........................59....................201......$...............@651.........*...........+..937...732...549-....284....544..........
|
||||||
|
...........................*..178.125*808....................919..............246....400.53......................................$.+........
|
||||||
|
.....=.....964.238..116.722.....*............624..............*...........209...............312....670.....953.....#....265.710.....414.....
|
||||||
|
...966.152*.........*........375....*........*.........663....639............................&....*.........*.....424......*....%...........
|
||||||
|
..................-..68.206......812.601......874..651...+..........857*686.........*.....33......825......497...............402............
|
||||||
|
862*766......@...545.......*4............706.....................................887.395....=.731......47.........................942.949...
|
||||||
|
..........900..........390........5-........*345.....................724.......%..........................+........778..761........%........
|
||||||
|
..............887..985...*.......................141...................$.......64......&...334.278..896...572.....*.....*.....804...........
|
||||||
|
......184....*..........596...$.....................*.-764....494....$....54............35....*................900...750..$.....*...........
|
||||||
|
.....#.....322......979.....447............542.61..52.................880..........................*718............-.......926..659.........
|
||||||
|
.......91.......793....*.@.............215*............@.........585......+....126..............102.......207..&....762.............&703....
|
||||||
|
.....................751..87..$........................117.......*.......778...*.....363...=157......351.....*..577.........................
|
||||||
|
..483=...374*823.............512...835.551.....333..........978........#.......954.....*.............*.....140..........634.....357......473
|
||||||
|
......................................*.........#...223.....*.........79...............275..........24..........310............+....390=....
|
||||||
|
755.255.#....751....#...342....51..........507.....*.....764....220..........909../.............................../.........................
|
||||||
|
.....&...320..=..293.....*.....@......204.....*.956.............*......485....#...866..765..973..668......476.............772.....463.479...
|
||||||
|
..........................53.............*.............+410...667.-103..&..%...................*....*......*..995#.........-..75.......*....
|
||||||
|
.......289.......995..............33...714.......366........................556.......518......95.....................836.....*......699....
|
||||||
|
......./............/...31.........................&...............&....................*...........220.814............$....658.............
|
||||||
|
.....=...10...............*.....+852.....%98...........838......652..........11.125..488......799...*.....*.................................
|
||||||
|
...470......102......889.250..................209..118..#...........729........*..................159..799..417.140....762......588.629.....
|
||||||
|
........@...*...844....*........900...507....*......#...............=......*...........878.....................*......*.....539*............
|
||||||
|
...756.769.426..*......94.........#......*....997...........#793........115.685..........*.....973.869.................12............596....
|
||||||
|
.....=..........2......................698........416...$.........302...............530...315...@...@.......-.....605.........542.....*.....
|
||||||
|
.711....373........................358...............&..820..........*......#243...=........................148...../...$........*...581....
|
||||||
|
...........$...727.......314...57..*.............*..................762......................524..346$...................581.....120........
|
||||||
|
......=568..........@844.&.....*...257.485......593.....*................................30..*.........740.....709*.........................
|
||||||
|
................................75......*............194.675.420*282.....967..552.............850..................789..=.....-........223..
|
||||||
|
..910.....$.............562..=..........56..............................-....*......194..................................425...789.....*....
|
||||||
|
...-......618.716#...........233.............................828...........301......................190....#120....................360.232..
|
||||||
|
.......................587..........766.812............178...-........929...................../.......*...........707.949....465..$.........
|
||||||
|
..........191...=....................-...................................*378............766..456......605...............*....*........593..
|
||||||
|
.481...#.%......267.....729.................619......116*164....................505....+....*.....531...................498...47...789*.....
|
||||||
|
.....625............579*.......914.997=.....*...............................456*....632..133......*......751...%..631.......................
|
||||||
|
..........=415.481.........979.............318.......=.............942.855...................512...985..*....865./..........................
|
||||||
|
.................*...........@..................436-..542.........*.....*..$631........108.....%........619........194..............825..329
|
||||||
|
..333.............624......................=.................795-..818.431.......702...*..............................=........+....%.......
|
||||||
|
.../......................312.....666.......785.198......719....................&.......837........838..551.175..........368....557.........
|
||||||
|
.....581.........986......*........+................268.....*...445............................*..*.......-...*.......99*...............=...
|
||||||
|
.................@....793..............868......935....*..415.....*.430....253....@...........678..............420............%.46#..679....
|
||||||
|
........................*....70*239......*............824.......499....*...*...965...696..532............405.........90....306..............
|
||||||
|
.........................34...........152...216$.381.................373..22......../........*.............&...........-.........976........
|
||||||
|
.................132.........448....................$.............*..........585.............321...............+...66....741.......*........
|
||||||
|
.......194........&..669.......*................*..............460.465.54.......+........763.......738......444..........*....791..776......
|
||||||
|
...978.....839......*........808...669.......864.30...................../...........439..*......*.....*...........441.....278...=...........
|
||||||
|
.......971*.....*.....................*......................................43....*....778..329.711.136....................................
|
||||||
|
............792.182..21............81..545...398&...........................*.....763....................181............*506....736..141.933
|
||||||
|
........282*..........*......=............................41*982......237..166.............678..718=.656..*...........87.........*..........
|
||||||
|
...................684........5......220*697.......259............347....*.............*.....*.........*.832.............388.....192........
|
||||||
|
..395........=............838......................+.....693....#.....784...469*334.....870.464......572........875.....*...............152.
|
||||||
|
......362.....693.........+...859......183..479...........*......969.......................................811..&....171.........284...=....
|
||||||
|
.....*.............../...................*.........606..187..........644..........401.=984...98....760....*..............54.................
|
||||||
|
...505.............806....249..........327....821.....@.................*....211..*.........*........*.422.................*......*902.863..
|
||||||
|
............................*...................*..........219..........571....*...284...272......477.........938.....312...981..7..........
|
||||||
|
.688...=....................544...766...........346..........................923.............................*........+............923......
|
||||||
|
......779..=281...419..............+.................308....51...................836.........537../....931....97.........................952
|
||||||
|
..................*......312...834.....................*........../.............*....83.262....*...748...............873.....46.4...........
|
||||||
|
......355.......728.........*....%....................56..........752..296-....386.....*......53...........................-...*........@...
|
||||||
|
....-.................483/..192.........714*248.................-.........................621.........*............926.....244.........867..
|
||||||
|
...878...........#641...........................832.72.........877.......=..793............@........676.696.........*.......................
|
||||||
|
.............853.............&614.......#699......*.......*29..........900.....*718..%369....#.$919........$....*....791.381......624.......
|
||||||
|
................*255..........................758.604...........985.......................811................955.745.......*................
|
||||||
|
...........9............276.393.76............$.............952......................947............385...*.............620....875..........
|
||||||
|
...........*..755...210...#..*..*................/............/.........774..=............315..........$.750...........................@.381
|
||||||
|
........975..*...../........168..472.....662.....308..............996........685..................77.........424..188...............639.....
|
||||||
|
..............173.........#..............*...........740.............*372...........................*...............*.......................
|
||||||
|
........................991.........*769..651.36.+12...*......861&............897.....749..859....271...96..........248.667.................
|
||||||
|
.............%..............518..624...........*.......475.........183*.......%...............*...........-.....658.......*..+..............
|
||||||
|
.29..........25...688..346.....*.........720.29....162.................502....................414...616........*........52..698..........453
|
||||||
|
........286%.........-....*....359.........*......*........................881.........634..........+....=.....932..519...........765.......
|
||||||
|
.................&........280....................282..209...519...........................*............970...........*.....@.....&.....*....
|
||||||
|
.....448...216..204.190........108*868...*408......../.......*..=............824.........344..................29.....254..139.......811.656.
|
||||||
|
.310..................*................................$..449....159...731.....*...154................107.......*279........................
|
||||||
|
...*...194...........307.817.........................314....................208.....*................*.......+.......................871*...
|
||||||
|
254...@............$.......@..#17..............284.........757......................921..#479..933.414....308......@154............*.....222
|
||||||
|
..................817.................555.389....*............@.151...........................&................143..............348.448.....
|
||||||
|
....20......321..........303............*.....873.....%..899...................226*.....*830.....................=........@.................
|
||||||
|
....@....*...*......698...*.............363........189......*...192.&................890...............@814...............830......300..48..
|
||||||
|
.......907..207.......*...284.......24+....................346.+....867.....808..43......397....363................254.........178......*...
|
||||||
|
.................$................$............772...............+..........-...........*.......-...............#...%..................246..
|
||||||
|
......332....855.250..........*....692.....968...............212..297...........223.....725.............419.....213...316.822...............
|
||||||
|
..290....&..*.........108.....577....................133.87...=..............$...+.............&.........................*..............917.
|
||||||
|
.......@.....701.......*..476....................806../..*.........216.5+.834......174..........597...............617......222..............
|
||||||
|
...-...618.........992.47...*............497.............428.....@.*..............*.........276........521.../.......*......*........234....
|
||||||
|
.465...............%........438.%627.727......................949...258.....927....365........*.....%..@...93.......186..535..........-.....
|
||||||
|
...........557..........................*66......456.....+........*........+............539..95..790............220..........$...........95.
|
||||||
|
117..........*.......869...........................@..943..133.249.364..............806*................=...342...*.......817....667........
|
||||||
|
.............490.641*...........273.786..507.................@...............244......................71....*......717...........*.......871
|
||||||
|
...236.70...............$.........*.*....%....................................*..386=...503..................778.=.........419...7..........
|
||||||
|
.......*....$905..660...287....654..634........934.............344..........322............*............702......745..........*.............
|
||||||
|
........950.........*............................#..906..118..*.....553.694.....191.....682.........412*.........................801........
|
||||||
|
.............186....175...607................330....@.......*..931.*....*......@...............618............14........851&.567*...../.....
|
||||||
|
............/...........+........156.449.334*....*....888..951.....559...144.........%.................................................16...
|
||||||
|
....835..................96..751..*...........409.365....*...........................736........................310$......583...............
|
||||||
|
......$........47...744........*.68.........*..........634..-..589........250..370...........644.....693...13*.............*...&............
|
||||||
|
..470.....775...=..+.....&..234..........538.597...$.......391...........................179*.................524.......232..79.............
|
||||||
|
....*.....=.............72..........................493..............=...............$.............................828...........583.*......
|
||||||
|
..207...........312..........867..806......224..123...............616......76.......777./200..232..149.48...716..@...=..........&.....7.....
|
||||||
|
......48.................277..........755=....*..........735.................*.................@....-...*..*....590..........+......-.......
|
||||||
|
........*787.....82........*.....208...........359......................766.11.............%..........31...60..............731...928........
|
||||||
|
...................@.......265..........333.........................781.@...................991...#............93...........................
|
||||||
|
.........................................+...........................*..........................848....691.97...*..........539..............
|
||||||
|
.507..238.........384......#.......&.....................987......@..395.844*......949*608.460......29...*.*.....955........*.........#734..
|
||||||
|
........*./910..........607.........411.................*........102........................=...169...*....74........846..382.533/..........
|
||||||
|
.....221........................742.......%...267.491.357...544......+...846.973....................66........440...........................
|
||||||
|
..............-................/....833.103...$....*..............369........*.......519.....4.........-........*................809.942....
|
||||||
|
.833..48..523..281.................&...................................442.958......*....951..-.........856.=..............49...&...../.....
|
||||||
|
.......*.....*.......275.......259.....$.........-.175...662.697.307................169....*................33.....955.....*......+......68.
|
||||||
|
...388.......231.994....@.........*.934........889.*.........*.............................668....*878......................353.560.....*...
|
||||||
|
....@................%.........806..................708...221.......................498.....................79..385...................112...
|
||||||
|
451.....555&........463.........................%.........................80*494...$.........976..487.......*.....=....801........781.......
|
||||||
|
...*.99.......766@.........751.........174..760..169....479*368.382..281.............&........*.....*......179........=..........=.......51.
|
||||||
|
.670..&..565.......577.$......+...=583........-......=............*....=......173#....732....9...221............*979....*....=..............
|
||||||
|
...............551..&..235..............204*....@...65.......735..664................................966.918$........247.945..237...........
|
||||||
|
.....920....42..*............844&.364@......823.801...........*.............&.995...344...688..178............560....................658*680
|
||||||
|
.271...#........339.142.................921.................219..748......727.*.....*............@...........*.................131..........
|
||||||
|
............./......#......260.....%851...*......................*.............910...535................657..996.%499..625........*.........
|
||||||
|
....@363......785...........*.............795........31.........213.443....................................*...........+........244.....=806
|
||||||
|
.........390.........320...553...84@.................*..........................45....547.........376.......206.............................
|
||||||
|
54...................*................131..........*..32...............469........=..*.....665......&.........................427......349..
|
||||||
|
..................193......894.....*.....*442...589.....................&............124...=.............405.........636......*...#.........
|
||||||
|
..............................-.950.457.......................................-...................39.875...*.....230*......191..349.........
|
||||||
|
.../.........863..........334...........90..199........184....................421.....979...666..+....*.....145.......#.....................
|
||||||
|
...288..............568..#....501........*.....................$.....677.+.............*......*....959..............302..=875..821@.....531.
|
||||||
|
...................*.........*....625..621.....689........405..247..#.....351..659..162....942............349.304...........................
|
||||||
|
.......851.........742...&...478.....*.........#....523.........................=................$.......*.........589....758......371......
|
||||||
|
...$........745........593...........181...835......*......794....=...502..381@...................973...77..433.....=....&........#.........
|
||||||
|
.932........*................-.............%..........147........316..*..........%..........................*................323............
|
||||||
|
.........283...+......100...486....4.............52..*...............977.......472.*....262................298......=894..........128.93....
|
||||||
|
.............953.......*........................*....719....$......................473.....=...523......-.......723..................*......
|
||||||
|
....................571.......................720..........269...........885.............................902...........80...738..........975
|
||||||
10
inputs/day03.test
Normal file
10
inputs/day03.test
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
467..114..
|
||||||
|
...*......
|
||||||
|
..35..633.
|
||||||
|
......#...
|
||||||
|
617*......
|
||||||
|
.....+.58.
|
||||||
|
..592.....
|
||||||
|
......755.
|
||||||
|
...$.*....
|
||||||
|
.664.598..
|
||||||
101
src/day03.clj
Normal file
101
src/day03.clj
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
(ns day03
|
||||||
|
(:require [clojure.string :as string]))
|
||||||
|
|
||||||
|
(def input
|
||||||
|
(->> "inputs/day03"
|
||||||
|
slurp string/split-lines))
|
||||||
|
|
||||||
|
(def width (count (first input)))
|
||||||
|
(def height (count input))
|
||||||
|
|
||||||
|
(defn find-numbers-in-line [row l]
|
||||||
|
(loop [l l ns {} col 0]
|
||||||
|
(if (empty? l)
|
||||||
|
ns
|
||||||
|
(if-let [n (re-find #"^\d+" l)]
|
||||||
|
(recur (subs l (count n))
|
||||||
|
(assoc ns [row col] n)
|
||||||
|
(+ col (count n)))
|
||||||
|
(recur (subs l 1)
|
||||||
|
ns
|
||||||
|
(inc col))))))
|
||||||
|
|
||||||
|
(def numbers
|
||||||
|
(->>
|
||||||
|
(map-indexed
|
||||||
|
(fn [row l] (find-numbers-in-line row l))
|
||||||
|
input)
|
||||||
|
(remove empty?)
|
||||||
|
(reduce into)))
|
||||||
|
|
||||||
|
(defn adjacent [[[row col] n]]
|
||||||
|
(let [first-col (dec col)
|
||||||
|
last-col (+ col (count n))
|
||||||
|
row-up (map #(vector (dec row) %) (range first-col (inc last-col)))
|
||||||
|
row-down (map #(vector (inc row) %) (range first-col (inc last-col)))]
|
||||||
|
(->> (concat row-up [[row first-col] [row last-col]] row-down)
|
||||||
|
(remove (fn [[row col]] (or (neg? row) (neg? col))))
|
||||||
|
(filter (fn [[row col]] (and (< row height) (< col width)))))))
|
||||||
|
|
||||||
|
(defn part-number? [num]
|
||||||
|
(->> (adjacent num)
|
||||||
|
(map #(get-in input %))
|
||||||
|
(remove #(= \. %))
|
||||||
|
seq))
|
||||||
|
|
||||||
|
(def part1-answer
|
||||||
|
(->> (filter part-number? numbers)
|
||||||
|
(map second)
|
||||||
|
(map read-string)
|
||||||
|
(reduce +)))
|
||||||
|
|
||||||
|
(defn find-gears-in-line [row l]
|
||||||
|
(loop [l l gs [] col 0]
|
||||||
|
(if (empty? l)
|
||||||
|
gs
|
||||||
|
(if (= \* (first l))
|
||||||
|
(recur (rest l) (conj gs [row col]) (inc col))
|
||||||
|
(recur (rest l) gs (inc col))))))
|
||||||
|
|
||||||
|
(def asterisks
|
||||||
|
(->> (map-indexed
|
||||||
|
(fn [row l] (find-gears-in-line row l))
|
||||||
|
input)
|
||||||
|
(remove empty?)
|
||||||
|
(apply concat)))
|
||||||
|
|
||||||
|
;; returns all adjacent coordinates for all copies of part number in
|
||||||
|
;; input data
|
||||||
|
(defn adjacent2 [n coords]
|
||||||
|
(mapcat (fn [[row col]]
|
||||||
|
(adjacent [[row col] n]))
|
||||||
|
coords))
|
||||||
|
|
||||||
|
(defn adjacent? [asterisk [n coords]]
|
||||||
|
(some #{asterisk} (adjacent2 n coords)))
|
||||||
|
|
||||||
|
(def numbers-part-2
|
||||||
|
(->>
|
||||||
|
numbers
|
||||||
|
(reduce
|
||||||
|
(fn [ns n]
|
||||||
|
(let [coord (key n)
|
||||||
|
num (val n)]
|
||||||
|
(if (contains? ns num)
|
||||||
|
(update ns num conj coord)
|
||||||
|
(assoc ns num [coord]))))
|
||||||
|
{})))
|
||||||
|
|
||||||
|
(defn gear [asterisk]
|
||||||
|
(let [adjacent-numbers (->>
|
||||||
|
numbers-part-2
|
||||||
|
(map #(when (adjacent? asterisk %) %))
|
||||||
|
(remove nil?))]
|
||||||
|
(when (== 2 (count adjacent-numbers))
|
||||||
|
(map #(read-string (first %)) adjacent-numbers))))
|
||||||
|
|
||||||
|
(def part2-answer
|
||||||
|
(->> (map gear asterisks)
|
||||||
|
(remove nil?)
|
||||||
|
(map #(apply * %))
|
||||||
|
(reduce +)))
|
||||||
Loading…
x
Reference in New Issue
Block a user