From e2b15781f674220586e860fb9a85b6ad0f278fad Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Wed, 29 Dec 2021 10:56:43 +0000 Subject: [PATCH] Done day 22 --- advent-of-code21.cabal | 5 + advent22/Main.hs | 112 +++++++++++ data/advent22.txt | 420 +++++++++++++++++++++++++++++++++++++++++ data/advent22a.txt | 4 + data/advent22b.txt | 22 +++ problems/day22.html | 287 ++++++++++++++++++++++++++++ 6 files changed, 850 insertions(+) create mode 100644 advent22/Main.hs create mode 100644 data/advent22.txt create mode 100644 data/advent22a.txt create mode 100644 data/advent22b.txt create mode 100644 problems/day22.html diff --git a/advent-of-code21.cabal b/advent-of-code21.cabal index b760b36..8ba8f78 100644 --- a/advent-of-code21.cabal +++ b/advent-of-code21.cabal @@ -211,3 +211,8 @@ executable advent21 import: common-extensions, build-directives main-is: advent21/Main.hs build-depends: text, attoparsec, containers, multiset + +executable advent22 + import: common-extensions, build-directives + main-is: advent22/Main.hs + build-depends: linear, text, attoparsec, containers, lens diff --git a/advent22/Main.hs b/advent22/Main.hs new file mode 100644 index 0000000..51c7a8e --- /dev/null +++ b/advent22/Main.hs @@ -0,0 +1,112 @@ +-- Writeup at https://work.njae.me.uk/2021/12/21/advent-of-code-2021-day-19/ + +import Data.Text () +import qualified Data.Text.IO as TIO +import Data.Attoparsec.Text -- hiding (take, takeWhile) +import Control.Applicative + +import Linear -- (V3(..), (^+^), (^-^)) +-- import Linear.V3 +-- import Data.Ix +import Control.Lens +import Data.List + +type Coord = V3 Int + +data Parity = On | Off deriving (Eq, Ord, Show) + +data Cuboid = Cuboid + { _bounds :: (Coord, Coord) + , _parity :: Parity + , _time :: Int + } + deriving (Ord, Eq, Show) +makeLenses ''Cuboid + +-- Main + +main :: IO () +main = + do text <- TIO.readFile "data/advent22.txt" + let cuboids = successfulParse text + print $ part1 cuboids + print $ part2 cuboids + -- print $ part2 transScanners + + +part1 cuboids = sweepX (filter isLocal cuboids) +part2 cuboids = sweepX cuboids + +isLocal :: Cuboid -> Bool +isLocal cuboid = all (>= (- 50)) ls && all (<= 50) hs + where ls = [cuboid ^. bounds . _1 . c | c <- [_x, _y, _z]] :: [Int] + hs = [cuboid ^. bounds . _2 . c | c <- [_x, _y, _z]] :: [Int] + +straddles :: (Lens' (V3 Int) Int) -> Int -> Cuboid -> Bool +straddles f here cuboid = + ((cuboid ^. bounds . _1 . f) <= here) && ((cuboid ^. bounds . _2 . f) >= here) + +events :: (Lens' (V3 Int) Int) -> [Cuboid] -> [Int] +events f cuboids = nub $ sort $ ls ++ hs + where ls = map (^. bounds . _1 . f) cuboids + hs = map ((+1) . (^. bounds . _2 . f)) cuboids + +isActive :: [Cuboid] -> Bool +isActive [] = False +isActive cs = ((last scs) ^. parity) == On + where scs = sortOn (^. time) cs + +sweepX :: [Cuboid] -> Int +sweepX cuboids = sum $ map (volumeSize cuboids) segments + where evs = events _x cuboids + segments = if null evs then [] else zip evs $ tail evs + +volumeSize :: [Cuboid] -> (Int, Int) -> Int +volumeSize cuboids (here, there) = (sweepY cuboidsHere) * (there - here) + where cuboidsHere = filter (straddles _x here) cuboids + +sweepY :: [Cuboid] -> Int +sweepY cuboids = sum $ map (areaSize cuboids) segments + where evs = events _y cuboids + segments = if null evs then [] else zip evs $ tail evs + +areaSize :: [Cuboid] -> (Int, Int) -> Int +areaSize cuboids (here, there) = (countActive cuboidsHere) * (there - here) + where cuboidsHere = filter (straddles _y here) cuboids + +-- assume for a given x and y. +countActive :: [Cuboid] -> Int +countActive cuboids = sum $ map (segmentSize cuboids) segments + where evs = events _z cuboids + segments = if null evs then [] else zip evs $ tail evs + +segmentSize :: [Cuboid] -> (Int, Int) -> Int +segmentSize cuboids (here, there) + | isActive $ filter (straddles _z here) cuboids = (there - here) + | otherwise = 0 + + +-- Parse the input file + +cuboidsP = timeify <$> cuboidP `sepBy` endOfLine + where timeify cuboids = map (\(c, n) -> c & time .~ n) $ zip cuboids [0..] + +cuboidP = cubify <$> (partiyP <* " ") <*> (boundsP `sepBy` ",") + where cubify p ranges = + Cuboid { _parity = p + , _bounds = ( vecify (map fst ranges) + , vecify (map snd ranges) + ) + , _time = 0 + } + vecify [c1, c2, c3] = V3 c1 c2 c3 + +partiyP = ("on" *> pure On) <|> ("off" *> pure Off) + +boundsP = (,) <$> (("x" <|> "y" <|> "z") *> "=" *> signed decimal) <*> (".." *> signed decimal) + +-- successfulParse :: Text -> (Integer, [Maybe Integer]) +successfulParse input = + case parseOnly cuboidsP input of + Left _err -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err + Right cuboids -> cuboids diff --git a/data/advent22.txt b/data/advent22.txt new file mode 100644 index 0000000..67ef470 --- /dev/null +++ b/data/advent22.txt @@ -0,0 +1,420 @@ +on x=-14..32,y=-22..28,z=-25..26 +on x=-20..32,y=-3..46,z=-33..18 +on x=-21..31,y=-37..11,z=-9..43 +on x=-21..25,y=-26..27,z=-25..26 +on x=-23..29,y=-36..13,z=-5..47 +on x=-9..42,y=-6..42,z=-11..38 +on x=-41..11,y=-47..5,z=-6..43 +on x=-21..23,y=-38..13,z=0..46 +on x=-14..30,y=-19..28,z=-47..-2 +on x=-19..25,y=-7..41,z=1..47 +off x=29..42,y=-16..-1,z=-31..-14 +on x=-10..36,y=-17..34,z=-47..-2 +off x=8..22,y=-49..-36,z=-29..-12 +on x=-25..19,y=-18..32,z=-2..45 +off x=5..20,y=-26..-11,z=17..28 +on x=-38..16,y=-11..37,z=-41..8 +off x=36..47,y=-42..-23,z=8..21 +on x=-38..14,y=-2..45,z=-16..34 +off x=10..21,y=21..34,z=-18..-5 +on x=-7..41,y=-20..31,z=-9..39 +on x=-34984..-26543,y=51648..78707,z=2562..28760 +on x=-86882..-76628,y=-3025..23665,z=212..25252 +on x=16968..25727,y=-63916..-50529,z=-66385..-32536 +on x=51240..58676,y=-70513..-53482,z=-5723..-2181 +on x=25522..47536,y=59856..75899,z=-26563..-20675 +on x=-47427..-29230,y=263..15871,z=60551..73934 +on x=-49066..-32558,y=-83794..-57151,z=-12695..9141 +on x=7952..11761,y=-87854..-74674,z=-33510..-18712 +on x=14813..45800,y=-88090..-59749,z=-16138..19384 +on x=54962..82735,y=-51202..-41420,z=-6277..3174 +on x=28570..39632,y=3948..23664,z=59429..92252 +on x=10173..34690,y=54298..71815,z=33766..47516 +on x=-30040..-8528,y=47135..59588,z=53864..79134 +on x=52995..78066,y=34652..67693,z=-1410..11141 +on x=-59744..-30776,y=-69920..-47945,z=-40419..-23586 +on x=-45505..-36193,y=-7591..6204,z=-81897..-56319 +on x=-27185..4367,y=70914..91278,z=-37549..-6146 +on x=68299..85401,y=-2668..18702,z=7949..38736 +on x=-48016..-39417,y=-30061..-131,z=47316..67163 +on x=18922..45171,y=45663..74414,z=29341..45641 +on x=-83234..-54027,y=44109..52468,z=-4481..28200 +on x=11096..21770,y=-29415..3025,z=75030..79682 +on x=-12391..240,y=-82180..-59968,z=-62792..-43281 +on x=-21740..3314,y=-76672..-50745,z=-67582..-45134 +on x=24006..40635,y=-54293..-32906,z=56194..75600 +on x=-64832..-51947,y=36327..54090,z=-31747..-13348 +on x=-5630..3784,y=25936..35775,z=-79445..-55695 +on x=59474..79536,y=11162..20087,z=-43239..-21997 +on x=-42704..-21476,y=42749..67408,z=45494..62514 +on x=-67338..-34016,y=39691..52934,z=23922..46990 +on x=36..29262,y=-66430..-64048,z=-47255..-32882 +on x=-49961..-23854,y=-33575..-17917,z=60474..79383 +on x=-95438..-61587,y=-6573..12719,z=-16659..7687 +on x=48747..57111,y=-45643..-33863,z=-52117..-25721 +on x=-7911..10806,y=69265..78676,z=-43705..-9526 +on x=-33044..-7074,y=-57853..-34320,z=36878..55690 +on x=-28089..-14737,y=-90786..-66686,z=-38515..-12841 +on x=28657..34475,y=51064..66504,z=-44706..-31406 +on x=-38352..-7921,y=-36628..-19091,z=69614..89173 +on x=33444..62541,y=-86297..-62055,z=-25558..3049 +on x=-7893..9364,y=45810..68816,z=-63710..-26796 +on x=-96852..-63618,y=-23272..5127,z=-21228..13510 +on x=49451..80375,y=-17005..5647,z=32766..48164 +on x=1216..23787,y=-79871..-70398,z=-39246..-12428 +on x=8110..28804,y=-23683..-6255,z=-86978..-60413 +on x=-91843..-64794,y=21641..32187,z=-25519..1600 +on x=-84702..-60659,y=24066..41911,z=-2842..13228 +on x=-84116..-50874,y=-2116..24626,z=39105..47345 +on x=-69707..-49086,y=-58246..-33874,z=-49108..-19140 +on x=-80374..-57833,y=-39663..-9105,z=37441..56461 +on x=-65366..-33730,y=43099..59734,z=12904..42440 +on x=-37882..-15459,y=55202..81928,z=29196..57759 +on x=-73714..-66268,y=-42342..-22053,z=-1577..17395 +on x=-45347..-18358,y=-71639..-50205,z=34719..43466 +on x=62119..93299,y=-38373..-6511,z=-19061..-12726 +on x=68788..79926,y=-26182..-6654,z=64..29378 +on x=-3163..19750,y=60505..90174,z=-29340..-4768 +on x=38014..51952,y=-55129..-41022,z=-53465..-43181 +on x=-83575..-54103,y=-38626..-8427,z=-58305..-26823 +on x=57418..75464,y=32685..55142,z=27632..47860 +on x=17409..43096,y=-14819..5247,z=56286..91172 +on x=-57542..-38853,y=-36026..-22716,z=-53467..-44559 +on x=-54395..-40763,y=53537..74971,z=8637..31681 +on x=-83350..-54446,y=-44870..-26661,z=5069..27109 +on x=45158..67479,y=29868..51352,z=-34080..-27306 +on x=-35037..1444,y=-27954..-14736,z=69985..75566 +on x=-48243..-29989,y=48513..72378,z=-846..11192 +on x=16940..34441,y=-23094..-20006,z=55993..84391 +on x=-80060..-74149,y=-19098..-6716,z=-8014..22908 +on x=-35703..-13805,y=-1750..11794,z=-79543..-68729 +on x=19096..49179,y=-19061..6100,z=-90095..-61586 +on x=-5759..12300,y=-24129..1598,z=-78131..-71335 +on x=-80107..-59282,y=18788..28926,z=-32218..-18671 +on x=17906..47185,y=70542..86767,z=-15303..-7410 +on x=-50011..-41176,y=-65664..-47330,z=-31467..145 +on x=-65064..-54780,y=30369..47688,z=-27259..-9116 +on x=-41200..-25966,y=8286..16030,z=-80485..-71406 +on x=-25005..-8465,y=-62425..-39883,z=-73910..-49104 +on x=-90073..-65919,y=-40041..-6842,z=11821..34815 +on x=-9863..5212,y=62942..73336,z=-49611..-32481 +on x=-72450..-53738,y=25669..61019,z=-42767..-33000 +on x=70524..79427,y=11137..21973,z=-33847..-17549 +on x=-33296..-22111,y=38021..42133,z=58536..63703 +on x=40059..62864,y=-31079..-21302,z=-64201..-54154 +on x=-15941..12092,y=7536..15873,z=78034..86183 +on x=-41500..-15959,y=33851..48026,z=-74834..-41086 +on x=-59083..-38668,y=-13350..-2027,z=49484..73174 +on x=-4095..8756,y=-64780..-51492,z=49645..62452 +on x=-31361..-12314,y=-38990..-20332,z=-90545..-56684 +on x=-46362..-38435,y=61213..80598,z=-30242..-4160 +on x=4406..10923,y=39319..59618,z=41980..76407 +on x=42541..67135,y=-62451..-39506,z=16125..24314 +on x=-88856..-63914,y=-41550..-6971,z=-36441..-10957 +on x=-57870..-29367,y=62111..87873,z=346..8417 +on x=25365..57215,y=-51965..-37423,z=-68675..-54847 +on x=-42073..-20908,y=-29544..-2756,z=-79732..-58011 +on x=-91232..-69039,y=-19067..-15167,z=-7422..7651 +on x=-79660..-56222,y=-40850..-16219,z=-53704..-34791 +on x=-28030..-19052,y=54771..59286,z=47770..61473 +on x=4679..8347,y=-68490..-37569,z=-71691..-54025 +on x=-36261..-33968,y=65152..87454,z=15069..23461 +on x=-57397..-32767,y=-26556..-7367,z=-61244..-50883 +on x=6647..43596,y=-65890..-33847,z=-60762..-39595 +on x=44139..81693,y=-18393..3971,z=41751..54896 +on x=58964..89245,y=-29574..-47,z=-37566..-18941 +on x=7903..23182,y=77128..82135,z=-26489..-3834 +on x=-53964..-36089,y=23028..34027,z=49791..71611 +on x=-42773..-30730,y=37521..59454,z=34775..47152 +on x=62682..94006,y=-28953..-20439,z=-2866..11849 +on x=-74328..-48322,y=13265..43861,z=30215..41614 +on x=28122..34699,y=32573..53783,z=64261..71660 +on x=25714..53862,y=32522..40741,z=44640..64380 +on x=5135..26963,y=66487..77359,z=-28927..-5750 +on x=53256..67009,y=42403..62040,z=-18273..3659 +on x=2724..22380,y=-71266..-50543,z=-51153..-23711 +on x=14732..36928,y=69759..80965,z=-14161..-1907 +on x=2221..11122,y=-2798..25790,z=75480..80705 +on x=-8779..12240,y=-52629..-41036,z=-79171..-44613 +on x=-78433..-58051,y=15556..26838,z=-63149..-36913 +on x=47350..65323,y=24442..41722,z=17632..48332 +on x=-87960..-72048,y=10138..40520,z=-28933..-22155 +on x=-70110..-51730,y=-53097..-48326,z=15171..33120 +on x=-42568..-22819,y=43046..63237,z=31102..53381 +on x=30352..42731,y=50048..74026,z=34360..45595 +on x=-75575..-60339,y=-49604..-26199,z=-30633..-26441 +on x=-39156..-11816,y=-7319..31334,z=-78526..-66890 +on x=63486..78527,y=-42319..-24356,z=-28097..5129 +on x=65275..77354,y=25121..50655,z=10107..44272 +on x=-15735..-8751,y=-80245..-59369,z=-45141..-22290 +on x=-22200..-16116,y=2819..20817,z=-92548..-73896 +on x=27288..32028,y=-56404..-45837,z=40665..65602 +on x=-87559..-65472,y=-7140..1719,z=-34450..150 +on x=65413..90229,y=12624..27783,z=-22210..-11015 +on x=52662..68206,y=-2927..12091,z=51623..58483 +on x=449..20528,y=58423..86096,z=-30314..-6386 +on x=28651..54782,y=67421..87042,z=-10822..18948 +on x=-2097..26946,y=55130..71111,z=48439..55954 +on x=-400..28565,y=30492..45291,z=-86975..-68022 +on x=-21933..11975,y=58104..64964,z=-52315..-37205 +on x=24194..42657,y=-8955..10900,z=61373..88513 +on x=-65768..-40461,y=-59729..-45233,z=1323..23698 +on x=44198..65990,y=-8536..10867,z=-62699..-49680 +on x=-92079..-66561,y=-42535..-10970,z=-28894..-6059 +on x=58008..85453,y=-30345..-15668,z=-28123..-16771 +on x=-41605..-10850,y=-89020..-56631,z=-38968..-24180 +on x=1405..24877,y=-8086..5054,z=73018..83807 +on x=57620..79914,y=-44641..-37383,z=11175..28207 +on x=-86076..-54704,y=20502..30827,z=902..30801 +on x=-9203..17788,y=-92465..-75253,z=-41423..-19571 +on x=-45664..-33915,y=-70403..-62505,z=4945..21199 +on x=37619..56309,y=49836..79516,z=-32494..-9948 +on x=-21412..3869,y=-59795..-57282,z=-67147..-47843 +on x=-6408..12811,y=76516..98300,z=-11383..12528 +on x=-11591..-3910,y=55667..65005,z=36361..59408 +on x=31944..43301,y=54028..66235,z=-43324..-9651 +on x=29667..49469,y=37069..52070,z=-65427..-36883 +on x=-47659..-25192,y=26924..37825,z=57768..80653 +on x=54447..87516,y=4450..27816,z=-50280..-19273 +on x=63625..74718,y=-43042..-17884,z=6860..19348 +on x=-37001..-25122,y=-84208..-55034,z=5805..29579 +on x=8593..23288,y=28927..60647,z=-66517..-51182 +on x=56272..86054,y=-35040..-11786,z=-4650..7567 +on x=24182..55820,y=-41430..-27777,z=49216..79902 +on x=53831..60772,y=-11415..12136,z=53987..64682 +on x=14912..30906,y=-72651..-41298,z=43838..63318 +on x=5745..25358,y=18835..35588,z=59119..84625 +on x=-26685..-11191,y=59709..88104,z=-6467..11125 +on x=-28912..3937,y=-15004..8700,z=-98360..-61633 +on x=-12903..2361,y=17..18248,z=-81715..-64855 +on x=45615..72644,y=-71598..-37550,z=-2676..19050 +on x=32929..68025,y=-37995..-23876,z=-58571..-48665 +on x=20720..27000,y=-85746..-62454,z=-42881..-29570 +on x=9751..32735,y=72223..82214,z=-615..26936 +on x=-54778..-31244,y=-51548..-34864,z=51437..67846 +on x=-95867..-62875,y=8059..39412,z=-16075..-8463 +on x=-58617..-29914,y=65524..80535,z=-160..14882 +on x=-97175..-66019,y=6732..13442,z=-5054..-4537 +on x=9909..22956,y=43932..69310,z=49991..62879 +on x=10307..26798,y=-58449..-22603,z=56161..69364 +on x=-52418..-35038,y=20797..32859,z=47367..68176 +on x=50865..70613,y=25950..55232,z=1715..18345 +on x=31261..50752,y=57062..67534,z=-10578..3620 +on x=-30718..-20498,y=-81224..-49610,z=-43224..-27822 +on x=-47016..-42262,y=-17054..3287,z=56582..68954 +on x=65759..75369,y=10302..34231,z=9079..37287 +on x=-81428..-45144,y=-38882..-18217,z=22946..47183 +on x=22364..47688,y=-9944..10518,z=-75657..-58786 +on x=-59323..-27735,y=-40586..-19558,z=48616..61066 +on x=-44355..-21766,y=44377..55344,z=-65666..-54938 +on x=7551..24890,y=-59803..-45594,z=-71713..-53395 +on x=21051..46459,y=-15141..11457,z=-81430..-55097 +on x=28451..40401,y=50600..73922,z=26258..48802 +on x=65359..73866,y=5848..25953,z=-41269..-12089 +on x=50437..79435,y=-12375..-3496,z=-56754..-34377 +on x=42960..71465,y=-50596..-33356,z=38051..53128 +on x=56065..83859,y=-12331..15724,z=-44672..-25006 +on x=43816..61467,y=-27854..3321,z=-66359..-46311 +on x=46419..71959,y=15799..49190,z=19825..42092 +on x=12762..19545,y=35492..53207,z=-76222..-59251 +on x=-29359..8800,y=-40020..-18365,z=59128..91042 +on x=-57633..-35906,y=11786..31613,z=-71153..-45879 +off x=-28805..-17956,y=-59888..-43012,z=42648..74611 +off x=-42643..-19588,y=-78988..-65222,z=-21724..-13301 +off x=-27666..-4093,y=25817..52990,z=-78773..-62002 +on x=-44447..-22010,y=-4350..8087,z=71792..76443 +off x=-52436..-36521,y=58187..60171,z=-30877..-5785 +off x=30949..54850,y=-80253..-51446,z=-42817..-17759 +off x=-12430..2289,y=67850..78580,z=-22187..-9886 +on x=78033..83816,y=-6411..2621,z=-16082..17718 +on x=6033..26840,y=46920..61676,z=-62786..-60371 +on x=41642..68702,y=-31557..-6225,z=-46944..-32045 +off x=42328..54839,y=-36837..-25464,z=44803..67696 +on x=-4696..9727,y=-78316..-61064,z=-43010..-25992 +off x=2577..5831,y=33478..50260,z=-74134..-59334 +off x=44509..59254,y=-46525..-24266,z=-52032..-29431 +on x=-49736..-39595,y=44340..73878,z=14131..27812 +on x=-5708..2717,y=-4707..26547,z=-82128..-73256 +off x=-3861..22852,y=20675..37847,z=62464..82612 +on x=-61797..-34749,y=-69661..-56672,z=-23152..-3146 +off x=-2151..11187,y=11086..15192,z=-98635..-74513 +on x=75566..89896,y=-6282..21295,z=-9410..15219 +on x=-6221..29338,y=-79154..-53916,z=38043..44917 +off x=-58367..-51748,y=44711..51127,z=-48861..-27655 +off x=-70068..-49996,y=-28609..-6512,z=34004..47719 +on x=73203..84184,y=7347..28289,z=25524..44263 +off x=-20438..6097,y=-78991..-53563,z=-62000..-43052 +off x=-74573..-47172,y=-50896..-27703,z=29260..43117 +off x=-64917..-43787,y=-69200..-48743,z=-4493..1817 +off x=-4740..6426,y=-79627..-55329,z=-35575..-19230 +off x=37671..67456,y=-20558..10079,z=-64509..-58127 +on x=-57773..-36494,y=43821..60372,z=-40639..-14709 +off x=42010..54972,y=-58161..-45494,z=13543..22941 +off x=-10472..11730,y=58458..92017,z=-34884..-19062 +on x=42614..70836,y=-58228..-25353,z=9183..41819 +off x=-71437..-59603,y=-18631..-1205,z=42785..55274 +on x=-11633..11598,y=-88188..-70416,z=-45039..-15775 +off x=-34786..-18384,y=48292..76994,z=27707..41300 +off x=67987..91582,y=-24892..-6216,z=-27519..-4073 +off x=-95863..-71402,y=-12803..-5538,z=-18344..-6375 +off x=-48801..-32636,y=-28613..-11153,z=-89670..-63018 +off x=-71768..-40216,y=26550..36989,z=30142..63385 +on x=37884..68214,y=39248..55995,z=25722..49328 +on x=70565..75222,y=-20619..-3412,z=27935..52930 +off x=-21598..2011,y=74781..81100,z=-25987..-3855 +on x=-75036..-61874,y=-32562..-5327,z=-50998..-41765 +off x=-63523..-41725,y=5688..29199,z=44858..71165 +off x=61744..82331,y=19706..33500,z=-8681..23550 +on x=-13018..1939,y=-85134..-70690,z=21016..26881 +off x=46091..63616,y=-9027..9805,z=47979..67694 +on x=-43168..-23185,y=38140..73548,z=-70403..-45064 +on x=-21826..-15877,y=-4769..2487,z=-88364..-72521 +on x=-45604..-19621,y=-42328..-27813,z=41716..64795 +off x=-37807..-6478,y=62634..81953,z=21871..40083 +off x=-86239..-52437,y=-34061..-24158,z=6635..22285 +off x=-10046..8212,y=-57826..-42350,z=-64233..-50000 +off x=-23400..-15200,y=20821..46657,z=-69259..-61044 +off x=63800..85169,y=25545..36274,z=1181..9243 +off x=34365..46983,y=60399..70159,z=-9350..10407 +off x=75052..91805,y=-6284..14904,z=-12646..5614 +off x=-12868..17859,y=26208..52846,z=69112..89483 +on x=-76644..-57162,y=-7449..5409,z=44619..49339 +on x=-80361..-62758,y=-39442..-4968,z=-22736..3121 +off x=-13708..17868,y=-84516..-60741,z=28313..59460 +on x=-61365..-27022,y=60283..79040,z=12734..34211 +on x=59390..88366,y=3645..19175,z=10089..27184 +on x=64326..76729,y=-41855..-20943,z=-15727..12390 +on x=-35669..-15225,y=-75232..-71544,z=14793..22177 +on x=1446..19605,y=30027..41625,z=-90629..-64444 +on x=-15642..-2359,y=-37513..-9009,z=60067..77543 +off x=42909..63915,y=42085..55920,z=26312..61750 +off x=68237..77060,y=-8177..10878,z=22063..39904 +on x=-79592..-61745,y=-43270..-22274,z=-15365..-7259 +on x=-26948..-1541,y=-70070..-35808,z=34287..67722 +off x=-1759..21632,y=51902..66240,z=-58961..-38669 +off x=62017..99124,y=8399..25845,z=-6825..4625 +on x=-81423..-45456,y=37042..58320,z=5711..35366 +on x=-82431..-70294,y=30267..46149,z=-21442..-8817 +on x=-50291..-18766,y=62669..89780,z=13362..37144 +on x=-57455..-30808,y=-33781..-25716,z=-62288..-49278 +off x=69408..77145,y=-30227..-3558,z=-8412..13712 +on x=61933..69888,y=-12888..12859,z=27164..54531 +off x=-72176..-58281,y=-30439..-3646,z=33760..57845 +on x=7054..12401,y=16053..38787,z=71624..93039 +on x=-82557..-56975,y=-9661..7083,z=-49481..-37495 +off x=-63291..-52165,y=38518..66467,z=-21283..-6110 +off x=16118..33266,y=14334..21585,z=-83237..-54922 +on x=-83749..-57908,y=-4258..16963,z=-39970..-37018 +off x=-40166..-19345,y=31883..67052,z=37110..67786 +off x=53380..67985,y=30237..59055,z=10510..26125 +on x=63305..68408,y=40812..49109,z=-19691..-270 +on x=7857..29425,y=-67019..-30697,z=-66136..-49059 +on x=-6691..14066,y=8113..20816,z=68185..83416 +off x=56395..68976,y=-41328..-8083,z=29277..56422 +off x=42604..60191,y=-46774..-17713,z=-70387..-40480 +on x=44483..57415,y=22283..48790,z=32064..54994 +on x=17474..35769,y=-66687..-45930,z=-58457..-49847 +off x=15183..27040,y=77008..79727,z=5346..23053 +on x=57382..83244,y=-1506..16227,z=-42975..-26600 +off x=-76269..-59043,y=-39996..-15892,z=-5245..9633 +off x=-48720..-38320,y=-82501..-50743,z=-30445..3452 +off x=44165..72746,y=-56976..-44779,z=20590..51329 +off x=31202..50559,y=-76566..-68717,z=1071..25360 +off x=48638..68694,y=39421..47200,z=-54744..-25005 +on x=-14094..4575,y=-83328..-65186,z=-34311..-29606 +off x=-19001..4040,y=-81894..-59810,z=-36960..-18967 +on x=30791..52076,y=-25832..-4223,z=-73089..-56794 +off x=-93615..-62412,y=-19037..11194,z=-14911..-3294 +on x=-25222..7078,y=-71358..-40442,z=49313..70622 +off x=34344..62044,y=6210..11031,z=60071..75518 +off x=-36155..-19629,y=-67617..-54197,z=-36930..-25462 +off x=14784..49665,y=56247..79695,z=-40721..-10725 +off x=45116..56902,y=-57539..-36970,z=-38837..-6838 +off x=-74240..-58205,y=40561..53888,z=-36015..-10174 +on x=-20339..7554,y=-96016..-65717,z=3050..11613 +on x=11773..47908,y=-81962..-63309,z=4806..34070 +off x=30557..49760,y=-58795..-43120,z=35993..53984 +off x=-52744..-19296,y=9807..25934,z=-79649..-49636 +on x=50803..83487,y=5479..33026,z=38339..61575 +on x=-70483..-50069,y=-49499..-47847,z=19537..32373 +on x=22443..26988,y=12955..44140,z=-79590..-63366 +on x=66174..78169,y=22686..40038,z=-8573..13730 +off x=62989..80551,y=-37830..-4601,z=-43297..-12723 +on x=53257..56713,y=34533..53556,z=26835..31575 +off x=-4031..7654,y=8509..39962,z=-81694..-67518 +off x=57233..71094,y=27227..54883,z=10536..37871 +on x=-70015..-61633,y=25435..48517,z=-49774..-30793 +off x=-93287..-56846,y=-43238..-6705,z=-755..14291 +on x=46390..75500,y=48598..66176,z=11222..19821 +off x=-33580..-11644,y=66170..90004,z=-34066..-6888 +on x=-52264..-22145,y=-59771..-39731,z=-48084..-35038 +on x=27471..41952,y=21493..51568,z=63607..68568 +off x=35677..61663,y=-38988..-27710,z=-59698..-34034 +on x=-12750..7828,y=76570..89565,z=-9478..-2925 +on x=-83435..-59885,y=9514..27522,z=-32652..-7881 +on x=44111..55856,y=-15541..11579,z=50997..67817 +on x=-22587..7924,y=-47534..-25646,z=56797..91101 +on x=-49141..-19337,y=-4798..4186,z=-80631..-56013 +off x=42136..65030,y=-16965..-1971,z=40298..60536 +on x=-42229..-34266,y=-69343..-51015,z=9907..27353 +on x=10687..25863,y=-43246..-37990,z=-80771..-54026 +off x=-76786..-59333,y=-34403..-28720,z=21847..36652 +off x=-30992..-13228,y=-2730..11499,z=-80125..-58916 +off x=65153..93773,y=18808..27693,z=-3847..9938 +off x=-2776..20701,y=28706..54514,z=-73135..-60817 +on x=60981..91265,y=-38749..-27891,z=-411..17743 +off x=-54774..-33840,y=-50944..-31833,z=36016..68503 +on x=-56457..-42433,y=-31957..-17376,z=-67116..-39975 +on x=-66548..-58154,y=6529..38361,z=-43401..-33610 +on x=-86989..-63673,y=1246..25535,z=-29251..-13027 +off x=-63939..-36393,y=58956..74942,z=14817..20781 +on x=8693..22260,y=-40321..-24946,z=-79188..-50768 +on x=41600..58375,y=-42108..-14086,z=51334..72807 +off x=23909..36369,y=-88428..-69824,z=4151..21692 +off x=39444..56689,y=38940..48175,z=-54864..-34971 +off x=-1029..21273,y=-52735..-43901,z=-74676..-47912 +on x=39722..48576,y=-47808..-36179,z=-54647..-33662 +on x=-46883..-23410,y=-75362..-58864,z=-49691..-14864 +on x=-45647..-11809,y=-80750..-61164,z=-58949..-35240 +on x=-65456..-42409,y=-24374..-17014,z=54398..75575 +on x=39202..51304,y=42703..59340,z=16019..36596 +off x=-57900..-42577,y=36780..51719,z=-53715..-32242 +on x=32591..58935,y=52680..81095,z=-11789..17287 +on x=-59966..-41440,y=-66646..-35336,z=-23770..-2743 +on x=19804..29351,y=-48957..-37599,z=-81246..-53109 +off x=-36..17904,y=-5338..21145,z=-79404..-61902 +on x=-39881..-31460,y=-86863..-57518,z=4307..22680 +off x=-25806..-11126,y=56665..87855,z=-26783..-4947 +on x=-25407..-17465,y=8784..45426,z=-80441..-63533 +off x=46122..78812,y=-64720..-41608,z=-20340..-3282 +on x=-21418..-6135,y=28527..45821,z=-74932..-63067 +off x=23344..47538,y=57629..72961,z=-15693..8596 +off x=-11839..6967,y=19341..34049,z=-89713..-60630 +on x=-45928..-37032,y=32356..52368,z=-66659..-40545 +on x=-86673..-53256,y=-44328..-16315,z=14485..35041 +on x=2034..20139,y=53326..81029,z=39047..50387 +off x=-81855..-73071,y=-10471..3245,z=-14114..-3707 +on x=70037..76368,y=12668..35400,z=-35982..-8580 +off x=32997..62535,y=-27698..-14203,z=-61911..-36153 +on x=-29767..-15210,y=63013..73940,z=-37989..-4125 +off x=-42214..-34753,y=-31073..-18734,z=-75221..-58111 +on x=-51293..-37459,y=-65903..-40128,z=30775..47506 +on x=-28933..-18510,y=-35296..-4760,z=54010..82629 +off x=55947..81197,y=-52157..-15817,z=15934..45009 +on x=-54644..-35512,y=-89321..-61207,z=-26266..3023 +on x=51084..75115,y=-46798..-32970,z=12847..16751 +on x=-31877..-1755,y=-16518..6949,z=76605..84122 +off x=-12076..20354,y=23633..59717,z=50871..78154 +on x=7581..23640,y=-85279..-54514,z=-49510..-19917 +on x=-79844..-68838,y=-49312..-17949,z=-7165..25085 +on x=-24243..3171,y=9716..31566,z=-81170..-65766 +off x=-74462..-58771,y=-3480..26616,z=-42553..-23900 +off x=-48143..-38553,y=42767..59814,z=30402..48166 +off x=33860..48294,y=-54872..-43212,z=34467..49090 +off x=40140..46896,y=-15654..9520,z=51719..87495 +off x=-69166..-61577,y=28458..40680,z=16037..37970 +off x=14225..35212,y=-86212..-67164,z=-34232..-24202 +off x=58187..73377,y=33943..39219,z=-18273..-5694 +on x=-42848..-21135,y=-52454..-36140,z=50130..82337 +on x=-80976..-60546,y=1460..28768,z=-4463..32217 +on x=-47037..-29197,y=63702..92015,z=-7785..16238 \ No newline at end of file diff --git a/data/advent22a.txt b/data/advent22a.txt new file mode 100644 index 0000000..4d036d6 --- /dev/null +++ b/data/advent22a.txt @@ -0,0 +1,4 @@ +on x=10..12,y=10..12,z=10..12 +on x=11..13,y=11..13,z=11..13 +off x=9..11,y=9..11,z=9..11 +on x=10..10,y=10..10,z=10..10 \ No newline at end of file diff --git a/data/advent22b.txt b/data/advent22b.txt new file mode 100644 index 0000000..8c68bc7 --- /dev/null +++ b/data/advent22b.txt @@ -0,0 +1,22 @@ +on x=-20..26,y=-36..17,z=-47..7 +on x=-20..33,y=-21..23,z=-26..28 +on x=-22..28,y=-29..23,z=-38..16 +on x=-46..7,y=-6..46,z=-50..-1 +on x=-49..1,y=-3..46,z=-24..28 +on x=2..47,y=-22..22,z=-23..27 +on x=-27..23,y=-28..26,z=-21..29 +on x=-39..5,y=-6..47,z=-3..44 +on x=-30..21,y=-8..43,z=-13..34 +on x=-22..26,y=-27..20,z=-29..19 +off x=-48..-32,y=26..41,z=-47..-37 +on x=-12..35,y=6..50,z=-50..-2 +off x=-48..-32,y=-32..-16,z=-15..-5 +on x=-18..26,y=-33..15,z=-7..46 +off x=-40..-22,y=-38..-28,z=23..41 +on x=-16..35,y=-41..10,z=-47..6 +off x=-32..-23,y=11..30,z=-14..3 +on x=-49..-5,y=-3..45,z=-29..18 +off x=18..30,y=-20..-8,z=-3..13 +on x=-41..9,y=-7..43,z=-33..15 +on x=-54112..-39298,y=-85059..-49293,z=-27449..7877 +on x=967..23432,y=45373..81175,z=27513..53682 \ No newline at end of file diff --git a/problems/day22.html b/problems/day22.html new file mode 100644 index 0000000..5a3bc45 --- /dev/null +++ b/problems/day22.html @@ -0,0 +1,287 @@ + + + + +Day 22 - Advent of Code 2021 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 44*

       Î»y.2021

+ + + +
+ +

--- Day 22: Reactor Reboot ---

Operating at these extreme ocean depths has overloaded the submarine's reactor; it needs to be rebooted.

+

The reactor core is made up of a large 3-dimensional grid made up entirely of cubes, one cube per integer 3-dimensional coordinate (x,y,z). Each cube can be either on or off; at the start of the reboot process, they are all off. (Could it be an old model of a reactor you've seen before?)

+

To reboot the reactor, you just need to set all of the cubes to either on or off by following a list of reboot steps (your puzzle input). Each step specifies a cuboid (the set of all cubes that have coordinates which fall within ranges for x, y, and z) and whether to turn all of the cubes in that cuboid on or off.

+

For example, given these reboot steps:

+
on x=10..12,y=10..12,z=10..12
+on x=11..13,y=11..13,z=11..13
+off x=9..11,y=9..11,z=9..11
+on x=10..10,y=10..10,z=10..10
+
+

The first step (on x=10..12,y=10..12,z=10..12) turns on a 3x3x3 cuboid consisting of 27 cubes:

+
    +
  • 10,10,10
  • +
  • 10,10,11
  • +
  • 10,10,12
  • +
  • 10,11,10
  • +
  • 10,11,11
  • +
  • 10,11,12
  • +
  • 10,12,10
  • +
  • 10,12,11
  • +
  • 10,12,12
  • +
  • 11,10,10
  • +
  • 11,10,11
  • +
  • 11,10,12
  • +
  • 11,11,10
  • +
  • 11,11,11
  • +
  • 11,11,12
  • +
  • 11,12,10
  • +
  • 11,12,11
  • +
  • 11,12,12
  • +
  • 12,10,10
  • +
  • 12,10,11
  • +
  • 12,10,12
  • +
  • 12,11,10
  • +
  • 12,11,11
  • +
  • 12,11,12
  • +
  • 12,12,10
  • +
  • 12,12,11
  • +
  • 12,12,12
  • +
+

The second step (on x=11..13,y=11..13,z=11..13) turns on a 3x3x3 cuboid that overlaps with the first. As a result, only 19 additional cubes turn on; the rest are already on from the previous step:

+
    +
  • 11,11,13
  • +
  • 11,12,13
  • +
  • 11,13,11
  • +
  • 11,13,12
  • +
  • 11,13,13
  • +
  • 12,11,13
  • +
  • 12,12,13
  • +
  • 12,13,11
  • +
  • 12,13,12
  • +
  • 12,13,13
  • +
  • 13,11,11
  • +
  • 13,11,12
  • +
  • 13,11,13
  • +
  • 13,12,11
  • +
  • 13,12,12
  • +
  • 13,12,13
  • +
  • 13,13,11
  • +
  • 13,13,12
  • +
  • 13,13,13
  • +
+

The third step (off x=9..11,y=9..11,z=9..11) turns off a 3x3x3 cuboid that overlaps partially with some cubes that are on, ultimately turning off 8 cubes:

+
    +
  • 10,10,10
  • +
  • 10,10,11
  • +
  • 10,11,10
  • +
  • 10,11,11
  • +
  • 11,10,10
  • +
  • 11,10,11
  • +
  • 11,11,10
  • +
  • 11,11,11
  • +
+

The final step (on x=10..10,y=10..10,z=10..10) turns on a single cube, 10,10,10. After this last step, 39 cubes are on.

+

The initialization procedure only uses cubes that have x, y, and z positions of at least -50 and at most 50. For now, ignore cubes outside this region.

+

Here is a larger example:

+
on x=-20..26,y=-36..17,z=-47..7
+on x=-20..33,y=-21..23,z=-26..28
+on x=-22..28,y=-29..23,z=-38..16
+on x=-46..7,y=-6..46,z=-50..-1
+on x=-49..1,y=-3..46,z=-24..28
+on x=2..47,y=-22..22,z=-23..27
+on x=-27..23,y=-28..26,z=-21..29
+on x=-39..5,y=-6..47,z=-3..44
+on x=-30..21,y=-8..43,z=-13..34
+on x=-22..26,y=-27..20,z=-29..19
+off x=-48..-32,y=26..41,z=-47..-37
+on x=-12..35,y=6..50,z=-50..-2
+off x=-48..-32,y=-32..-16,z=-15..-5
+on x=-18..26,y=-33..15,z=-7..46
+off x=-40..-22,y=-38..-28,z=23..41
+on x=-16..35,y=-41..10,z=-47..6
+off x=-32..-23,y=11..30,z=-14..3
+on x=-49..-5,y=-3..45,z=-29..18
+off x=18..30,y=-20..-8,z=-3..13
+on x=-41..9,y=-7..43,z=-33..15
+on x=-54112..-39298,y=-85059..-49293,z=-27449..7877
+on x=967..23432,y=45373..81175,z=27513..53682
+
+

The last two steps are fully outside the initialization procedure area; all other steps are fully within it. After executing these steps in the initialization procedure region, 590784 cubes are on.

+

Execute the reboot steps. Afterward, considering only cubes in the region x=-50..50,y=-50..50,z=-50..50, how many cubes are on?

+
+

Your puzzle answer was 545118.

--- Part Two ---

Now that the initialization procedure is complete, you can reboot the reactor.

+

Starting with all cubes off, run all of the reboot steps for all cubes in the reactor.

+

Consider the following reboot steps:

+
on x=-5..47,y=-31..22,z=-19..33
+on x=-44..5,y=-27..21,z=-14..35
+on x=-49..-1,y=-11..42,z=-10..38
+on x=-20..34,y=-40..6,z=-44..1
+off x=26..39,y=40..50,z=-2..11
+on x=-41..5,y=-41..6,z=-36..8
+off x=-43..-33,y=-45..-28,z=7..25
+on x=-33..15,y=-32..19,z=-34..11
+off x=35..47,y=-46..-34,z=-11..5
+on x=-14..36,y=-6..44,z=-16..29
+on x=-57795..-6158,y=29564..72030,z=20435..90618
+on x=36731..105352,y=-21140..28532,z=16094..90401
+on x=30999..107136,y=-53464..15513,z=8553..71215
+on x=13528..83982,y=-99403..-27377,z=-24141..23996
+on x=-72682..-12347,y=18159..111354,z=7391..80950
+on x=-1060..80757,y=-65301..-20884,z=-103788..-16709
+on x=-83015..-9461,y=-72160..-8347,z=-81239..-26856
+on x=-52752..22273,y=-49450..9096,z=54442..119054
+on x=-29982..40483,y=-108474..-28371,z=-24328..38471
+on x=-4958..62750,y=40422..118853,z=-7672..65583
+on x=55694..108686,y=-43367..46958,z=-26781..48729
+on x=-98497..-18186,y=-63569..3412,z=1232..88485
+on x=-726..56291,y=-62629..13224,z=18033..85226
+on x=-110886..-34664,y=-81338..-8658,z=8914..63723
+on x=-55829..24974,y=-16897..54165,z=-121762..-28058
+on x=-65152..-11147,y=22489..91432,z=-58782..1780
+on x=-120100..-32970,y=-46592..27473,z=-11695..61039
+on x=-18631..37533,y=-124565..-50804,z=-35667..28308
+on x=-57817..18248,y=49321..117703,z=5745..55881
+on x=14781..98692,y=-1341..70827,z=15753..70151
+on x=-34419..55919,y=-19626..40991,z=39015..114138
+on x=-60785..11593,y=-56135..2999,z=-95368..-26915
+on x=-32178..58085,y=17647..101866,z=-91405..-8878
+on x=-53655..12091,y=50097..105568,z=-75335..-4862
+on x=-111166..-40997,y=-71714..2688,z=5609..50954
+on x=-16602..70118,y=-98693..-44401,z=5197..76897
+on x=16383..101554,y=4615..83635,z=-44907..18747
+off x=-95822..-15171,y=-19987..48940,z=10804..104439
+on x=-89813..-14614,y=16069..88491,z=-3297..45228
+on x=41075..99376,y=-20427..49978,z=-52012..13762
+on x=-21330..50085,y=-17944..62733,z=-112280..-30197
+on x=-16478..35915,y=36008..118594,z=-7885..47086
+off x=-98156..-27851,y=-49952..43171,z=-99005..-8456
+off x=2032..69770,y=-71013..4824,z=7471..94418
+on x=43670..120875,y=-42068..12382,z=-24787..38892
+off x=37514..111226,y=-45862..25743,z=-16714..54663
+off x=25699..97951,y=-30668..59918,z=-15349..69697
+off x=-44271..17935,y=-9516..60759,z=49131..112598
+on x=-61695..-5813,y=40978..94975,z=8655..80240
+off x=-101086..-9439,y=-7088..67543,z=33935..83858
+off x=18020..114017,y=-48931..32606,z=21474..89843
+off x=-77139..10506,y=-89994..-18797,z=-80..59318
+off x=8476..79288,y=-75520..11602,z=-96624..-24783
+on x=-47488..-1262,y=24338..100707,z=16292..72967
+off x=-84341..13987,y=2429..92914,z=-90671..-1318
+off x=-37810..49457,y=-71013..-7894,z=-105357..-13188
+off x=-27365..46395,y=31009..98017,z=15428..76570
+off x=-70369..-16548,y=22648..78696,z=-1892..86821
+on x=-53470..21291,y=-120233..-33476,z=-44150..38147
+off x=-93533..-4276,y=-16170..68771,z=-104985..-24507
+
+

After running the above reboot steps, 2758514936282235 cubes are on. (Just for fun, 474140 of those are also in the initialization procedure region.)

+

Starting again with all cubes off, execute all reboot steps. Afterward, considering all cubes, how many cubes are on?

+
+

Your puzzle answer was 1227298136842375.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your Advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file -- 2.34.1