From bde069f91262dbe45c68cc903a4fe590dfcbc93b Mon Sep 17 00:00:00 2001 From: HoLLy Date: Wed, 22 Dec 2021 16:07:07 +0100 Subject: [PATCH] Solve day 22 --- Cargo.lock | 10 +- Cargo.toml | 1 + day-21/Cargo.toml | 1 - day-22/Cargo.toml | 17 ++ day-22/benches/bench.rs | 42 ++++ day-22/input.txt | 420 ++++++++++++++++++++++++++++++++++++++++ day-22/sample.txt | 60 ++++++ day-22/src/main.rs | 328 +++++++++++++++++++++++++++++++ 8 files changed, 877 insertions(+), 2 deletions(-) create mode 100644 day-22/Cargo.toml create mode 100644 day-22/benches/bench.rs create mode 100644 day-22/input.txt create mode 100644 day-22/sample.txt create mode 100644 day-22/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 97ff398..554553c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -378,7 +378,15 @@ version = "0.1.0" dependencies = [ "aoc-lib", "criterion", - "either", +] + +[[package]] +name = "day-22" +version = "0.1.0" +dependencies = [ + "aoc-lib", + "criterion", + "tinyvec", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 5842a0f..02db9b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,4 +22,5 @@ members = [ "day-19", "day-20", "day-21", + "day-22", ] diff --git a/day-21/Cargo.toml b/day-21/Cargo.toml index d42110c..4efc4d9 100644 --- a/day-21/Cargo.toml +++ b/day-21/Cargo.toml @@ -7,7 +7,6 @@ edition = "2021" [dependencies] aoc-lib = { path = "../aoc-lib" } -either = "1.6.1" [dev-dependencies] criterion = "0.3" diff --git a/day-22/Cargo.toml b/day-22/Cargo.toml new file mode 100644 index 0000000..ef62cff --- /dev/null +++ b/day-22/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "day-22" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +aoc-lib = { path = "../aoc-lib" } +tinyvec = { version = "1.5.1", features = ["rustc_1_55", "alloc"] } + +[dev-dependencies] +criterion = "0.3" + +[[bench]] +name = "bench" +harness = false diff --git a/day-22/benches/bench.rs b/day-22/benches/bench.rs new file mode 100644 index 0000000..2b3f174 --- /dev/null +++ b/day-22/benches/bench.rs @@ -0,0 +1,42 @@ +#![allow(dead_code)] + +use aoc_lib::AdventOfCode; +use criterion::{black_box, criterion_group, criterion_main, Criterion}; + +#[path = "../src/main.rs"] +mod main; + +fn bench_main(c: &mut Criterion) { + c.bench_function("parse input", |b| { + let input = include_str!("../input.txt"); + b.iter(|| main::Day22::parse_input(black_box(input))) + }); + + c.bench_function("solve 1", |b| { + let input = main::Day22::parse_input(include_str!("../input.txt")); + b.iter(|| main::Day22::solve_1(black_box(&input))) + }); + + c.bench_function("solve 2", |b| { + let input = main::Day22::parse_input(include_str!("../input.txt")); + b.iter(|| main::Day22::solve_2(black_box(&input))) + }); + + c.bench_function("parse sample input", |b| { + let input = include_str!("../sample.txt"); + b.iter(|| main::Day22::parse_input(black_box(input))) + }); + + c.bench_function("solve 1 (sample input)", |b| { + let input = main::Day22::parse_input(include_str!("../sample.txt")); + b.iter(|| main::Day22::solve_1(black_box(&input))) + }); + + c.bench_function("solve 2 (sample input)", |b| { + let input = main::Day22::parse_input(include_str!("../sample.txt")); + b.iter(|| main::Day22::solve_2(black_box(&input))) + }); +} + +criterion_group!(benches, bench_main); +criterion_main!(benches); diff --git a/day-22/input.txt b/day-22/input.txt new file mode 100644 index 0000000..f879914 --- /dev/null +++ b/day-22/input.txt @@ -0,0 +1,420 @@ +on x=-22..32,y=-14..30,z=-17..31 +on x=-4..46,y=-21..23,z=-37..8 +on x=-49..0,y=-27..19,z=-11..42 +on x=-31..15,y=-5..46,z=-44..8 +on x=-13..38,y=-42..6,z=-48..6 +on x=-31..19,y=-13..31,z=-33..21 +on x=-42..2,y=-46..-2,z=-38..16 +on x=-39..5,y=-11..36,z=-24..24 +on x=-28..23,y=-26..24,z=-42..11 +on x=-26..27,y=-16..34,z=-13..36 +off x=-10..-1,y=22..35,z=0..13 +on x=-33..17,y=-35..19,z=-15..35 +off x=5..20,y=28..40,z=-19..-6 +on x=-33..11,y=-3..46,z=-24..21 +off x=19..28,y=36..49,z=-30..-19 +on x=-19..33,y=-21..32,z=-6..39 +off x=-9..7,y=-30..-21,z=-31..-14 +on x=-12..40,y=-14..38,z=-19..30 +off x=-22..-12,y=-48..-33,z=25..37 +on x=-28..18,y=-23..24,z=-14..37 +on x=-88454..-76767,y=-25440..-11640,z=-27748..1322 +on x=-63800..-35390,y=-42699..-23055,z=-53580..-40789 +on x=-49378..-22320,y=29310..52217,z=-68342..-37168 +on x=42129..61973,y=50539..69619,z=-18285..-5271 +on x=38183..68805,y=-74582..-36391,z=-35206..-22560 +on x=55715..83989,y=-36041..-11394,z=-39598..-4388 +on x=-24130..3055,y=-23490..12236,z=70765..96298 +on x=-16702..-2383,y=-30032..-19554,z=57781..94950 +on x=-3..22648,y=-17296..8429,z=-97524..-66196 +on x=57800..79035,y=-32755..-20229,z=-21361..2035 +on x=-64663..-45003,y=51119..67839,z=-39329..-20426 +on x=-86879..-63664,y=-29483..2376,z=-17511..917 +on x=-18452..12418,y=8090..21339,z=59767..97693 +on x=-74788..-63022,y=-31181..-4135,z=-55104..-34567 +on x=-460..20397,y=-79399..-53010,z=26609..52940 +on x=-76979..-49925,y=-12691..17007,z=44020..52464 +on x=37332..53185,y=62313..69083,z=-7879..9039 +on x=2139..32377,y=-15539..13573,z=-80647..-76654 +on x=-37295..-3736,y=6548..44201,z=66072..90569 +on x=-94984..-74748,y=9526..27861,z=899..25480 +on x=-43224..-30630,y=48652..69720,z=24956..44512 +on x=53697..65990,y=-45123..-18444,z=-34789..-26896 +on x=-18456..-7747,y=70117..80638,z=-25850..-19531 +on x=61541..84653,y=18018..51509,z=8929..29313 +on x=238..15748,y=6654..31913,z=-96887..-62882 +on x=64727..82963,y=13674..39700,z=-36851..-20759 +on x=-17347..-2428,y=-62480..-40504,z=65147..85025 +on x=-64303..-38190,y=62454..79069,z=-8611..5858 +on x=-78239..-48889,y=-45528..-32327,z=-48391..-19550 +on x=-37818..-21478,y=42272..65566,z=-55774..-52165 +on x=-31522..4867,y=23602..36817,z=59949..92353 +on x=-18083..7059,y=-84816..-56766,z=-57571..-37939 +on x=52431..79707,y=-2553..17080,z=-42747..-32049 +on x=-37827..-11781,y=-45813..-29417,z=-68088..-60947 +on x=-4968..21509,y=-4044..5972,z=68851..86247 +on x=-28623..205,y=41906..54807,z=-66205..-55226 +on x=-15232..-98,y=-86655..-64295,z=21073..37532 +on x=66601..90692,y=8660..26049,z=-38904..-13268 +on x=-21995..5505,y=50562..70740,z=41136..67156 +on x=-59910..-42170,y=58687..73827,z=-24631..7956 +on x=-63497..-60583,y=-34715..-25636,z=31097..51188 +on x=-92738..-61303,y=25051..30662,z=6602..16255 +on x=8377..29047,y=-17074..8870,z=69424..93335 +on x=13739..28496,y=35713..39818,z=-81849..-57081 +on x=41352..64038,y=-51138..-41287,z=-53062..-40227 +on x=51036..53935,y=-37781..-17188,z=49692..68781 +on x=-43656..-34095,y=-50455..-38715,z=-79038..-48370 +on x=36392..44292,y=35090..64928,z=31715..60406 +on x=3784..17707,y=45655..61258,z=64349..82290 +on x=-78233..-61209,y=37584..54206,z=-7228..4744 +on x=19133..39855,y=8370..35562,z=-76400..-51649 +on x=44940..69069,y=6980..17695,z=24148..56135 +on x=49916..63939,y=-38666..-17185,z=51377..71423 +on x=50416..60332,y=15771..32060,z=-67420..-46877 +on x=-24035..-22523,y=-56011..-29290,z=58988..70764 +on x=-18658..17034,y=-81665..-72546,z=-4251..28250 +on x=-45242..-28048,y=-67929..-51986,z=24579..32786 +on x=29827..58064,y=26194..31982,z=-58304..-40216 +on x=50210..63389,y=-61068..-38010,z=-49628..-32224 +on x=78061..95501,y=-14872..5137,z=-23903..-2240 +on x=-11792..27254,y=-24383..-9290,z=56942..90022 +on x=75509..81773,y=-23043..-13248,z=-11188..2679 +on x=39779..62451,y=-34729..-17403,z=-63193..-38463 +on x=-56516..-20811,y=-47394..-18785,z=-65798..-58923 +on x=-15527..-6785,y=-86549..-63698,z=-34131..-14592 +on x=-65135..-42832,y=45840..53433,z=26435..44333 +on x=-71276..-41790,y=-67893..-43766,z=-16337..-968 +on x=60424..70944,y=-44795..-25972,z=12607..25838 +on x=46362..72434,y=-4434..12037,z=-47234..-35883 +on x=-49165..-29696,y=-18515..-9626,z=-81945..-67024 +on x=21864..32006,y=64402..76917,z=17871..32613 +on x=-52720..-45788,y=9114..40971,z=-66103..-41360 +on x=-87716..-59548,y=-43592..-26489,z=3085..15187 +on x=3346..29055,y=55291..85873,z=12726..39861 +on x=-66852..-48236,y=-56135..-54476,z=-19570..2585 +on x=-6600..13943,y=-97410..-61149,z=-8479..15831 +on x=-89076..-68935,y=-39116..-12632,z=16991..36348 +on x=16269..33593,y=-40336..-26046,z=58639..85537 +on x=-42702..-17930,y=-28755..-16049,z=-76632..-51955 +on x=66805..80326,y=-25202..2085,z=13446..26443 +on x=-22322..-8440,y=63417..72298,z=-38633..-25355 +on x=20542..43539,y=-62501..-39529,z=44885..62706 +on x=9584..17847,y=29529..45652,z=63581..78803 +on x=23885..45874,y=-30164..-12899,z=-70757..-62124 +on x=37153..70408,y=-5521..12256,z=-67461..-48372 +on x=-46778..-36147,y=-66822..-46741,z=21424..46935 +on x=60003..69355,y=37220..51059,z=22198..37018 +on x=-17406..18840,y=46510..62998,z=50847..74599 +on x=-51384..-24911,y=-10223..-636,z=59655..84141 +on x=25484..40197,y=9034..36344,z=-72754..-54857 +on x=-71701..-54435,y=-28868..-11491,z=19639..59029 +on x=45737..57801,y=19059..23686,z=-64089..-41263 +on x=-5851..18117,y=-39606..-18579,z=-80821..-70127 +on x=-66648..-40084,y=-18660..5431,z=-62190..-48355 +on x=40862..55351,y=-13685..7791,z=56599..67604 +on x=-42130..-29079,y=-82415..-50464,z=5605..23656 +on x=-95269..-70941,y=20196..33814,z=10130..27706 +on x=15341..40959,y=7559..14321,z=-80380..-58605 +on x=29317..47110,y=42998..56003,z=-68483..-36911 +on x=26491..32948,y=-83896..-71337,z=4893..29927 +on x=8400..34640,y=-9502..-7270,z=-79855..-75872 +on x=21876..34277,y=7372..15787,z=63943..91349 +on x=34199..70599,y=-19237..15833,z=-77424..-53540 +on x=-22839..6013,y=-73022..-47467,z=-63703..-26086 +on x=54543..81844,y=-16406..13479,z=-47514..-26753 +on x=70476..73903,y=17310..36991,z=-31416..-21047 +on x=11696..18671,y=44938..72642,z=-73098..-39508 +on x=29397..54555,y=40884..70484,z=-42500..-20693 +on x=-57114..-27588,y=-71837..-63179,z=-36334..-16541 +on x=-51823..-43923,y=16450..45857,z=-61616..-51663 +on x=-29109..-5238,y=53987..76251,z=-55133..-37181 +on x=-3422..14330,y=-84711..-58996,z=-47949..-19595 +on x=-52454..-40721,y=53056..76839,z=-37476..-21973 +on x=-76617..-70495,y=19494..45487,z=13388..34241 +on x=-75446..-46660,y=431..33240,z=40504..57409 +on x=1543..30060,y=-81196..-69614,z=3020..9042 +on x=-18251..-10130,y=-58331..-51994,z=-60581..-38884 +on x=66251..75474,y=-37181..-18094,z=-3742..13757 +on x=35485..50056,y=-12721..9840,z=47834..77651 +on x=62694..75245,y=-21368..4500,z=20741..32850 +on x=-75016..-60592,y=-38855..-30438,z=16871..39322 +on x=-61131..-40040,y=44401..66363,z=-32901..-5525 +on x=2055..28830,y=55873..80956,z=32494..48324 +on x=-5265..17194,y=-83183..-59325,z=13202..17470 +on x=-70307..-50148,y=-27205..-1188,z=-69380..-52520 +on x=-21370..543,y=13724..48061,z=57478..71969 +on x=-76385..-53780,y=-60960..-38839,z=-39620..-21794 +on x=32400..38571,y=-74410..-52196,z=-9470..201 +on x=-38861..-31110,y=-80914..-54541,z=11829..30050 +on x=63874..77010,y=23872..49561,z=-7752..3084 +on x=-48030..-22878,y=-53255..-37302,z=-59114..-47148 +on x=-63103..-48650,y=-50918..-35572,z=32128..46900 +on x=68411..86462,y=-39632..-19086,z=-19878..7685 +on x=1828..26148,y=-56036..-37523,z=50147..68351 +on x=-9724..5895,y=-5172..32358,z=76776..96915 +on x=-38932..-16881,y=-16146..5456,z=67509..81030 +on x=-16338..7196,y=-79105..-57072,z=-42657..-21321 +on x=61233..83455,y=6098..18960,z=12323..31016 +on x=9042..30357,y=65461..76250,z=29733..37437 +on x=23380..42597,y=38600..59064,z=-59247..-44272 +on x=39973..67058,y=-14206..16528,z=52355..65297 +on x=-47021..-26446,y=9946..20783,z=49418..83591 +on x=-75141..-53881,y=-55869..-45372,z=-28360..-4117 +on x=45026..60406,y=-53381..-33802,z=27691..57938 +on x=-73720..-65458,y=-21860..492,z=-53511..-24601 +on x=60212..72693,y=-37762..-35999,z=-42815..-26983 +on x=-30258..-2620,y=-22581..-4543,z=71856..82036 +on x=-81917..-67231,y=16207..48891,z=16921..35789 +on x=59790..75355,y=-6758..-3820,z=-50118..-30498 +on x=-23723..197,y=23771..51186,z=70364..71952 +on x=-27373..-2691,y=42551..53579,z=50413..75951 +on x=-36099..-13006,y=55250..83420,z=4309..17794 +on x=69060..95622,y=-10128..-951,z=-795..22186 +on x=56127..85787,y=-30037..-21119,z=-35307..-11321 +on x=-33032..-9336,y=-30837..-22229,z=-73300..-68041 +on x=59597..67022,y=-64506..-37160,z=-9847..15021 +on x=9970..23688,y=-43936..-29323,z=64977..68927 +on x=-3914..26168,y=-47..11869,z=-82627..-75284 +on x=-29133..-13827,y=-48029..-35305,z=54780..71029 +on x=7394..32695,y=73292..87416,z=-9660..16079 +on x=-72894..-55982,y=39964..60385,z=-18363..8239 +on x=-53059..-42742,y=-39424..-24726,z=36268..68568 +on x=-49419..-31464,y=-73913..-49406,z=-46601..-25505 +on x=-38654..-10260,y=-85131..-70803,z=10832..24274 +on x=-17968..5655,y=-31483..726,z=66336..88777 +on x=-29014..-8520,y=-16871..18131,z=-93226..-60814 +on x=-78874..-55250,y=-49821..-33482,z=3107..27213 +on x=-92556..-76869,y=-6668..-364,z=15270..27386 +on x=-22215..-7196,y=-7303..15077,z=60389..90959 +on x=-93390..-74504,y=-2241..16233,z=-12209..5132 +on x=21904..41801,y=-73000..-54183,z=-21711..814 +on x=-8278..10978,y=-67977..-46914,z=-64187..-49790 +on x=4239..22956,y=-58223..-40911,z=-59585..-45385 +on x=-4748..10534,y=-83544..-57441,z=-54311..-27710 +on x=-50658..-22001,y=-44731..-40535,z=-74296..-52635 +on x=-73371..-51231,y=41705..55729,z=-2136..19712 +on x=48243..77179,y=3412..14317,z=31248..60862 +on x=-44765..-27702,y=29871..53988,z=-63811..-50932 +on x=-17034..5077,y=29452..33591,z=58227..92536 +on x=29547..56695,y=18622..44417,z=-72529..-47628 +on x=-69378..-56446,y=3396..26243,z=-51428..-20265 +on x=-37272..-20491,y=62540..75188,z=-34300..-21161 +on x=-75714..-41150,y=-48120..-24177,z=-44886..-40122 +on x=-88051..-70106,y=-17143..-2101,z=12879..34712 +on x=-24044..-419,y=69387..92418,z=-36321..-7590 +on x=-39626..-18360,y=-72459..-56902,z=-40206..-29716 +on x=-40072..-7959,y=-81166..-49297,z=37946..53588 +on x=-19880..-8493,y=-79108..-58905,z=-31425..-14365 +on x=-59075..-38677,y=-16567..4053,z=58068..78602 +on x=50543..73261,y=-61665..-50628,z=11045..35168 +on x=20733..38626,y=-53298..-49643,z=31925..53213 +on x=31450..53471,y=23956..57055,z=-66313..-47665 +on x=-22847..12046,y=41380..60616,z=59682..79353 +on x=25374..56608,y=-16960..4916,z=-85310..-55978 +on x=46454..61094,y=9436..31666,z=43529..66722 +on x=-71259..-52917,y=35270..57533,z=-7536..7620 +on x=67101..92182,y=-14652..22663,z=-30737..-15519 +on x=-34646..-5218,y=-51561..-44384,z=-77546..-51548 +on x=-28631..-4530,y=-22864..-8159,z=64317..95016 +on x=18905..29015,y=-88901..-62496,z=-10474..923 +off x=14731..49270,y=-84487..-50114,z=-51741..-24666 +on x=36523..50234,y=-48798..-19725,z=32659..68320 +on x=-3881..18286,y=1695..27599,z=-94451..-75599 +on x=1564..16942,y=62339..91951,z=839..30149 +on x=20304..36064,y=3459..38319,z=66965..92109 +on x=10530..44290,y=-45612..-34771,z=54526..80647 +on x=28855..39394,y=64446..79461,z=-22155..2732 +off x=-33928..-27582,y=33621..39729,z=-83053..-50096 +off x=-72831..-49562,y=-11141..14696,z=-48377..-30813 +on x=-1514..7888,y=4627..28889,z=-90790..-72548 +off x=27257..48714,y=-80852..-56188,z=10667..39106 +off x=-23354..-4251,y=59966..78809,z=34709..54372 +on x=-35282..-16997,y=-56317..-24933,z=-62805..-56245 +on x=-2227..17847,y=8470..38424,z=-78695..-65213 +off x=60896..72927,y=35024..55913,z=-32775..-16911 +off x=-64864..-42920,y=-17407..-54,z=-72066..-44353 +on x=-46157..-27456,y=57262..78720,z=-20145..4131 +off x=43849..77466,y=-58778..-43963,z=-14159..10415 +on x=-23828..-16150,y=-87866..-75490,z=11407..16939 +off x=12164..33665,y=-6400..10238,z=-87979..-63401 +on x=23615..55833,y=7453..18457,z=-85920..-57350 +on x=-40675..-26911,y=48362..56763,z=-54066..-43705 +on x=-72738..-51291,y=-2891..15976,z=44235..58189 +on x=49207..81951,y=-24926..-9957,z=-42240..-41508 +off x=-57161..-27129,y=-73081..-55256,z=23321..40263 +on x=-60626..-45750,y=45993..59627,z=7283..31969 +on x=-54424..-37511,y=-73156..-68044,z=617..20695 +off x=61577..83239,y=-30533..4182,z=19445..39734 +off x=-44142..-36940,y=-18137..-11026,z=57620..87726 +on x=39838..60496,y=-62187..-29792,z=26209..36906 +on x=5260..33769,y=73133..86434,z=-24525..-11423 +on x=-36986..-20984,y=69578..79995,z=-18380..9919 +on x=-4107..12679,y=-36148..-8537,z=-77473..-70965 +on x=13785..32581,y=47182..57084,z=-73051..-37330 +off x=-13673..6927,y=-87455..-64845,z=9385..21722 +on x=-7488..3867,y=-79234..-59075,z=38258..57402 +off x=39494..54675,y=27851..46588,z=-54082..-50869 +on x=385..27692,y=3728..33216,z=67955..84805 +off x=-58671..-25964,y=-29442..-10127,z=59383..75507 +off x=-50276..-27994,y=-14406..2283,z=59649..77291 +off x=-68879..-47043,y=-69011..-39870,z=-9656..3781 +off x=-99286..-61487,y=-13924..19764,z=-515..7798 +off x=50555..62847,y=45129..70075,z=15519..29885 +on x=-42096..-29655,y=-66843..-42930,z=45201..52617 +on x=-50756..-39612,y=49926..80383,z=12310..24441 +off x=-3553..21102,y=-77079..-40343,z=-58589..-37948 +off x=55423..69667,y=35605..61318,z=-37529..-9051 +on x=-88304..-70427,y=-2975..3327,z=-3023..20432 +off x=27110..48238,y=20429..33447,z=-69513..-56847 +off x=-70408..-53349,y=-55870..-28425,z=13136..40077 +on x=48007..76031,y=-43660..-18374,z=34610..43102 +on x=73260..86617,y=-1983..22849,z=6331..35201 +off x=-761..17563,y=64663..80718,z=15966..41641 +off x=-84860..-61114,y=20448..26796,z=-12883..13181 +on x=63639..78218,y=-16977..1308,z=27008..46964 +on x=76372..99554,y=-7769..14001,z=-16385..13079 +on x=26077..44013,y=-38989..-13822,z=49410..82519 +off x=-88969..-67232,y=12375..30961,z=-21281..-14983 +off x=-5679..12231,y=-67548..-40613,z=-67084..-50655 +on x=42574..61455,y=-66165..-41420,z=1108..10164 +off x=-94411..-63824,y=-18028..11154,z=-2723..21381 +off x=23966..41695,y=-76794..-66939,z=-38633..-19803 +on x=-74432..-40535,y=-53173..-44420,z=10148..32370 +off x=50456..52963,y=-71445..-46802,z=-3674..2428 +off x=-63251..-38375,y=-21573..-14697,z=-61030..-50647 +off x=-9040..17325,y=-84203..-51999,z=-47776..-28636 +off x=47829..60033,y=37452..52950,z=19921..47713 +on x=4092..10774,y=-76148..-60505,z=-30759..-19411 +on x=-19582..-7278,y=50458..74608,z=-57030..-41393 +on x=-30012..-8293,y=-58676..-29428,z=-69030..-44855 +off x=-68617..-53489,y=-63251..-31395,z=-3656..25906 +off x=-46677..-34554,y=53900..73260,z=-53285..-21932 +on x=36566..56112,y=-78527..-45595,z=-11235..6797 +off x=12140..25848,y=19063..26359,z=-82256..-57662 +on x=-86928..-64851,y=-16250..928,z=-55822..-28505 +off x=58075..85878,y=-348..16123,z=35184..49161 +off x=63936..88315,y=-23152..-16516,z=4477..23650 +on x=40738..61070,y=-236..3665,z=-68195..-55697 +off x=-41602..-27710,y=-73006..-48646,z=-47815..-40020 +off x=53913..77912,y=-56565..-29880,z=4686..27374 +on x=56518..74434,y=23660..28530,z=-39444..-24110 +off x=31245..38156,y=-41849..-25016,z=-80947..-45648 +off x=-89747..-63224,y=-39495..-11265,z=-2950..16591 +off x=35157..61351,y=-66491..-49632,z=-6433..21343 +on x=21541..49495,y=48698..53352,z=-56567..-49125 +on x=-62163..-35676,y=-61226..-38709,z=26288..30690 +off x=69678..79191,y=12568..14636,z=-18574..3692 +on x=-39899..-9054,y=19150..30502,z=62489..91883 +off x=-71869..-48062,y=-49812..-32215,z=33163..53311 +on x=26941..53838,y=-1303..21909,z=56402..86047 +on x=50550..68782,y=-44952..-39608,z=-60228..-29559 +off x=50646..61197,y=-67049..-46828,z=-51485..-22749 +off x=36842..62795,y=-34822..854,z=49658..64445 +on x=35847..47428,y=-79651..-62416,z=-900..22992 +off x=-61406..-38742,y=-74001..-59858,z=6310..32696 +on x=-84111..-58275,y=-19309..-375,z=33607..55412 +on x=-36737..-19900,y=60080..86363,z=-6260..9897 +on x=53693..89030,y=-43427..-16229,z=3869..31797 +on x=28324..53073,y=22609..50161,z=-67145..-54434 +off x=55949..65761,y=-29545..-13501,z=-64713..-32859 +off x=-96236..-59884,y=-11717..27654,z=17302..18944 +on x=-43618..-26100,y=56513..59228,z=-52987..-34324 +off x=-50881..-19804,y=-10923..12100,z=56244..74652 +on x=56146..75267,y=-53449..-42648,z=8110..18495 +off x=63899..85556,y=11701..18829,z=18988..43945 +off x=-40653..-30108,y=58184..77244,z=11725..23530 +on x=-60737..-32507,y=-67499..-49484,z=23919..45578 +off x=3752..24483,y=-72786..-48857,z=-66342..-45354 +off x=35369..64973,y=17707..38166,z=36260..57833 +on x=16635..33210,y=-68338..-33314,z=-75064..-38351 +off x=41960..56487,y=50836..74269,z=14745..50757 +on x=9727..44306,y=-19440..9568,z=-75073..-66244 +on x=-15357..838,y=31431..49207,z=54685..72307 +on x=1917..33121,y=-93589..-65219,z=-6017..12682 +on x=-31369..-15085,y=-19253..-5193,z=73309..92445 +off x=44628..50955,y=-57555..-30844,z=-55578..-34925 +on x=-70657..-52916,y=22021..31482,z=24552..39706 +off x=-32490..-21370,y=-72385..-60365,z=22672..45800 +off x=-19688..10295,y=-13286..4000,z=75107..90096 +off x=-27500..-8167,y=-84631..-67703,z=-26448..8903 +off x=-76756..-55653,y=-44765..-16974,z=19305..50451 +on x=-46309..-17419,y=66446..78979,z=-9728..11074 +on x=-26157..-13355,y=-45399..-40164,z=61445..77979 +on x=-87604..-65296,y=-41115..-21388,z=-7099..14239 +on x=40633..54882,y=-78910..-48822,z=-23752..-10051 +off x=-36336..-17656,y=-1156..13130,z=54373..76857 +on x=-63819..-53538,y=-57090..-45025,z=-29722..-10535 +on x=57011..91186,y=11756..32448,z=-10867..9254 +on x=-22364..-5307,y=20622..37876,z=58946..86117 +on x=-59980..-31720,y=-47670..-37234,z=44881..52209 +on x=49265..64801,y=46991..53469,z=27257..34340 +off x=53351..71717,y=338..16177,z=29927..48599 +on x=-38570..-14069,y=14728..25621,z=60304..83577 +off x=38198..52088,y=-67408..-43440,z=31808..39413 +on x=64754..74363,y=-54904..-36354,z=-17697..4749 +on x=-87028..-62151,y=1846..10714,z=25186..30315 +off x=13716..21776,y=-42101..-21980,z=-88304..-61130 +on x=-74326..-49738,y=-54477..-23790,z=15316..39169 +off x=-3996..23870,y=-26610..966,z=-85461..-74626 +on x=34842..46245,y=-63089..-48763,z=-53242..-24181 +off x=-2564..21696,y=-78933..-70900,z=-33487..-6437 +on x=-77436..-52434,y=-43271..-37281,z=-20012..14207 +on x=-57968..-32018,y=57320..82092,z=-685..25354 +off x=62375..84306,y=-4431..19734,z=-53910..-36550 +on x=44282..64354,y=54385..60505,z=12380..31235 +on x=-31759..-9099,y=-70774..-44052,z=-68587..-35980 +on x=51420..71131,y=26676..47714,z=-25438..2278 +on x=-45378..-26445,y=-29567..-20067,z=52655..65744 +off x=-54492..-41001,y=21779..28365,z=-65457..-51298 +on x=26802..57812,y=18839..47919,z=60875..73925 +off x=43350..69089,y=52449..69930,z=-10726..5245 +off x=-36340..-8137,y=-81759..-71529,z=-8584..1026 +off x=-41220..-15645,y=59471..73882,z=-22939..-9672 +on x=46739..71438,y=-67442..-39101,z=18873..23039 +off x=-60109..-39925,y=-69091..-62600,z=1987..25605 +off x=-20539..-10602,y=67786..89947,z=18242..32090 +off x=-17486..566,y=-37789..-18295,z=-89004..-65327 +on x=73694..79961,y=-22703..-2905,z=-1455..25008 +off x=32398..58652,y=-40166..-28747,z=46861..65868 +off x=-21486..-566,y=-93693..-62694,z=14592..26164 +off x=-75971..-56350,y=-22828..-280,z=-31029..-12846 +on x=13459..45768,y=-64642..-58315,z=-54124..-31850 +off x=-66965..-46502,y=-20216..2758,z=38423..48301 +off x=-60396..-27257,y=57130..80378,z=-8515..-538 +on x=-38572..-9482,y=-71712..-59943,z=-44950..-16801 +on x=-40911..-36074,y=-22661..-22165,z=62296..71986 +on x=-28920..-27155,y=-17625..-8342,z=54287..75009 +off x=24528..42887,y=68297..76197,z=10121..23017 +off x=-6241..18629,y=-92557..-73620,z=-6434..21836 +off x=-37174..-11800,y=25936..41352,z=61640..72778 +on x=-39468..-20408,y=-86032..-63751,z=8944..25611 +on x=-95385..-58672,y=-13317..7950,z=13973..31134 +on x=26506..47467,y=13884..33644,z=-82085..-55283 +off x=9931..29351,y=-21038..2020,z=73505..79532 +on x=-8542..1700,y=-39514..-9338,z=67916..80047 +off x=-58352..-45804,y=-62065..-52580,z=-11506..8422 +off x=-47052..-21418,y=-78922..-55148,z=4620..28133 +off x=-3055..13383,y=51213..79836,z=-55657..-22114 +off x=-58931..-33924,y=-10129..3414,z=-61569..-59313 +on x=-43539..-35924,y=67026..78136,z=-1835..15128 +on x=-84912..-70146,y=27256..36775,z=-12002..12405 +off x=-33768..-6824,y=55884..82639,z=22900..39117 +off x=-7963..11605,y=4762..20586,z=72897..95863 +off x=-27166..-1053,y=75934..91899,z=-3221..21564 +on x=58624..85880,y=-44030..-24644,z=-17819..-6406 +off x=11480..26782,y=76293..97455,z=-1115..21654 +on x=-88461..-70399,y=-17656..6894,z=30129..35244 +on x=27351..39603,y=47868..68849,z=-63370..-34989 +on x=71151..85607,y=2620..29746,z=-27629..-2110 +off x=23934..39205,y=-59694..-41732,z=50604..63849 +off x=12280..40332,y=-88699..-64334,z=-21555..-8697 +off x=-18979..2769,y=-67677..-39972,z=-65859..-60216 +off x=-53659..-23466,y=-62413..-32160,z=34013..60913 +off x=-11238..19640,y=26217..52206,z=56252..79692 +off x=46704..59211,y=-41361..-13454,z=-67118..-47016 +on x=53995..89863,y=-21371..-4249,z=15123..37907 +on x=67058..80641,y=-29949..-12598,z=-12532..11852 +off x=-12791..9642,y=-39314..-26020,z=-93039..-58541 +on x=43167..47607,y=25154..49498,z=43859..69861 +off x=-11454..-5139,y=63917..92333,z=3879..12251 \ No newline at end of file diff --git a/day-22/sample.txt b/day-22/sample.txt new file mode 100644 index 0000000..2790bed --- /dev/null +++ b/day-22/sample.txt @@ -0,0 +1,60 @@ +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 \ No newline at end of file diff --git a/day-22/src/main.rs b/day-22/src/main.rs new file mode 100644 index 0000000..e279ec8 --- /dev/null +++ b/day-22/src/main.rs @@ -0,0 +1,328 @@ +use aoc_lib::*; +use tinyvec::{array_vec, ArrayVec, TinyVec}; + +aoc_setup!(Day22, sample 1: 474140, sample 2: 2758514936282235, part 1: 568000); + +#[derive(Debug, Clone, Copy)] +pub struct Instruction { + state: bool, + region: Region3D, +} + +impl Instruction { + pub fn parse(line: &str) -> Self { + let (state, line) = line.split_once(' ').unwrap(); + + Self { + state: state.len() == 2, + region: Region3D::parse(line), + } + } +} + +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] +pub struct Region3D { + range_x: Region1D, + range_y: Region1D, + range_z: Region1D, +} + +impl Region3D { + pub fn parse(text: &str) -> Self { + let mut parts = text.split(','); + Self { + range_x: Region1D::parse(&(parts.next().unwrap()[2..])), + range_y: Region1D::parse(&(parts.next().unwrap()[2..])), + range_z: Region1D::parse(&(parts.next().unwrap()[2..])), + } + } + + pub fn is_empty(&self) -> bool { + self.range_x.is_empty() || self.range_y.is_empty() || self.range_z.is_empty() + } + + #[must_use] + pub fn clamp_to_init(self) -> Self { + Self { + range_x: self.range_x.clamp_to_init(), + range_y: self.range_y.clamp_to_init(), + range_z: self.range_z.clamp_to_init(), + } + } + + // can return up to 3^3 values, if split in the center + // for performance reasons, only store up to 4 items on the stack (common case) + // this can be increased to 27 if needed + pub fn split_excluding_self(self, other: &Self) -> TinyVec<[Self; 9]> { + self.range_x + .split(&other.range_x) // split x range + .into_iter() + .map(|new_region_x| Self { + range_x: new_region_x, + ..self + }) // turn split x ranges into + .flat_map(|x_chunk| { + self.range_y + .split(&other.range_y) + .into_iter() + .map(move |new_region_y| Self { + range_y: new_region_y, + ..x_chunk + }) + .flat_map(|y_chunk| { + self.range_z + .split(&other.range_z) + .into_iter() + .map(move |new_region_z| Self { + range_z: new_region_z, + ..y_chunk + }) + }) + }) + .filter(|region| !region.fits_inside(other)) + .collect() + } + + pub fn fits_inside(&self, other: &Self) -> bool { + let ret = self.range_x.fits_inside(&other.range_x) + && self.range_y.fits_inside(&other.range_y) + && self.range_z.fits_inside(&other.range_z); + ret + } + + pub fn intersects(&self, other: &Self) -> bool { + self.range_x.intersects(&other.range_x) + && self.range_y.intersects(&other.range_y) + && self.range_z.intersects(&other.range_z) + } + + pub fn size(&self) -> usize { + self.range_x.size() * self.range_y.size() * self.range_z.size() + } +} + +#[derive(Default, Clone, Copy, PartialEq, Eq)] +pub struct Region1D(isize, isize); + +impl Region1D { + pub fn parse(text: &str) -> Self { + let (a, b) = text.split_once("..").unwrap(); + let pair: (isize, isize) = (a.parse().unwrap(), b.parse().unwrap()); + Self(pair.0.min(pair.1), pair.0.max(pair.1)) + } + + pub fn is_empty(&self) -> bool { + self.0 > self.1 + } + + #[must_use] + pub fn clamp_to_init(self) -> Self { + Self(self.0.max(-50), self.1.min(50)) + } + + /// Splits the Region1D into pieces. One of the regions will perfectly cover the other region. + // TODO: rewrite to be cleaner using inclusiverange::contains? + pub fn split(self, other: &Self) -> ArrayVec<[Self; 3]> { + if other.1 < self.0 || other.0 > self.1 { + // other is definitely outside + array_vec![[Self; 3] => self] + } else if other.0 <= self.0 && other.1 >= self.1 { + // other completely covers us (ie. we are contained in them) + array_vec![[Self; 3] => self] + } else if other.0 >= self.0 && other.1 <= self.1 { + // other is definitely inside + array_vec![[Self; 3] => Self(self.0, other.0 - 1), *other, Self(other.1 + 1, self.1)] + } else { + // other partially overlaps, covering one of our edges + if other.0 < self.0 { + // other covers left edge + array_vec![[Self; 3] => Self(self.0, other.1), Self(other.1 + 1, self.1)] + } else { + // other covers right edge + array_vec![[Self; 3] => Self(self.0, other.0 - 1), Self(other.0, self.1)] + } + } + } + + pub fn fits_inside(&self, other: &Self) -> bool { + self.0 >= other.0 && self.1 <= other.1 + } + + pub fn intersects(&self, other: &Self) -> bool { + (self.0..=self.1).contains(&other.0) + || (self.0..=self.1).contains(&other.1) + || (other.0..=other.1).contains(&self.0) + || (other.0..=other.1).contains(&self.1) + } + + pub fn size(&self) -> usize { + (self.1 - self.0 + 1) as usize + } +} + +impl std::fmt::Debug for Region1D { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "({}..{})", self.0, self.1) + } +} + +#[derive(Default)] +pub struct RegionCollection { + regions: Vec, +} + +impl RegionCollection { + pub fn add_region(&mut self, to_add: Region3D) { + self.segment_regions_to_fit(to_add, true) + } + + pub fn remove_region(&mut self, to_remove: Region3D) { + self.segment_regions_to_fit(to_remove, false) + } + + fn segment_regions_to_fit(&mut self, to_fit: Region3D, add_afterwards: bool) { + // for each region that intersects with the region to be fit: take it out, split it up, put the pieces back in + for i in (0..self.regions.len()).rev() { + if self.regions[i].intersects(&to_fit) { + let new_regions = self.regions[i].split_excluding_self(&to_fit); + + // PERF: can get rid of this remove + self.regions.remove(i); + self.regions.extend_from_slice(new_regions.as_slice()); + } + } + + if add_afterwards { + self.regions.push(to_fit); + } + } + + pub fn total_size(&self) -> usize { + self.regions.iter().map(|r| r.size()).sum() + } +} + +pub struct Day22; + +impl AdventOfCode for Day22 { + type Input = Vec; + type Output = usize; + + fn parse_input(s: &str) -> Self::Input { + s.lines().map(Instruction::parse).collect() + } + + fn solve_1(input: &Self::Input) -> Self::Output { + solve(input, false) + } + + fn solve_2(input: &Self::Input) -> Self::Output { + solve(input, true) + } +} + +fn solve(input: &[Instruction], solve_outside_init: bool) -> usize { + let mut regions = RegionCollection::default(); + + for &instruction in input { + let region = if !solve_outside_init { + instruction.region.clamp_to_init() + } else { + instruction.region + }; + + if !region.is_empty() { + if instruction.state { + regions.add_region(region); + } else { + regions.remove_region(region); + } + } + } + + regions.total_size() +} + +#[test] +fn test_region_size() { + assert_eq!(9, Region3D::parse("x=0..2,y=3..1,z=1..1").size()); + assert_eq!(198, Region3D::parse("x=10..20,y=-1..1,z=0..5").size()); +} + +#[test] +fn test_split_region1d() { + assert_eq!( + [Region1D(5, 10)], + Region1D(5, 10).split(&Region1D(20, 30)).as_slice(), + "outside" + ); + + assert_eq!( + [Region1D(5, 10)], + Region1D(5, 10).split(&Region1D(5, 10)).as_slice(), + "covers completely" + ); + + assert_eq!( + [Region1D(5, 7), Region1D(8, 10)], + Region1D(5, 10).split(&Region1D(0, 7)).as_slice(), + "covers left" + ); + + assert_eq!( + [Region1D(5, 7), Region1D(8, 10)], + Region1D(5, 10).split(&Region1D(8, 20)).as_slice(), + "covers right" + ); + + assert_eq!( + [Region1D(5, 6), Region1D(7, 8), Region1D(9, 10)], + Region1D(5, 10).split(&Region1D(7, 8)).as_slice(), + "covers center" + ); +} + +#[test] +fn test_split_region3d() { + assert_eq!( + TinyVec::default(), + Region3D::parse("x=2..7,y=2..7,z=2..7") + .split_excluding_self(&Region3D::parse("x=2..7,y=2..7,z=2..7")), + "covers completely", + ); + + assert_eq!( + [Region3D::parse("x=6..7,y=2..7,z=2..7")], + Region3D::parse("x=2..7,y=2..7,z=2..7") + .split_excluding_self(&Region3D::parse("x=0..5,y=0..9,z=0..9")) + .as_slice(), + "covers left side", + ); + + assert_eq!( + [ + Region3D::parse("x=2..7,y=2..7,z=2..3"), + Region3D::parse("x=2..7,y=2..7,z=6..7") + ], + Region3D::parse("x=2..7,y=2..7,z=2..7") + .split_excluding_self(&Region3D::parse("x=0..9,y=0..9,z=4..5")) + .as_slice(), + "covers middle", + ); + + assert_eq!( + 26, + Region3D::parse("x=2..7,y=2..7,z=2..7") + .split_excluding_self(&Region3D::parse("x=4..5,y=4..5,z=4..5")) + .len(), + "covers center", + ); +} + +#[test] +fn part_1_small_sample() { + const TEST:&str = "on x=10..12,y=10..12,z=10..12\non x=11..13,y=11..13,z=11..13\noff x=9..11,y=9..11,z=9..11\non x=10..10,y=10..10,z=10..10"; + let input = Day22::parse_input(TEST); + let solution = Day22::solve_1(&input); + assert_eq!(39, solution); +}