Compare commits

...

21 Commits

Author SHA1 Message Date
afff8dd79f Final 2021 changes. 2022-11-30 18:56:34 -08:00
6cab94ba0b Day 22 part 2 stubbed out 2021-12-23 20:46:56 -08:00
987760422a Day 22 part 1 2021-12-23 20:34:13 -08:00
2400d34336 Day 21 some clippy cleanup 2021-12-23 19:44:52 -08:00
e4e00517f7 Disable broken day 21 part 2. 2021-12-23 19:43:32 -08:00
d56885bd14 Day 21 part 1 and non-functioning part 2. 2021-12-23 19:25:22 -08:00
b67eea9efe Day 20 part 2 2021-12-22 15:07:49 -08:00
fca64c2988 Day 20 part 1 w/ hack 2021-12-22 13:31:18 -08:00
6fbc06d4e1 advent: add helper image class (not currently used). 2021-12-22 12:46:30 -08:00
1b837b8965 Day 18 WIP, soo close. 2021-12-22 11:33:39 -08:00
a31a47b4b5 Day 18 add merge (aka +) capability. 2021-12-21 14:07:45 -08:00
247e4a89a5 Day 18 basic parse/Display implementations. 2021-12-21 14:07:17 -08:00
7a3a9c096a Address cargo clippy 2021-12-17 20:41:20 -08:00
b83d6f081d Day 17 part 2 2021-12-17 20:37:05 -08:00
2848a9ae1f Day 17 part 1 2021-12-17 19:40:54 -08:00
232b2687ca Create advent prelude and cleanup lint by using it. 2021-12-16 20:10:01 -08:00
1fb9a8416d Day 16 part 2 2021-12-16 20:00:41 -08:00
6ba8f3900c Day 16 part 1 rewrite into structured data 2021-12-16 19:28:08 -08:00
8739e5cc91 Day 16 part 1 2021-12-15 23:32:42 -08:00
c7a180dee3 Day 15 part 2 2021-12-15 11:24:42 -08:00
c16e3ad864 Day 15 part 1 2021-12-15 10:37:15 -08:00
29 changed files with 4407 additions and 24 deletions

13
.gitignore vendored
View File

@@ -1 +1,14 @@
**/target/
# Added by cargo
/target
# Added by cargo
#
# already existing elements were commented out
#/target
Cargo.lock

12
2021/Cargo.lock generated
View File

@@ -2,10 +2,22 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "advent"
version = "0.1.0"
dependencies = [
"anyhow",
"aoc-runner",
"aoc-runner-derive",
"pretty_assertions",
"thiserror",
]
[[package]]
name = "advent2021"
version = "0.1.0"
dependencies = [
"advent",
"ansi_term",
"anyhow",
"aoc-runner",

View File

@@ -12,3 +12,4 @@ aoc-runner = "0.3.0"
aoc-runner-derive = "0.3.0"
pretty_assertions = "1.0.0"
thiserror = "1.0.30"
advent = { path = "../" }

100
2021/input/2021/day15.txt Normal file
View File

@@ -0,0 +1,100 @@
1377191657764644549114627284634913412287739519982496231416283824918194961929936311588211113324916282
1212297521448698359953919612341968949351139967911631956522229222119792152999899993551168415419536961
6159423311678979112521371444921299897197276181489988271511963872918146853113979656119891919929116284
4791323871358399412199298219622788641186798927938163199341117218462279651818983841791285198158689197
7974189811899763275179618283116319192597185732127393472591197762491696726534696112661928998124191441
1719457851119351145679896476439455919892412234291392849988759922872162286199995759192522267673971911
5158991981989914259268282912286934836159941437649989252868156886971224151841291227119542781714229192
8971141349643841951211794248819968548427435466191723999244827371882121325261988461388697379557117813
6984596925919191179943678951217187999979717952993237915971469193192562968292161332928141319899991618
9198522521864259381829335129227674372277199124547185988889492259941882883937916158399823156688865128
8124682618753649934148821878377819747891787181829195781346118199594997697952812114668182584998375593
9981691284313278913662888992426344931897231669692112847984979448943849148537789387936982196747899979
4612594287729384862398119828742189442883965266198489994718698924935284181865741287193312419976111121
9698161969149478565386491726144137461938929999938688185784251478539975684351897911814796415999137427
4215198687955128645232416239572133179888263876627217299182179119281398198291519743556311698595119447
4769546216536687838722946279539949718927689379457711267152121289312649499122925941999769894516538948
2995881981397899317151897695171147699149921139717478649199572991745138182938859417981979318636371733
5896265687179618976487519127375146169177832848256796867888967833698488841993835411394159986678877843
3216645889149389762597933279559298615997238172696673733899239658535753129812911379977549791117989658
3884816682163948316281466989293387989351399981758245478789465919897897823972111872243196689493592859
2147411131991983852642238326868847926889726486359749798736947597961361232791761388538288138311778889
9667691879291819996979958298917569848789574811461556979737155191582528998586729496138919417641516671
2985749275368176832969872226792613823962499974291511594718356964791163978998319397955899236233367196
9146792975371119515819629946162595322898172566118389981777412389859448518999793848789641614927396527
3837698153921119789191289266863437969919159469416859886989979288999338293364182598593531435838918299
3116635237718874995349321215895321218981134288293817795799641825791914139998851951282874988993695793
7561593471951437617129518549197936965341984929165166946255778981198999251529198369291988622121996798
5671179846637276839326113991917774391317631914135868839991981526789969485778186774513984993819918858
6829332192876499239259333179131416651652797879989678673653791799313167962195825894623277922675147419
2114199213615191896184775491411521611589987859919419942228661718872131759515219245894537581322719999
9111987791914149296428193389129811532587733939222947217392149842114232514793791618151173592229671281
6995979618889983189931395618837152991881964616562117919875424671693616994916979814989851972258726362
8173982492272918487736387139295771914721973489627891148815174139856171119417695981112277482189839214
1973523966752994411692968746997296995879577697121374597143849499591366213426378939791149178595161638
5585893988624565875416425158849421541992777828132459639548799994998964889296735678462311472353412967
7998888978481899581293841169535632193113981375899864159935724481141949931596889935158262842998335918
3628289787876715955926724188591588897816776996999489661971733199394273281174961857913237989933989451
8999891193589892228558191976973146639339899432418722147824142953482626221311856529949321934951835227
3992588951996859132652698919297445999986186384145475156862729991985721181638964313723299881446598791
8998893814179771279297296539879723788234226489159913739727989917182438292712863462491283593165387584
2225959678947915336339588711833943992739379439179861836978399199426662599832739483128822695786825928
4919938138891315978138689416887291774881364839148591162128739219164535299459734549155337432999197911
5811216519919174895539797554977936679185698239492921912567121774999488711422544391969771432876537978
5239614948192317841478695119921598999373194178187891395921768432788912522293359417821221957119946281
1988479858958131921192883481392499559613188252381615491311198981658653691389656839995274969418186999
7681966495996941919639623988649188782853795834411334478225123731129587739936417679491619562326995611
5163121511288963298982952314191859371657513679589589729491476296199145629795989556991718931869954195
5286192694799561972391914255528729388961162579826999991948753996967849341943671642439977232844987241
4428178895995144247759399167269333681679595491292381917995999298326832893137644125138231529974592499
4818747595975454796832557299789738541316991638259842799695195686599979445783273918859921299915986952
9417754891252219525194996254344713299966647874186411227972277894179442589131199217999933824814123221
4994994141179221782999435468132963989941331328924377879416667894964349919881719919273389195858632919
6976968727729789979799866987389137352126998771769996593346529935239178231461934599332849891191669783
3638279769222728924783398321639372424899579939421986797719284324742197761998188819691989998694456134
7942399772312398643212169162588662393626113494573293938992496629858316948459199523796897485999921378
1618926196928832916821317498771468433845191219257749761298211872798193673922139789349388129994864112
2744672662951116969991384387997932899791711411128942949881741539172615486131619958932849888897989682
8193685169797222169278874619231971178551717869816119199998223293869323667584378393769322171757659961
3567192399993235879219629458779937992841292566997843196897379191646481548121489984711917991295935959
9145791161291867819713918855165912963216971699817988984653272136935995972742126586832589192163991927
8712419729372896439849678162371923271999124418917611596199166288782836898567935828461556972645691618
1974943823672482569954185719922998567599896762236938717767279661182995366114432538799683897379592854
9379716279297768319753269447991272919214661239572849997471968329889977329941157596172896596534356541
6646625991776574551798145872614721797912217571335478976189691399885799159954742823589679431499683381
8989686475467621429635363139819837633819917676359955759915798412948369139162496822357488719729929699
9952575399865114983355711819388989998898752988216171812892716892892635288989999528963419618898418969
5628277482612799192913177936216253978413218756816239842196325888285889223998597986171699813494975765
8787997189686121858117929165777499728392581119799148997676831848921925893394992791762794499675789289
7568442691119995411445659817292988957454727197967556114172439626332319694755129914814595388989917393
1167946915152153284992129919799398949246947728761958125919513931223797949156627271789872543952184319
4333282313991449435292492972979115959364971739574697214551891471125231369421937456949722396783112819
7993297562943921256985797994839951414791377275818117983949117165983469235412826115996393789982931376
1298945898187917593419458849192816235299119793321858952722674788872499636859117952152983654581115748
4878958876835131628931718969837331917173664888187661244567581167587853362752963213489995238858567899
9168679699798852526937699621149596872192558861969279971951421899916958917919199222795984799115181512
8495992724128927671571265912135878347948681836489591199188311363919513913891778977157177888916936997
8999191812299999181826412647458779129464557453899711499291711727199181999313788754481739897894282299
2297358211198319622489958131157137589129796865587176999673399971359991483798893432429642581288291194
9191151249993972592269748389939197998879479776716874389424892989896918395262244556692919498634767192
3146999389982294398923279851895822879874999174675832246619395368959948618376991191857891919997189827
9252644326896871318783941879993798291951189674981419995699216498289849253481984958375343761948696962
4149366989699391314516198739739998365611211146769689837964632818199827733417982911432639733919961118
3785694943346449113313437192173297234317511989486918779179169811959978466519619814382389974795179229
8173193661981438212589321931819226834981393943686781771986927128994261912991939615971363661699439979
8479197349619477856688178917192741661541913841798699288998264161919784358998345986951361544895722194
9387823917552123692884995799334448659591539912212939191659149351819987712961219375817984328598599894
7812964315918866599999396818117119925739213839329419979879298266584522638128949596632628794899927594
2391843348716127619923835446829918293129713997618113899286695811914941791777191781188282864971628831
9869814173464627244763819721441998547833914115357196275866981483721979728829754991183494575248873899
3759525119799512972171139432761121922192889938411511154289933278983814729624198361288155121749742596
2429134344895591883761666874289213982919652654471211641928968826911633151195891488124183473783171928
8886181492349995235524939198716394499923967835658289266169965949434995834857389557293375945691237192
2153231111419541496813818582276229211229793882886697591279261494221346473438333716131521195987982819
9983399491911518719129352817678695921953245885213828259983567431899628715997395359992977155298181325
3474642518564151369499969799513918368919798899185479829989997639246197855513493872738928431872179498
2732318412186196256356897928718985284818126794829779199946899179899192919186578957838955489991999919
5174898514492714279961793617689337727531149659179437978254251988382912951529715877997391742994559149
6441191197877982527547982766164652298155632979453683329949678198587924143899618194194673133812385279
9721933192312647794494392675978994249714237519435528899779968981967867171823197399159123171497951794
9529734972179199615231232651591988769887899696743322386222999813557814511366456767451396998925196619

View File

@@ -0,0 +1 @@
6051639005B56008C1D9BB3CC9DAD5BE97A4A9104700AE76E672DC95AAE91425EF6AD8BA5591C00F92073004AC0171007E0BC248BE0008645982B1CA680A7A0CC60096802723C94C265E5B9699E7E94D6070C016958F99AC015100760B45884600087C6E88B091C014959C83E740440209FC89C2896A50765A59CE299F3640D300827902547661964D2239180393AF92A8B28F4401BCC8ED52C01591D7E9D2591D7E9D273005A5D127C99802C095B044D5A19A73DC0E9C553004F000DE953588129E372008F2C0169FDB44FA6C9219803E00085C378891F00010E8FF1AE398803D1BE25C743005A6477801F59CC4FA1F3989F420C0149ED9CF006A000084C5386D1F4401F87310E313804D33B4095AFBED32ABF2CA28007DC9D3D713300524BCA940097CA8A4AF9F4C00F9B6D00088654867A7BC8BCA4829402F9D6895B2E4DF7E373189D9BE6BF86B200B7E3C68021331CD4AE6639A974232008E663C3FE00A4E0949124ED69087A848002749002151561F45B3007218C7A8FE600FC228D50B8C01097EEDD7001CF9DE5C0E62DEB089805330ED30CD3C0D3A3F367A40147E8023221F221531C9681100C717002100B36002A19809D15003900892601F950073630024805F400150D400A70028C00F5002C00252600698400A700326C0E44590039687B313BF669F35C9EF974396EF0A647533F2011B340151007637C46860200D43085712A7E4FE60086003E5234B5A56129C91FC93F1802F12EC01292BD754BCED27B92BD754BCED27B100264C4C40109D578CA600AC9AB5802B238E67495391D5CFC402E8B325C1E86F266F250B77ECC600BE006EE00085C7E8DF044001088E31420BCB08A003A72BF87D7A36C994CE76545030047801539F649BF4DEA52CBCA00B4EF3DE9B9CFEE379F14608

View File

@@ -0,0 +1 @@
target area: x=88..125, y=-157..-103

100
2021/input/2021/day18.txt Normal file
View File

@@ -0,0 +1,100 @@
[[2,[2,[4,0]]],[6,1]]
[[3,[4,[2,4]]],[[6,9],[6,1]]]
[7,[8,[8,[0,8]]]]
[[[[2,9],5],5],[[[0,1],8],[[7,9],5]]]
[[[[3,0],[7,0]],[[9,6],[1,9]]],4]
[[[0,[4,8]],8],[[[2,1],9],6]]
[[[5,[7,7]],[[9,6],2]],[[[5,8],8],0]]
[[0,3],[[8,2],[6,[2,2]]]]
[[[9,0],[4,[4,7]]],[7,[[9,1],9]]]
[0,[7,[1,1]]]
[[[4,[0,1]],[[1,0],8]],[[[3,9],[0,1]],[[9,1],[8,8]]]]
[[[6,0],3],2]
[[[[4,1],[2,7]],[9,[8,9]]],[[3,0],0]]
[[[[2,4],[8,7]],[9,[9,7]]],[[[2,5],6],9]]
[[7,6],[[4,[2,4]],[3,8]]]
[[7,2],[[8,8],7]]
[[[[6,0],4],[[4,7],4]],[[6,[2,7]],[[6,5],3]]]
[[[[8,8],[7,6]],4],5]
[[0,[[6,9],[7,9]]],[9,5]]
[9,[[[0,4],6],[[7,0],0]]]
[[[[4,4],0],[3,[3,9]]],[[7,5],[5,[7,2]]]]
[[[8,3],[[8,5],[4,4]]],[0,[0,3]]]
[[9,[3,[6,7]]],[[7,0],[[9,2],7]]]
[[[3,7],[[3,6],9]],7]
[[2,[2,[5,7]]],[[[6,4],5],[4,7]]]
[[[[9,0],2],[[4,4],6]],[[[3,2],[5,5]],[[5,9],7]]]
[[[[2,5],4],[8,5]],6]
[[[3,2],[[1,7],5]],[[8,1],[1,[1,2]]]]
[8,[[3,[5,4]],5]]
[[[2,[5,9]],[1,3]],[[[2,3],[8,3]],[[5,1],[8,9]]]]
[[[2,0],[[3,3],[4,7]]],[[[8,7],[7,4]],1]]
[[[[7,4],9],[3,[4,1]]],[[[8,4],5],7]]
[[[[0,2],9],3],[9,[5,3]]]
[3,4]
[[[1,[0,2]],[[9,9],[8,2]]],6]
[[[[2,9],[3,5]],9],[[9,3],[3,[6,7]]]]
[[0,[[4,6],4]],[2,[5,2]]]
[9,[[9,[6,8]],8]]
[3,[[[1,2],[0,9]],[[4,9],1]]]
[[[[8,7],[1,7]],[[2,6],[8,5]]],[3,[[8,0],[6,9]]]]
[[8,[[4,9],7]],[3,[9,4]]]
[[0,[[3,2],[2,2]]],0]
[[[2,7],[[5,7],4]],[[[6,0],[2,1]],[[4,1],[1,6]]]]
[[[[9,6],[0,3]],[[0,6],[0,4]]],[[[3,7],[6,7]],7]]
[[[[1,1],6],[[5,6],4]],[[5,[0,7]],1]]
[[[3,9],[[7,3],[1,5]]],[[[1,2],3],[0,[5,6]]]]
[[[[4,4],[0,5]],6],[[7,[2,0]],6]]
[[[[2,2],6],9],[[[9,1],2],[[8,6],8]]]
[[[[5,0],8],[[5,7],7]],[6,[5,3]]]
[[[[8,2],[8,4]],1],[[1,[7,3]],8]]
[[[[3,2],2],[[4,9],[5,4]]],[[[9,2],4],[5,[6,0]]]]
[[1,[[0,6],0]],[[[1,5],2],[[6,0],[3,7]]]]
[4,[7,[6,[3,3]]]]
[[[0,[2,5]],2],5]
[[[0,[5,7]],9],[[[2,3],[3,4]],[[0,4],9]]]
[[3,1],[[[4,1],9],[[0,5],[8,6]]]]
[[9,[2,0]],[[0,[1,7]],[9,[6,4]]]]
[[[[6,5],5],5],[5,8]]
[[[[2,8],[1,3]],[[5,4],2]],[[[0,8],[5,1]],[9,[5,6]]]]
[[[[6,9],7],[9,7]],2]
[[[[1,7],8],[8,7]],[[[3,5],4],8]]
[[[[1,8],[1,0]],0],[[7,1],5]]
[[[9,[6,8]],3],[[5,1],[4,[8,2]]]]
[[[0,[2,1]],1],[3,[9,[5,5]]]]
[[2,5],[2,5]]
[[[[1,1],[8,3]],[[1,9],[4,9]]],[[5,[4,8]],[[5,0],0]]]
[[[0,7],[[3,4],1]],[[[1,2],[2,9]],[[2,0],9]]]
[3,2]
[[[9,[8,2]],[7,3]],7]
[[[[6,9],9],[3,2]],0]
[[3,[[6,1],8]],6]
[[[[5,9],9],[[4,4],7]],[7,5]]
[1,[[2,8],0]]
[[2,[0,6]],[[[3,3],[0,4]],8]]
[[[[4,8],9],[0,[3,0]]],[[0,[3,1]],[8,[7,4]]]]
[[[6,[8,0]],[0,[8,9]]],[3,8]]
[[[[0,8],[9,4]],[1,[2,0]]],1]
[[7,6],[[[0,2],9],3]]
[[[[1,0],3],2],1]
[[[[1,2],8],5],7]
[0,[[3,0],7]]
[[7,[[0,9],[8,4]]],[[2,0],[[2,8],1]]]
[[[1,8],[[8,1],1]],[3,[8,9]]]
[4,[[3,7],[[5,2],9]]]
[[[[3,8],[2,9]],[3,9]],[[[3,7],[6,9]],[[1,7],2]]]
[9,[[[3,7],9],[[4,9],[8,6]]]]
[[7,[3,9]],[0,7]]
[[[1,6],0],[[7,[8,1]],[6,3]]]
[[[[3,9],3],[[2,6],[8,0]]],[[3,3],9]]
[[[1,2],[1,6]],[[1,[4,2]],0]]
[[[0,[3,0]],2],[[7,[9,4]],[6,8]]]
[6,[[[3,1],1],5]]
[[[3,4],[[5,9],[1,1]]],[[2,[0,1]],3]]
[[2,[[1,5],7]],[0,2]]
[[1,[[6,7],7]],4]
[6,[5,[[3,2],[6,8]]]]
[[[3,9],[[4,0],6]],[8,[3,[5,2]]]]
[[5,[[7,3],[2,2]]],[[7,7],7]]
[[[1,2],[[2,4],[6,1]]],[[0,[4,2]],[[5,7],[2,3]]]]
[[[8,7],8],[[7,[3,6]],[[1,0],4]]]

1054
2021/input/2021/day19.txt Normal file

File diff suppressed because it is too large Load Diff

102
2021/input/2021/day20.txt Normal file
View File

@@ -0,0 +1,102 @@
#.#.#...###..#.##...######..###..#...#.#.#.##.##.#.#.#..##..#..###...#..#.#.#.#.#....##.##..#....##...#..#.###.###.##...####...##....#..#..#.#.###...#.#..##..#.#.......#...###..####..##..##.##.###..#.#...##...#.###.#..##..####.#.......###...####.#.#....#.#.#.#.##.##.####..#..#..##..#......##.....#..#.#..#..#.##.########.........#.###.#####...##.#...####.#..#.#..#....#.##...##.##.#.##.##......####.###.#..##.#..###.##..###.#.###...######.#######...#..##...#.......###..#.####.#.####.#.#......#.#.#.#...#..###..
##.##...#..#....##.####.####..##..#..###.#..##.#...##....##.##..#....##.#.#..........##...####.#.#..
#.....#.#..##.##..#...#..#...####.##..#....#.##.#...####...##...###.#.####.##.#..#.#.....#..#..#.#.#
###.###......#.##...#.##.###.##.#.##..#.#...##.##.#......#...#..####.###.#####.####....#.###.#.#.#.#
..##..###..#...##.##.##.....#.###...##.#.##.####.#...#...##.#..##.##..#.....####..#.#.#.....#.#.#.##
..##.#..##.##....##.#.#.#######...#...#..#....####......##....#..##.##..#.#.####....#...##...#..#.##
###..##.##.#...#.#..##.....#.##.#.#.##.#..#..#.##.#.####.#.....##..#.#..#..##.##.####...####.###..#.
..#..#.###.####.#...###.#..###.#.......#.##.##.........###..#.#########.......#####..#######..#..###
#####.###..##.###.##.....##......##.##..###.##.#.#.#.#####.......####.#.##.#.####.###.#.###..##.####
#...####.##..#.#####.#.###.####.#..#.##..#.##..##..###.#.##.###...#...#.#..#.#.#.###.###.#####.#.#..
..#########.##.#.##.#.###..####.##...####.#...#..#.#.#.#.##.....##.#.###.####.....#...####...#####.#
....####...###.#..#.####...#....#####.##.##..#...###...##.#######..#.#....#..##.#..#.#.#........#..#
..##.#.##.#..#.#..####.#.......#..##...#......#..#.#...##.######.#...#.######.####.###.####.###.#.##
...#.###..####...###########.##.#.#.####.##....#..#...###.#.#######..###.#.###.##..#####.#...#.#.##.
###..##.#.....##.#...##..#.#...##..#...#..#.##....#.#####.#.##..#...##.....#..#...##.....##.#..##..#
..##....#######.##.#..##.#..#..#.#.#....##.####.#..##.#####....##...#..######..####.##.#####....#.##
....#.####.###...#..#.#.##.##.#...#.####.#.##...#..##...#####.#.#....#.####.#..#.#.#.#.#..#..##..#.#
.###.#.##.#####.##.###...#...##.###......#.#.###.##.###.####..##......#...#####.......######.#.#.##.
...#..#..##.#..##.######.....#......#......##..#.##...##.#...#.##########.#.###.####.##..##.......#.
.##.....###.####...##.#....##.##.#.#.##..#..##..##...##...#.##..#..#.#.#.###.#..##...##...####.#..##
##..#.....##..#####.....#....#..#.#####.#..##.#####.#..##...###.####..##..##...#####.#.....#....#.##
...#....####.#.....##.#.#..#..#.#.#.#......#####.#.#.##...#..###.##..#...#.#.###...####.#.##.####.#.
.###.#...##.##....##.#####..##.##.#.#.#..#..#.########...#..####.....#####.####.#.....#..#.#.#..##..
.#..###...##.##......#.##....#...#...#.##...###...#.#..#..#.##.##...##.#..#.#.#.#.##..#.....###.#.##
.#.#...#######..##.##.##......#.#.#..#.#####.#..#.#.##.#...#..#.##.#.###..#.#..###...##.#.#.##..#...
.##...#....####.#...##.#....#.....##.#...#.##....#.#..##..###.#.###.#.#.##.##.#.#####.###....##.##.#
...##...###.#.#.#....#.##.##.###.#.#..#.##.##....#.#.....#......#...##.##.#....##..#...#....#.#..##.
.##..#.#...####.#..####.#.....##.#...#.#...####.......#....#.####.#...#..##..#...##..##.####.#.####.
.#...###....#.#...##....##.##.##.##.###.##.##.###...##.##...#.###.##...#...#...#..####.###..#.#..#..
.##.#..#..#.#####...#.#.#..##...##.####.......######.#.#.#####....####....##.###.##..#.........#.##.
#.###...#.##...###....##.....#...##.#..##.##..##.#...#..#..#.#.###..#.#...#.......#.####..#...###..#
#.###...#.####..#.##....##.#########..#......##.###...#.##.###.#.#.#..####......#####.###..#.#....#.
#.#.....####.#..###..#.....#....#....#####.#.#...#.##..#####.##..#.#####....####.#..#######.##.#..##
##.#..##..##.###...##.###.....#...##....#..#.####.###....##....##.##...###.#.#......####.#..#...####
...#####.#.#....#.#..#..#......###.##.##.##.#....#......#..#.#.##..##.#....##.#.#....#.#.#...#####.#
#....#.#..#.##..#..#.#.##....#..#.##..#..##.#.###.#.###..##..#..#..###.##.....##.#.#....#####.#..#.#
#.#.##.#...#..###.##....#.#...#.#....######....##...#.###..##..#....##.#####..##.#.##.###...##.#.#.#
.##..##.##.####..##.#.#..#.#.###..#.#..#.#.##...#####.##.#.....#.....##....###..#.###.##...#...#...#
###.#.###..##...####.#.#...#.##..#.##.#..#..########.###.###.#.#.###.####.#.#..#..#...#.###....##...
##.##....#....#.##.##.#.#.##.#....##.####.#..##.##.#.##..#.#.##..##.####..#.#.##...#.##...#...#..##.
#.##.##..#......##.#.#.#..#...#.##...#.#..###..#.#.#....##.#...#...###.#..#.###..#.######...######..
#.#......###.#.#..#.......##.#...###.#..###.#.#.###.#.##.....######.##.#.#...#.####...###.#.....####
#######.#.#.##.#.##...#...##..####..#.###....#..#.#...##.#......#..#.........##.#..#.##.#####.#####.
##.###....###.###.####.#......#.#.##.###..#####.###...######....#...#...###.#..#..#.###..#..#.###...
.#.#...#..###.#.###.#..#.#.#####..#.....####.#..##...####..#..#.#....##.##...####...#..##.##...#####
#.###.####.######.....##..##..#.#...#.#####.#..###.#.#.....#.#.#####.....#.#..####..##..####.#.#...#
#...###.####.#.....##.#.###.....#..#..#..#.#.....##...#.#.####..#.#.#.#####..#..#.##...#...##....###
#.#..#####...####.#.#...#.#####.#..#.###..##......##..#....##...#.####...###.###..#..#..#.#.#####...
.##..#.####.###.###..####..###.###.....###..#..##.#..#.###.##..........#.#..#.##..#.###.##...####.#.
#.#.##....#..###..##.#..#......#.#.#.#.#####....####.....#.####..###.#.#.#...####.###..#####.....##.
##....#...#....###.#.###.#..##...#.#..#..##......#.#.##.##..###...#..##.##...#.##..#.######.#..#.#.#
.###..#...###...####..###.#.###.#..####...##.##.#....##.#..#.####....##.####......##.#.#..#..#.#.##.
.###..##...##..#.##......#.#..#.#..#.#..##.#######.###..####...###.####.##.###.#####....##..###.##.#
##..#.#...#####..##..#.###.#..#..##.#...####....#...##.#.##..#.#..#.##.###.#.######..##.#.#.#..###..
..##.##.##......#..#..#.##....##.#.#.###.#..#.#...#.#.#.##.#..##.#.#####.#.#.......#.#....####.#....
....##....#######......##.#..#..#..#...##..##..#..##.#.#.#.##...#######.####.#..###..#.####.####....
#..##....###.##....##.##.#####.######.######.#.#####.#..#....#....#.##..##..##.#...##..##..#.#.#..##
##.##..##.#..#...####.#..##.##...#....#...#.##...#.##..#....##....#..####..##....#..##.####......#.#
.#.##....#######.....#.##.##..####.#.#.##...##..#.#..##...#..##...#.##......####...#...#...#...#...#
..#..#.###..#.#.##.#.#.#.#...###..#.##.##...#.##.....###.#......####........#####..#....#####...#.#.
.###..##..#.##.###.##..###....#.###.#...#.###.####.#..#....##......#.#...#.##...#.##.#.#.####....##.
#..###.#..#...###.##.###..########.##..#...#..##.#..##.##.##..#...#.####..###.#.####.###..##..####..
...#.###..##..#.##.#.#######.###.##.#.###.###...#..#.##..#######.#.#####....#..##.###.###...##..#.#.
####..#.#.######..###...#..#...##.#.#..##.#....##.##.#.#...#....##....####.###.#...#.#...########.#.
...#####..#.#.####.#.#.#..##....#######.##..###.###.##...#.#.###...##.####.#..##..#.#..####.#####...
..###.##.##.##.###.##.#.#.#.##..##..#.#.#.##..#####.##..##.##.##.#.....#.####.#..##.#.####.#........
##.###....###..###..#.#..##..#..###..#.###...#.#.....#.#...#.#..#.#.#....##...#....##.##......#.....
.....##.#.####.######..####..#..####.####.#.#.#.#..#..#.#.#.##.#.##...##..#.##.##.#.#..#.####...#...
#..##.#..##.#####.#.#..#..#.#.#..##..#..##.#.#..##.#.#.#..####.##.#.###...######...##....###.#.#..##
##.#..##.#.#####.##..###.##.##......#####...#.....#..#..#####...#....##.###.....#....###..###.##.#.#
.#.###........##..##.####...###.#.####..#.#.....###..#..#..#..###..#.#.#.####..####.##.....##.###.##
#..####.#..#.#.##..#.....#.####..#.##.#.#...##...#..#..#.#.#.#...####.###.##....#.####.##..###.#.##.
.#...#..#..#####..#....##.#........##..#..#..##.########..#...####.#..#..#..###...##.##...#.........
.####....#.#.#.#..#.#..#.######...#..##.......#..#.##....#.#.#.###.#.##.###..#.#..##.####.#...#..#..
...#.######.###.#.##.#.######.##..##.......#..#.###..#####..#.###..##.##...###...#..#.#..##...#.#..#
#....##.#.#...#.#.##.##.#####.....##..#..##.##...#..#####.#.###...#######...#...#..##.#.###.......##
#.###......#.##.##.#..######.#.##...#.##...#.###...##...#..##....#..###.#.###.#..##..####.##.##.....
..#.#...#.#.####..#.#.#...#..##..##.....#..#..##.#...###....#.#.####...#.##....##...#..#####....##..
####.##...###...#..#.###.#..#.#......##...####.#####..##...##.###.##.##.##..#####.#.###....#...###.#
#.#...###.....#.....#.#.....####.##.#..#..#.#.#.#..###.##...#.#..##.#..##.###.#.#.###....#####.##...
...##.#.####.##..###..###.#...##..##.#.#.#.##.#.##.#.##.#....#...##...####.###..#.##..#..##..##.##.#
###...#...#..#..#..##..#.#..##...#..###....#..#.##.###..#####.#..###..#......##.####..#.#.#....#####
#.#..#.####.##..#......###..#.###....#.#....#..#..#.#.#.....#.##...#..##...##..#..##..#...####...#..
.#.....#.###..##.#.##..##.##..#..#.#..##.#......#.#.#....#####.####....###..###.###...####.#.....###
..##.##...##.#.##.#....##.#.#.....########.##########........#.###..#..#.#.....#.###..#.#.###.###.#.
####.#..##.#..##..#####.#.....###....#.#..#.#..##..#..#....#..####.#.....#..##.#..#.#.###.#.#.##.##.
.##.##....##..#..#.#....###....#.#...##..#..#..##.#..##..##.#..######.#.##..##.#.######..#.#.###..##
###..#..#####.#....#..###.###.##.##..####..#.#....##........#.##..#####...######.##.#...#.#####...#.
.#.#..##.......#..##....#...#.########.#...###.##.#.##.##.##.#.#.#....###..###..#...#...#.#.###...##
.##..######.#.#..#......#...##....#.###.#.##..#.....#...#.##.#..#.#.#..#####.#####.##......##..#.##.
##.#.###....###.##..#.#..####.......######.#...###.#.#.##.#.....####....#......##...###.##.#.#####..
#.###.#.##.##..##..#..#..##...##.#..###.#.#.....####..####..#...#.#.##.###.#..#..#.##...#..##.#.###.
..##.##....##...###..#######..#...##....###.#..###..###....###..##....##.#..##..##..##.#.###.#.#..##
..#.#..#.####.#.#.#..###.###.........#.#.####.##.#.##....##..#.#.#...#.###.#.##.##.#....#.#.#.#..#..
...##....###.##.##.##.##...#.##.....#.###..##....#..#.##...#..#..##..#...#..###.##.##..#.#.#######..
..#.#.#..##.#.#..##.##.##..#...#.##.#..#.#..####..##....#.#.#....###...#..##...##.#..##.##....##...#
...###.########....##...##..#.####..#######.#..####...###......##....#..#....###..###..#...####.##..
.##..###.....#.###.....##.###.####.######.##.#..#.#.###.###.#.#...#.##..#..###.#.#....##.#####....#.
###..##.####.##.####.#.##.#...#.##.#..##.###.#.###..########.#.#..#.###...##.###.....#..#####.#..##.
...##.##.###..#.#####..#.#.#.#..#..#...#...##.....#.##...###.#.#.####...#.....####.##.####...#..###.
#.###..###..###..####..#..##..#########..#...##..#........#..#..#####.###..###..#...#..#..###.#.##..

View File

@@ -0,0 +1,2 @@
Player 1 starting position: 4
Player 2 starting position: 5

420
2021/input/2021/day22.txt Normal file
View File

@@ -0,0 +1,420 @@
on x=-46..2,y=-26..20,z=-39..5
on x=0..44,y=-44..0,z=-19..32
on x=-44..10,y=-20..28,z=4..48
on x=-12..39,y=-35..9,z=-12..36
on x=-20..31,y=-15..36,z=-44..2
on x=-18..36,y=-43..8,z=-12..41
on x=-24..27,y=-5..39,z=-30..24
on x=-40..4,y=-20..28,z=-33..14
on x=-16..30,y=-16..31,z=-12..32
on x=-3..48,y=-27..18,z=-5..39
off x=-37..-22,y=24..41,z=20..38
on x=-33..16,y=2..49,z=-46..4
off x=-9..2,y=29..41,z=-45..-32
on x=-7..44,y=-27..22,z=-40..6
off x=-43..-30,y=17..27,z=-43..-34
on x=-13..35,y=-19..25,z=-45..2
off x=31..45,y=36..49,z=12..28
on x=-16..33,y=-20..27,z=-34..16
off x=-43..-30,y=-1..17,z=2..13
on x=-7..47,y=-8..39,z=-2..44
on x=-51481..-16686,y=-55882..-41735,z=31858..57273
on x=62605..83371,y=-20326..7404,z=721..31101
on x=52622..66892,y=-53993..-42390,z=-32377..-15062
on x=32723..65065,y=-77593..-55363,z=-14922..15024
on x=-19522..-12277,y=-93093..-59799,z=23811..37917
on x=71595..92156,y=-44022..-19835,z=16005..34327
on x=-3586..34080,y=2703..21691,z=-92268..-61619
on x=-15386..715,y=73535..97744,z=-8553..16939
on x=-36770..-25752,y=71882..77753,z=-22483..5934
on x=-87151..-59512,y=-52304..-38677,z=-6844..14195
on x=-49843..-11520,y=-59097..-36023,z=-68112..-44642
on x=-54750..-35235,y=-65545..-48408,z=8090..44796
on x=-94375..-72818,y=9788..29493,z=-9581..832
on x=7678..15728,y=-72950..-37506,z=43639..74189
on x=-2150..1370,y=-47547..-22907,z=67903..86640
on x=30996..54239,y=58993..70521,z=10200..33219
on x=11010..19912,y=-82111..-52326,z=-59754..-28912
on x=21482..32440,y=-86476..-62698,z=14238..24595
on x=75846..79807,y=-34167..-20170,z=-5351..13100
on x=-57020..-37686,y=43544..67110,z=-37496..-30899
on x=14662..49584,y=-58025..-40735,z=-47292..-41465
on x=-63346..-55137,y=21250..38290,z=33568..51615
on x=-85269..-67041,y=-25194..571,z=-52696..-14325
on x=-54438..-31138,y=-25222..3408,z=-74478..-51576
on x=-55260..-36337,y=-36146..-21397,z=52332..57160
on x=-65984..-51756,y=-1513..8161,z=46328..64968
on x=28910..41579,y=-63016..-52977,z=-61128..-33613
on x=-46241..-27998,y=52847..77588,z=6053..16826
on x=-58382..-31024,y=-25378..11770,z=62336..77861
on x=14836..43351,y=43097..66395,z=26797..52340
on x=-2107..15942,y=-94637..-77185,z=-12264..-1486
on x=52176..62331,y=-18190..8633,z=40476..60619
on x=57517..70517,y=-13924..4531,z=46436..67802
on x=-21645..-1991,y=-50529..-11513,z=59680..91420
on x=-60101..-36307,y=11413..32936,z=-79198..-42467
on x=-27245..-11982,y=12680..44262,z=61571..82539
on x=62043..80570,y=-16742..15490,z=41267..54928
on x=-48117..-37581,y=34828..44610,z=-59598..-49709
on x=59522..87726,y=-16741..-1638,z=-37838..-7715
on x=-40860..-21613,y=-63238..-50545,z=38865..68651
on x=-53664..-45002,y=43993..64127,z=-50526..-32706
on x=-71250..-45260,y=-45511..-32954,z=23030..44098
on x=-50714..-25029,y=10683..33392,z=58357..85044
on x=40647..68356,y=40530..46549,z=21996..38584
on x=-63640..-36211,y=46146..49642,z=-56306..-41971
on x=21790..44008,y=-41519..-38626,z=46575..60879
on x=29993..40615,y=-74802..-65958,z=-10835..3294
on x=63509..72412,y=33988..60123,z=17699..27626
on x=-75402..-51635,y=-41551..-19817,z=-28290..-15067
on x=-24611..-7664,y=-76920..-49198,z=-69901..-36628
on x=-55000..-37217,y=-65084..-44457,z=33862..49998
on x=-83578..-56334,y=3079..36144,z=30213..52349
on x=-78623..-56475,y=45055..61546,z=-7798..15403
on x=31042..53339,y=-48138..-27455,z=-74296..-40132
on x=-16338..3820,y=-85143..-57755,z=-35857..-14502
on x=1..30237,y=63222..92071,z=14406..27979
on x=-50768..-29497,y=-62276..-42537,z=31270..66285
on x=14750..33888,y=25707..41545,z=60136..71374
on x=-73804..-51260,y=34623..68995,z=11793..27887
on x=-22099..3107,y=29817..55030,z=-86473..-71182
on x=44846..63690,y=-39204..-30200,z=-70048..-31226
on x=12735..25519,y=-50839..-27301,z=-85035..-52999
on x=-79843..-67300,y=6320..19153,z=-16078..5129
on x=-25488..3381,y=-81495..-59824,z=-51236..-39103
on x=29516..52668,y=-91122..-64377,z=-7299..8614
on x=-45309..-34530,y=59207..85364,z=-5722..17346
on x=-75869..-48145,y=-36278..-31560,z=-48241..-14556
on x=5416..33979,y=71734..85654,z=-6354..17686
on x=-38328..-6825,y=33508..60073,z=-73314..-49127
on x=-16455..5840,y=-90011..-56004,z=12046..34429
on x=19942..40435,y=-38063..-14227,z=-72406..-60179
on x=-71208..-36468,y=-51769..-39093,z=-39553..-32066
on x=-30611..-15238,y=-74996..-51621,z=41626..60792
on x=-26266..-6821,y=-76803..-54508,z=26326..49720
on x=5554..23770,y=60181..92306,z=-3903..22337
on x=35188..60913,y=405..13616,z=-68688..-53839
on x=-84198..-52574,y=-51383..-21298,z=959..28430
on x=-32433..-12722,y=-58750..-50875,z=33399..60295
on x=-42283..-20916,y=60934..71844,z=-48024..-17122
on x=45510..69038,y=-22876..-7766,z=41312..55989
on x=4077..26087,y=-87438..-67786,z=-6454..9574
on x=60630..91396,y=2047..28989,z=8771..31412
on x=-14685..3138,y=34908..62560,z=-66694..-42144
on x=44452..66566,y=-63825..-31278,z=-33877..-24172
on x=36589..53960,y=43341..55297,z=16299..30540
on x=-50464..-33544,y=745..34345,z=-72288..-52398
on x=21307..54983,y=-62869..-43283,z=41406..72552
on x=44587..71433,y=1841..17317,z=-57698..-42925
on x=58150..68363,y=28852..55677,z=17960..29455
on x=-7196..19282,y=-89129..-71321,z=13723..41169
on x=-68501..-46903,y=-26290..-15637,z=35391..42746
on x=3892..29864,y=64133..84173,z=-8782..5299
on x=-43516..-30450,y=26793..49391,z=53089..61207
on x=-53171..-30883,y=-48101..-43726,z=44797..67740
on x=68286..92818,y=-9936..15488,z=-6245..6271
on x=-69288..-35805,y=-18686..12501,z=-63926..-46763
on x=-13676..10358,y=29729..48962,z=-82290..-61499
on x=-91342..-58913,y=5804..18438,z=-10738..20089
on x=-2086..15635,y=62442..80850,z=11045..35648
on x=-75732..-51506,y=-5835..28683,z=-57038..-22560
on x=33708..52638,y=40215..48167,z=-64930..-42437
on x=42559..66539,y=-46203..-29630,z=38529..49019
on x=-49526..-43357,y=-61139..-43371,z=-59437..-20443
on x=8103..28088,y=66204..85143,z=-25951..-13527
on x=-72345..-62556,y=-63706..-38173,z=-17152..-1145
on x=54774..91997,y=-37670..-12831,z=-30161..-17865
on x=6148..25487,y=46052..71759,z=33247..65381
on x=-57328..-49224,y=26458..36097,z=45759..52150
on x=-39304..-28724,y=-39127..-23995,z=50978..73051
on x=-64985..-61846,y=31392..53299,z=25687..50050
on x=63677..86665,y=21170..35031,z=-17997..-9002
on x=-2035..9063,y=-91127..-59256,z=-41298..-26586
on x=-42966..-13481,y=-63752..-52787,z=39934..42013
on x=-4052..16276,y=4300..33419,z=-97023..-69495
on x=28754..55492,y=-63861..-30281,z=-57610..-42673
on x=-75541..-66845,y=-16327..9136,z=-47999..-37146
on x=-95243..-67165,y=8035..18049,z=-8895..24816
on x=-67563..-50842,y=-47960..-21085,z=-60573..-40893
on x=-41648..-12477,y=-20284..2911,z=-81827..-69369
on x=-28144..-17284,y=-34620..-6183,z=-89244..-54720
on x=65922..92404,y=-1466..6914,z=16157..39291
on x=14668..24690,y=-77921..-61312,z=-30488..-1115
on x=36923..56302,y=46079..74765,z=-15871..4979
on x=-46591..-23525,y=-76935..-44182,z=-43573..-20271
on x=29721..45936,y=6887..30073,z=59075..67521
on x=-890..30663,y=23754..47276,z=-82425..-63230
on x=4752..19784,y=-83727..-61899,z=33506..54760
on x=-94526..-71296,y=-14265..-4610,z=11418..33014
on x=58300..97221,y=-16175..4231,z=-21919..-2358
on x=-57482..-45486,y=-67406..-47228,z=20248..37478
on x=31402..48989,y=-62481..-50137,z=22060..47161
on x=40599..50616,y=10759..15911,z=46121..76734
on x=48057..61128,y=35427..62674,z=-21549..-9614
on x=-28135..-20962,y=59633..89312,z=-13540..5340
on x=47549..71482,y=17382..53520,z=17716..45082
on x=78070..81154,y=-13655..13343,z=-168..16596
on x=20673..45758,y=65526..76695,z=-13497..-36
on x=-4786..2818,y=-4837..5549,z=-93451..-76191
on x=14302..49076,y=61818..81437,z=-14176..9531
on x=-68602..-53393,y=16455..38152,z=25132..43913
on x=-24130..-13936,y=-7644..-4498,z=-85828..-59036
on x=-11717..16687,y=10353..30087,z=-80942..-58069
on x=12682..32617,y=58507..76138,z=20849..32759
on x=49088..65243,y=53393..74520,z=-14943..321
on x=-63756..-46629,y=-69037..-41666,z=-15234..-9712
on x=44240..46104,y=1468..21751,z=46542..79291
on x=-80700..-60977,y=24543..33596,z=-6372..11056
on x=-72072..-64358,y=-6985..13991,z=-65594..-35346
on x=-54425..-32669,y=-51345..-34855,z=29309..52921
on x=57189..87162,y=14470..19132,z=-51139..-22474
on x=76017..80222,y=-8175..18033,z=4083..27696
on x=-42368..-31460,y=-50798..-22007,z=-66532..-47444
on x=9223..15360,y=-54293..-43392,z=43665..64825
on x=-50450..-28738,y=-67485..-46021,z=25963..50292
on x=49773..57601,y=40208..61258,z=10697..36254
on x=-87157..-62119,y=-46554..-23602,z=-33740..-21462
on x=24144..48485,y=30406..55581,z=-63251..-41224
on x=-57059..-28465,y=36514..59020,z=-62118..-39872
on x=51418..73395,y=-33294..-7934,z=-55359..-46837
on x=35051..53350,y=48381..69938,z=30992..50675
on x=-49838..-13680,y=52337..71756,z=19703..47005
on x=-37895..-13199,y=17817..26885,z=-85792..-66246
on x=25443..49007,y=55523..83334,z=-38543..-8471
on x=-83264..-53691,y=-50838..-28415,z=-45433..-11937
on x=18129..44224,y=56025..71419,z=25496..28014
on x=64401..91096,y=-9626..9767,z=-19328..-16234
on x=40564..60820,y=21806..27545,z=-63583..-55689
on x=59287..82435,y=-49803..-36598,z=-19736..10678
on x=-61406..-31848,y=29090..48153,z=49953..68273
on x=-80129..-63495,y=32758..49898,z=15420..28891
on x=-71918..-59088,y=-8042..4070,z=-61979..-32744
on x=18507..32332,y=37492..69230,z=-61212..-48361
on x=18232..24748,y=-47573..-19392,z=-77084..-69812
on x=3538..29122,y=49351..68476,z=-77426..-58306
on x=14899..28875,y=33798..52962,z=-74740..-47031
on x=-33805..-9226,y=-4797..32978,z=-87103..-57644
on x=64283..84636,y=22189..47622,z=952..13372
on x=48891..68906,y=11825..36343,z=34507..53080
on x=8785..32809,y=-61933..-45683,z=42536..64070
on x=-23386..12584,y=60365..94330,z=-35407..-19640
on x=-69295..-63793,y=39194..55998,z=-29323..2775
on x=-69311..-40624,y=-11655..12445,z=52893..65442
on x=63480..66888,y=13195..36768,z=-40754..-23113
on x=-71702..-44882,y=-34923..3115,z=36891..68959
on x=1227..20599,y=3304..38907,z=-87371..-69194
on x=-38051..-5608,y=66679..76678,z=-22327..-17200
on x=-71086..-50108,y=-43265..-21453,z=45617..65108
on x=-75499..-48938,y=-22961..2638,z=-62154..-39253
on x=-58844..-35498,y=-73497..-60885,z=-3349..4528
on x=-80442..-65527,y=-51151..-25462,z=14193..28272
on x=-36446..-15525,y=2899..19438,z=54499..93589
on x=21122..41146,y=-34781..-12126,z=-81333..-47863
on x=40057..68736,y=12318..42116,z=-62457..-51443
on x=14076..29019,y=44823..82163,z=29271..59964
on x=-49251..-25009,y=47681..66203,z=-41367..-27670
on x=-62329..-39224,y=-34111..-4674,z=-65314..-57930
on x=-61408..-31233,y=22748..44986,z=41804..72595
on x=-21267..231,y=-85144..-60581,z=-14979..-6323
on x=-74958..-66383,y=-16735..2794,z=34735..59977
on x=-23282..-12409,y=69175..83534,z=313..21732
off x=59267..85411,y=514..23328,z=-4846..14301
on x=-17178..9607,y=-64979..-33229,z=57298..68101
on x=-7419..15896,y=-89840..-63418,z=-31002..-8653
off x=-73365..-57136,y=7908..35149,z=-59465..-32345
on x=63815..89172,y=6116..28592,z=11336..49089
on x=13237..36417,y=-44604..-18476,z=58867..71392
on x=-31835..-2018,y=48122..51530,z=-79384..-51387
on x=-93617..-71409,y=-6592..18534,z=-3366..22689
off x=29296..37825,y=29193..53548,z=-76978..-56558
on x=10940..21199,y=-70736..-49011,z=23206..53098
off x=46366..51784,y=-73666..-55961,z=-12714..5969
on x=-54335..-22852,y=-7983..25369,z=-79636..-67406
off x=-24260..2762,y=-64478..-41674,z=-64474..-45494
on x=22187..41667,y=15612..28847,z=58068..87450
on x=63150..83396,y=1346..33053,z=27298..45814
off x=59837..81917,y=-17191..8875,z=5474..24724
off x=19093..34648,y=-93372..-66153,z=-2175..27526
off x=30666..50683,y=52749..78100,z=-17491..10648
on x=26870..45665,y=56112..77327,z=-43808..-31865
off x=11864..18598,y=-1684..25669,z=-97875..-71735
on x=53157..75832,y=-29574..-16764,z=12318..43107
on x=43719..77079,y=-55045..-20872,z=15413..38119
off x=14623..37763,y=-89983..-54806,z=13234..35206
off x=-20732..-6457,y=4017..28816,z=71076..89286
on x=-20590..-14094,y=50358..65175,z=47899..66858
on x=62320..69077,y=23748..44841,z=-7970..22293
off x=21884..39495,y=67007..78871,z=-6077..14897
off x=62186..75230,y=-50181..-16289,z=-14388..7026
off x=-38405..-22937,y=-5429..28417,z=55944..88446
on x=662..26305,y=-87554..-58836,z=-17299..2266
on x=72718..77163,y=-1163..18561,z=-44259..-28618
off x=22260..41297,y=-44311..-19228,z=47468..84313
off x=-40602..-18738,y=-34574..-133,z=64944..85128
on x=-29694..-18496,y=54923..65374,z=38879..55584
off x=72863..81078,y=-14522..-3236,z=-33189..-20138
off x=-42230..-18771,y=69691..87669,z=14085..23774
off x=-9126..9929,y=68147..99240,z=-12694..-3519
off x=-26468..-7104,y=64299..76065,z=-36099..-15599
on x=67973..84722,y=-21303..7452,z=-18666..2678
on x=4102..21407,y=15532..19634,z=58771..86790
on x=37549..65086,y=-7487..-2758,z=55748..74491
on x=-14811..4584,y=-74652..-68275,z=-38615..-8763
off x=37106..50803,y=-53386..-44103,z=-58613..-40526
off x=-74450..-40998,y=43771..58392,z=1444..19278
on x=-50536..-28522,y=310..5308,z=58703..88882
on x=-20939..3950,y=-19820..-18182,z=-93492..-57274
off x=-73988..-70744,y=-53683..-27500,z=2877..9737
on x=-7526..22863,y=-8072..17539,z=73403..87835
off x=-48736..-25834,y=-83795..-55637,z=-14055..-1592
on x=46004..70445,y=11832..29476,z=43772..50920
off x=62244..84049,y=13880..25447,z=33699..42878
off x=-65300..-54528,y=47126..50687,z=7884..28591
on x=-55956..-47412,y=-68950..-31938,z=-33567..-21955
off x=27523..54204,y=53311..66537,z=-41809..-5196
on x=-95373..-75620,y=-27024..1838,z=3422..7457
off x=-41424..-13493,y=-93359..-55448,z=-26642..-13733
off x=45093..70424,y=51183..64547,z=-28549..-12261
off x=32082..52590,y=-68034..-42190,z=24265..40002
off x=-83441..-70495,y=2611..23905,z=-3669..20088
off x=-5850..22395,y=11248..36413,z=-78880..-56272
on x=78982..83822,y=-14121..143,z=-15875..5994
off x=39610..65471,y=39412..61214,z=17938..53604
on x=-73170..-48249,y=-48894..-33070,z=26483..60838
on x=-68144..-52978,y=-23980..-6621,z=-59936..-34090
on x=41115..53204,y=26168..46031,z=-63732..-34602
off x=-47255..-20253,y=5004..35879,z=49163..83111
off x=-63225..-49222,y=-62533..-36601,z=-935..13732
off x=-53817..-39048,y=-81526..-62330,z=-25691..-2500
off x=-76260..-62299,y=-29061..-19304,z=39220..48501
off x=-80241..-49003,y=-52126..-30868,z=-9247..6617
off x=6227..11948,y=-32268..-17660,z=-85825..-59021
on x=-36533..-26269,y=-79451..-60932,z=1614..7881
on x=589..25506,y=-12202..3560,z=-85890..-74851
on x=5360..19575,y=-62141..-51745,z=45034..63905
off x=-39349..-23146,y=23975..44390,z=48709..77537
on x=28342..46781,y=-29976..-8233,z=57734..69595
off x=-77303..-50182,y=8314..15684,z=-60271..-36934
on x=15565..23346,y=-24515..5163,z=-77856..-68954
on x=-38928..-18718,y=-26186..-6428,z=63840..76872
off x=9997..35709,y=-60625..-56365,z=-68727..-47677
on x=-6312..17890,y=-10921..5064,z=-91584..-67839
off x=12562..23601,y=-59006..-25156,z=-67383..-62614
off x=56511..85396,y=-9472..21355,z=-49007..-13930
on x=48654..68194,y=2363..22687,z=-54149..-39844
on x=-70180..-49056,y=-43968..-29640,z=21331..49497
on x=375..26280,y=53582..64641,z=45975..73486
off x=-50286..-39551,y=-69893..-49626,z=18452..52094
off x=-55882..-33331,y=344..18448,z=-80801..-51551
off x=57268..84471,y=-48138..-39865,z=-23525..6531
off x=-5837..2735,y=-73830..-52479,z=41854..58096
on x=-27554..-4489,y=53367..83205,z=31562..49332
off x=-20742..5467,y=-65244..-27510,z=53341..76842
off x=17914..42871,y=-53623..-33086,z=-77085..-47173
off x=36492..49968,y=35632..41326,z=-74714..-40178
off x=12446..45639,y=61743..75034,z=-40513..-27030
off x=16145..43992,y=-26882..-8326,z=-82954..-59935
on x=-23775..-7690,y=45368..50716,z=-64252..-49534
off x=-78791..-60343,y=11003..36392,z=-58207..-42229
on x=50675..70130,y=-21834..11787,z=34188..56587
on x=-34986..-11593,y=71125..86822,z=-11765..10487
off x=26121..54204,y=57376..70480,z=-44536..-14084
off x=47869..65894,y=33992..53419,z=-56863..-28538
off x=-24334..-8612,y=28640..61652,z=-78408..-43374
on x=27154..47449,y=43628..62103,z=-48166..-37361
on x=15338..32128,y=66233..90008,z=15361..36731
on x=-48061..-32730,y=7101..33292,z=51441..75575
off x=29688..55006,y=53948..61368,z=31296..38879
off x=59479..82841,y=27639..46701,z=-27632..-16641
on x=33822..55847,y=-60451..-48526,z=19976..54898
on x=-46685..-18938,y=-76878..-56088,z=23616..39907
off x=-74144..-59325,y=11439..22058,z=-39530..-17804
on x=60278..85196,y=-53386..-26378,z=-7422..6296
on x=11602..38478,y=47634..59849,z=-60457..-41436
on x=-67981..-51375,y=-43182..-28399,z=-30170..-5548
off x=-39484..-26626,y=46563..57448,z=-70491..-43484
on x=-11575..6240,y=-44470..-23882,z=-80590..-52319
off x=34368..38954,y=-77664..-45070,z=23154..37589
on x=23476..25973,y=-81946..-53549,z=19755..36372
on x=-70072..-56024,y=-42751..-25176,z=4968..16718
on x=44401..55480,y=-10001..5439,z=60788..71332
on x=-80447..-65355,y=24494..41918,z=-48047..-24893
on x=-25175..-5264,y=59599..83851,z=-43682..-19535
off x=-16008..-9052,y=68012..88912,z=-30931..-23360
on x=3262..31382,y=-59562..-48713,z=-65187..-50358
on x=-38630..-14647,y=-67513..-61283,z=21962..51871
off x=-37398..-18396,y=-36946..-24449,z=67584..85395
on x=-83796..-70334,y=-1617..3346,z=-42353..-5422
on x=71118..72869,y=-13101..1991,z=30833..48589
off x=19182..36667,y=-84948..-53662,z=-15128..-6251
on x=-32891..-12512,y=46790..64941,z=-37805..-31758
off x=-87853..-57958,y=-9105..10250,z=-27666..-5705
on x=-55159..-15768,y=-23929..-9785,z=49353..69701
off x=58381..63179,y=37782..69395,z=-5063..21127
on x=29347..54026,y=-84030..-63792,z=20530..32897
on x=1052..19601,y=-14750..-4884,z=70402..91679
off x=-43193..-30770,y=63615..86118,z=-19288..-5103
on x=59886..71212,y=-50156..-18781,z=-25959..4904
on x=-47400..-33917,y=45116..46183,z=-52576..-42214
off x=-2916..8013,y=72215..79301,z=12660..39986
on x=-47600..-29671,y=-66262..-47736,z=-250..18288
off x=-76766..-67021,y=23902..56855,z=-24188..-10948
on x=40863..55547,y=-41154..-4752,z=-60017..-51932
on x=19691..33799,y=-91077..-61569,z=-17018..7955
on x=-2214..28174,y=-39590..-31800,z=59281..85857
on x=40206..73039,y=45740..74950,z=-32389..-7431
on x=-60834..-50842,y=-68433..-41429,z=22771..27775
off x=31115..64073,y=-66256..-40204,z=-44062..-24526
on x=-10832..6359,y=-85458..-70278,z=-31345..-13315
off x=26581..36176,y=66579..67779,z=-35420..-10427
on x=66912..86434,y=-10806..2524,z=30276..35350
off x=60734..71718,y=-26519..-13963,z=32357..52134
on x=64029..94207,y=-21424..4543,z=-19917..-5796
off x=-22578..9205,y=26649..48657,z=-69531..-60261
off x=36844..66645,y=34250..50170,z=31072..49267
on x=-9961..-5497,y=7380..37072,z=-89348..-57961
on x=18102..50680,y=-76562..-65621,z=4542..24597
on x=-63529..-54599,y=-53394..-47786,z=21888..25725
off x=63654..94850,y=965..22382,z=-31743..-2000
on x=74998..76278,y=-37896..-10505,z=-9186..22612
off x=-30811..2911,y=-89485..-75160,z=-36405..-3483
off x=51745..78582,y=25188..33294,z=26010..58376
off x=15024..35252,y=-43146..-33304,z=-73102..-57750
on x=2426..9775,y=54690..83625,z=-41065..-25812
off x=-24228..-570,y=-52801..-30297,z=49276..66451
off x=-74129..-54073,y=-56542..-35923,z=-32562..-468
off x=-2928..15901,y=15260..33101,z=67801..88745
off x=-53099..-47112,y=-19233..13470,z=52727..69682
off x=35876..62082,y=-29873..3214,z=64189..67241
off x=-3064..17661,y=65230..82048,z=-410..28371
on x=14630..34564,y=-4463..16268,z=-96585..-71246
on x=-60251..-39251,y=-6775..3343,z=37689..65303
off x=50322..66243,y=19278..44771,z=41067..64697
off x=36020..61904,y=-27183..-9736,z=46137..73036
on x=61922..69366,y=-51710..-34152,z=21534..42960
on x=-48310..-33496,y=36461..61490,z=42916..57166
off x=-14180..12358,y=67333..80981,z=-40807..-23314
off x=-806..32676,y=-88882..-71765,z=10596..21784
on x=4361..34509,y=73189..90685,z=2499..14138
off x=-87784..-75559,y=-23875..-9307,z=-7239..13845
on x=-68172..-53511,y=35832..49496,z=33932..46400
on x=-55713..-37433,y=-5286..18352,z=55977..63293
on x=-17643..17283,y=-35303..-16046,z=68866..82087
off x=25050..58536,y=-75214..-40996,z=-53573..-16621
off x=14898..28167,y=-85300..-58742,z=-21836..-5732
off x=-87322..-55171,y=-35094..-10807,z=9939..27679
on x=-56866..-31891,y=-52393..-39065,z=51551..54781
on x=1566..33534,y=54640..59730,z=51744..60395
on x=15272..24542,y=64983..91848,z=3089..8627
on x=65424..83864,y=29222..36126,z=-12872..15695
off x=-2899..17557,y=-87982..-63662,z=1631..29537
off x=-26849..-9477,y=53092..71495,z=-64534..-33903
on x=-8981..22557,y=22399..30246,z=-76499..-58300
off x=-72834..-48859,y=-18764..9128,z=35760..62856
off x=-24250..-15338,y=31706..46920,z=55005..82901
off x=37368..45012,y=-40763..-35016,z=-60587..-38956
on x=9616..34618,y=-61089..-34252,z=50095..76726
off x=-14337..4309,y=-63805..-49426,z=-75783..-53417
on x=-31091..2449,y=55254..83398,z=-55349..-29593
on x=66679..79368,y=-43938..-18102,z=1633..26650
on x=-32143..-25109,y=62219..88867,z=1027..29516

View File

@@ -0,0 +1,5 @@
#############
#...........#
###D#A#C#D###
#C#A#B#B#
#########

View File

@@ -1,13 +1,5 @@
use std::{
fmt::{Debug, Error, Formatter},
num::ParseIntError,
ops::{Index, IndexMut},
str::FromStr,
};
use anyhow::Result;
use aoc_runner_derive::{aoc, aoc_generator};
use thiserror::Error;
use advent::prelude::*;
use aoc_runner_derive::aoc;
struct Image {
width: usize,
@@ -182,7 +174,7 @@ fn part2(input: &str) -> Result<usize> {
mod tests {
use super::*;
//#[test]
#[test]
fn test_part1() -> Result<()> {
let input = r#"
6,10

272
2021/src/day15.rs Normal file
View File

@@ -0,0 +1,272 @@
use advent::prelude::*;
use aoc_runner_derive::aoc;
struct Image {
width: usize,
height: usize,
pixels: Vec<usize>,
}
impl Image {
fn new(width: usize, height: usize) -> Image {
let pixels = vec![0; width * height];
Image {
width,
height,
pixels,
}
}
}
impl Debug for Image {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
writeln!(f)?;
for y in 0..self.height {
for x in 0..self.width {
write!(f, "{}", self[(x, y)])?;
}
writeln!(f)?;
}
Ok(())
}
}
impl Index<(usize, usize)> for Image {
type Output = usize;
fn index(&self, (x, y): (usize, usize)) -> &Self::Output {
&self.pixels[x + y * self.width]
}
}
impl IndexMut<(usize, usize)> for Image {
fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut Self::Output {
&mut self.pixels[x + y * self.width]
}
}
impl FromStr for Image {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let rows: Vec<_> = s.lines().collect();
let width = rows[0].len();
let height = rows.len();
let pixels = rows
.iter()
.flat_map(|row| row.as_bytes().iter().map(|b| (b - b'0') as usize))
.collect();
Ok(Image {
width,
height,
pixels,
})
}
}
use std::{cmp::Ordering, collections::BinaryHeap};
#[derive(Copy, Clone, Eq, PartialEq)]
struct State {
cost: usize,
position: usize,
}
// The priority queue depends on `Ord`.
// Explicitly implement the trait so the queue becomes a min-heap
// instead of a max-heap.
impl Ord for State {
fn cmp(&self, other: &Self) -> Ordering {
// Notice that the we flip the ordering on costs.
// In case of a tie we compare positions - this step is necessary
// to make implementations of `PartialEq` and `Ord` consistent.
other
.cost
.cmp(&self.cost)
.then_with(|| self.position.cmp(&other.position))
}
}
// `PartialOrd` needs to be implemented as well.
impl PartialOrd for State {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
// Each node is represented as a `usize`, for a shorter implementation.
struct Edge {
node: usize,
cost: usize,
}
impl Debug for Edge {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
write!(f, "Edge{{node: {}, cost: {}}}", self.node, self.cost)?;
Ok(())
}
}
// From https://doc.rust-lang.org/std/collections/binary_heap/index.html
// Dijkstra's shortest path algorithm.
// Start at `start` and use `dist` to track the current shortest distance
// to each node. This implementation isn't memory-efficient as it may leave duplicate
// nodes in the queue. It also uses `usize::MAX` as a sentinel value,
// for a simpler implementation.
fn shortest_path(adj_list: &[Vec<Edge>], start: usize, goal: usize) -> Option<usize> {
// dist[node] = current shortest distance from `start` to `node`
let mut dist: Vec<_> = (0..adj_list.len()).map(|_| usize::MAX).collect();
let mut heap = BinaryHeap::new();
// We're at `start`, with a zero cost
dist[start] = 0;
heap.push(State {
cost: 0,
position: start,
});
// Examine the frontier with lower cost nodes first (min-heap)
while let Some(State { cost, position }) = heap.pop() {
// Alternatively we could have continued to find all shortest paths
if position == goal {
return Some(cost);
}
// Important as we may have already found a better way
if cost > dist[position] {
continue;
}
// For each node we can reach, see if we can find a way with
// a lower cost going through this node
for edge in &adj_list[position] {
let next = State {
cost: cost + edge.cost,
position: edge.node,
};
// If so, add it to the frontier and continue
if next.cost < dist[next.position] {
heap.push(next);
// Relaxation, we have now found a better way
dist[next.position] = next.cost;
}
}
}
// Goal not reachable
None
}
fn make_graph(im: &Image) -> Vec<Vec<Edge>> {
let idx = |x, y| y * im.width + x;
let mut graph: Vec<_> = Vec::new();
for y in 0..im.height {
for x in 0..im.width {
let mut edges = Vec::new();
if x > 0 {
edges.push(Edge {
node: idx(x - 1, y),
cost: im[(x - 1, y)],
});
}
if x < im.width - 1 {
edges.push(Edge {
node: idx(x + 1, y),
cost: im[(x + 1, y)],
});
}
if y > 0 {
edges.push(Edge {
node: idx(x, y - 1),
cost: im[(x, y - 1)],
});
}
if y < im.height - 1 {
edges.push(Edge {
node: idx(x, y + 1),
cost: im[(x, y + 1)],
});
}
graph.push(edges);
}
}
graph
}
#[aoc(day15, part1)]
fn part1(input: &str) -> Result<usize> {
let im: Image = input.parse()?;
let graph = make_graph(&im);
Ok(shortest_path(&graph, 0, im.pixels.len() - 1).unwrap())
}
fn x5(im: &Image) -> Image {
let mut im5 = Image::new(im.width * 5, im.height * 5);
for iy in 0..5 {
for ix in 0..5 {
for y in 0..im.height {
for x in 0..im.width {
let v = im[(x, y)] + ix + iy;
let dst_x = ix * im.width + x;
let dst_y = iy * im.height + y;
im5[(dst_x, dst_y)] = if v > 9 { v % 9 } else { v };
}
}
}
}
im5
}
#[aoc(day15, part2)]
fn part2(input: &str) -> Result<usize> {
let im: Image = input.parse()?;
let im = x5(&im);
let graph = make_graph(&im);
Ok(shortest_path(&graph, 0, im.pixels.len() - 1).unwrap())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part1() -> Result<()> {
let input = r#"
1163751742
1381373672
2136511328
3694931569
7463417111
1319128137
1359912421
3125421639
1293138521
2311944581
"#
.trim();
assert_eq!(part1(input)?, 40);
Ok(())
}
#[test]
fn test_part2() -> Result<()> {
let input = r#"
1163751742
1381373672
2136511328
3694931569
7463417111
1319128137
1359912421
3125421639
1293138521
2311944581
"#
.trim();
assert_eq!(part2(input)?, 315);
Ok(())
}
}

245
2021/src/day16.rs Normal file
View File

@@ -0,0 +1,245 @@
use advent::prelude::*;
use aoc_runner_derive::aoc;
fn hex(b: &u8) -> u8 {
if *b >= b'A' {
10 + b - b'A'
} else {
b - b'0'
}
}
fn sum_version(packet: &Packet) -> u64 {
fn sum_packets(packets: &[Packet]) -> u64 {
packets.iter().map(sum_version).sum()
}
packet.version as u64
+ match &packet.packet_type {
PacketType::Sum(packets) => sum_packets(packets),
PacketType::Product(packets) => sum_packets(packets),
PacketType::Minimum(packets) => sum_packets(packets),
PacketType::Maximum(packets) => sum_packets(packets),
PacketType::Literal(_) => 0,
PacketType::GreaterThan(packets) => sum_packets(packets),
PacketType::LessThan(packets) => sum_packets(packets),
PacketType::Equal(packets) => sum_packets(packets),
}
}
fn interpret(packet: &Packet) -> u64 {
match &packet.packet_type {
PacketType::Sum(packets) => packets.iter().map(interpret).sum(),
PacketType::Product(packets) => packets.iter().map(interpret).product(),
PacketType::Minimum(packets) => packets.iter().map(interpret).min().unwrap(),
PacketType::Maximum(packets) => packets.iter().map(interpret).max().unwrap(),
PacketType::Literal(v) => *v,
PacketType::GreaterThan(packets) => {
if interpret(&packets[0]) > interpret(&packets[1]) {
1
} else {
0
}
}
PacketType::LessThan(packets) => {
if interpret(&packets[0]) < interpret(&packets[1]) {
1
} else {
0
}
}
PacketType::Equal(packets) => {
if interpret(&packets[0]) == interpret(&packets[1]) {
1
} else {
0
}
}
}
}
#[derive(Debug)]
enum PacketType {
// 0
Sum(Vec<Packet>),
// 1
Product(Vec<Packet>),
// 2
Minimum(Vec<Packet>),
// 3
Maximum(Vec<Packet>),
// 4
Literal(u64),
// 5
GreaterThan(Vec<Packet>),
// 6
LessThan(Vec<Packet>),
// 7
Equal(Vec<Packet>),
}
#[derive(Debug)]
struct Packet {
version: u64,
bit_size: u64,
packet_type: PacketType,
}
struct Parser<'a> {
bytes: &'a [u8],
tmp: u64,
tmp_len: usize,
}
impl<'a> Parser<'a> {
fn new(input: &str) -> Parser {
Parser {
bytes: input.as_bytes(),
tmp: 0,
tmp_len: 0,
}
}
fn read(&mut self, n: usize) -> u64 {
assert!(n < 32, "can't read more than 32 bits at time");
//print!( " BEGIN n {0} tmp 0b{1:b} len {2} - ", n, self.tmp, self.tmp_len);
while self.tmp_len < n {
let mut buf = [0; 1];
self.bytes.read_exact(&mut buf).expect("EOF");
// Convert the byte from hexdecimal to binary and merge with any leftover bits.
self.tmp = (self.tmp << 4) | hex(&buf[0]) as u64;
self.tmp_len += 4;
}
let mask = (1 << n) - 1;
self.tmp_len -= n;
let v = (self.tmp >> self.tmp_len) & mask;
let mask = (1 << self.tmp_len) - 1;
self.tmp &= mask;
//println!( " END n {0} tmp 0b{2:b} len {3} v 0b{1:00$b} ", n, v, self.tmp, self.tmp_len);
v as u64
}
}
fn parse_packet(p: &mut Parser) -> Packet {
let mut bit_size: u64 = 0;
let version = p.read(3);
bit_size += 3;
let packet_type_id = p.read(3);
bit_size += 3;
let packet_type = if packet_type_id == 4 {
// Literal, read 5 bits at a time until MSB is 0
let mut v = 0;
loop {
let l = p.read(5);
v = (v << 4) | (l & 0b1111);
bit_size += 5;
if 0b10000 & l == 0 {
break;
}
}
PacketType::Literal(v)
} else {
// length type ID
let ltid = p.read(1);
bit_size += 1;
let mut packets = Vec::new();
if ltid == 0 {
// If the length type ID is 0, then the next 15 bits are a number that represents the total length in bits of the sub-packets contained by this packet.
let len = p.read(15);
bit_size += 15;
let mut sub_bits = 0;
while sub_bits < len {
let sub_p = parse_packet(p);
bit_size += sub_p.bit_size;
sub_bits += sub_p.bit_size;
packets.push(sub_p);
}
} else {
// If the length type ID is 1, then the next 11 bits are a number that represents the number of sub-packets immediately contained by this packet.
let num = p.read(11);
bit_size += 11;
for _ in 0..num {
let sub_p = parse_packet(p);
bit_size += sub_p.bit_size;
packets.push(sub_p);
}
}
match packet_type_id {
0 => PacketType::Sum(packets),
1 => PacketType::Product(packets),
2 => PacketType::Minimum(packets),
3 => PacketType::Maximum(packets),
5 => PacketType::GreaterThan(packets),
6 => PacketType::LessThan(packets),
7 => PacketType::Equal(packets),
_ => panic!("unknown packet type ID {}", packet_type_id),
}
};
Packet {
version,
bit_size,
packet_type,
}
}
#[aoc(day16, part1)]
fn part1(input: &str) -> Result<u64> {
let mut p = Parser::new(input);
let packet = parse_packet(&mut p);
Ok(sum_version(&packet))
}
#[aoc(day16, part2)]
fn part2(input: &str) -> Result<u64> {
let mut p = Parser::new(input);
let packet = parse_packet(&mut p);
Ok(interpret(&packet))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part1() -> Result<()> {
let input = vec![
("D2FE28", 6),
//("38006F45291200", 1 + 0 + 0),
("8A004A801A8002F478", 16),
("620080001611562C8802118E34", 12),
("C0015000016115A2E0802F182340", 23),
("A0016C880162017C3686B18A3D4780", 31),
];
for (inp, want) in input {
print!("\nTesting '{}'\n - ", inp);
inp.as_bytes().iter().for_each(|c| print!("{:04b}", hex(c)));
println!();
assert_eq!(part1(inp)?, want);
println!("Passed '{}'", inp);
}
Ok(())
}
#[test]
fn test_part2() -> Result<()> {
let input = vec![
("C200B40A82", 3),
("04005AC33890", 54),
("880086C3E88112", 7),
("CE00C43D881120", 9),
("D8005AC2A8F0", 1),
("F600BC2D8F", 0),
("9C005AC2F8F0", 0),
("9C0141080250320F1802104A08", 1),
];
for (inp, want) in input {
print!("\nTesting '{}'\n - ", inp);
inp.as_bytes().iter().for_each(|c| print!("{:04b}", hex(c)));
println!();
assert_eq!(part2(inp)?, want);
println!("Passed '{}'", inp);
}
Ok(())
}
}

107
2021/src/day17.rs Normal file
View File

@@ -0,0 +1,107 @@
use advent::prelude::*;
use aoc_runner_derive::aoc;
#[derive(Debug)]
struct Target {
x_min: isize,
x_max: isize,
y_min: isize,
y_max: isize,
}
impl Target {
fn hit(&self, x: isize, y: isize) -> bool {
x >= self.x_min && x <= self.x_max && y >= self.y_min && y <= self.y_max
}
}
fn shoot(x: isize, y: isize, tgt: &Target) -> bool {
let mut x_inc = x;
let mut y_inc = y;
let mut x_cur = 0;
let mut y_cur = 0;
while x_cur <= tgt.x_max && y_cur >= tgt.y_min {
x_cur += x_inc;
y_cur += y_inc;
if x_inc > 0 {
x_inc -= 1;
}
y_inc -= 1;
if tgt.hit(x_cur, y_cur) {
return true;
}
}
false
}
impl FromStr for Target {
type Err = Infallible;
fn from_str(input: &str) -> std::result::Result<Target, Infallible> {
let parts: Vec<_> = input.split(' ').collect();
let x = &parts[2][2..].strip_suffix(',').unwrap();
let y = &parts[3][2..];
let (x_min, x_max) = x
.split_once("..")
.map(|(min, max)| (min.parse().unwrap(), max.parse().unwrap()))
.unwrap();
let (y_min, y_max) = y
.split_once("..")
.map(|(min, max)| (min.parse().unwrap(), max.parse().unwrap()))
.unwrap();
Ok(Target {
x_min,
x_max,
y_min,
y_max,
})
}
}
#[aoc(day17, part1)]
fn part1(input: &str) -> Result<isize> {
let tgt: Target = input.parse()?;
let n = tgt.y_min.abs() - 1;
Ok(n * (n + 1) / 2)
}
#[aoc(day17, part2)]
fn part2(input: &str) -> Result<usize> {
let tgt: Target = input.parse()?;
let mut cnt = 0;
let y_range = tgt.y_min.abs().max(tgt.y_min.abs());
for y in -y_range..=y_range {
for x in 1..=tgt.x_max {
if shoot(x, y, &tgt) {
cnt += 1;
}
}
}
Ok(cnt)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part1() -> Result<()> {
let input = r#"
target area: x=20..30, y=-10..-5
"#
.trim();
assert_eq!(part1(input)?, 45);
Ok(())
}
#[test]
fn test_part2() -> Result<()> {
let input = r#"
target area: x=20..30, y=-10..-5
"#
.trim();
assert_eq!(part2(input)?, 112);
Ok(())
}
}

732
2021/src/day18.rs Normal file
View File

@@ -0,0 +1,732 @@
use advent::prelude::*;
use aoc_runner_derive::aoc;
use std::{
io::{BufReader, Cursor, Read},
ops::Add,
};
#[derive(Copy, Clone, Debug, PartialEq)]
enum ChildType {
None,
Value(usize),
Subtree(Idx),
}
#[derive(Copy, Clone, Default, PartialEq)]
struct Idx(usize);
impl Debug for Idx {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl Display for Idx {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Debug, PartialEq)]
struct Node {
deleted: bool,
idx: Idx,
parent: Option<Idx>,
left: ChildType,
right: ChildType,
}
// Tree needs to support merging two into one for adding snailfish numbers.
// Tree needs to support rightward and leftward depth first searches to find neighbors for applying
// exploded spill over.
// Need to support remove and/or replace for explode.
// Need to support insert and/or replace for split.
#[derive(Debug, Default)]
struct Tree {
root: Idx,
nodes: Vec<Node>,
}
struct TreeIter {
it: std::vec::IntoIter<Idx>,
}
impl TreeIter {
fn new(indices: &[Idx]) -> TreeIter {
let indices = indices.to_vec();
TreeIter {
it: indices.into_iter(),
}
}
}
impl Iterator for TreeIter {
type Item = Idx;
fn next(&mut self) -> Option<Self::Item> {
self.it.next()
}
}
impl PartialEq for Tree {
fn eq(&self, other: &Self) -> bool {
// Lazy but should work.
self.to_string() == other.to_string()
}
}
fn read_byte<R: Read>(reader: &mut R) -> std::io::Result<Option<u8>> {
reader.bytes().next().transpose()
}
impl Tree {
fn reduce(&mut self) {
let mut changed = true;
while changed {
changed = self.explode();
println!("after explode {}", self);
if changed {
continue;
}
changed = self.split();
println!("after split {}", self);
//println!("splice changed {}", changed);
}
}
fn magnitude(&self) -> usize {
fn inner(tree: &Tree, node: &Node) -> usize {
match (node.left, node.right) {
(ChildType::Value(l), ChildType::Value(r)) => 3 * l + 2 * r,
(ChildType::Subtree(idx), ChildType::Value(r)) => {
3 * inner(&tree, &tree[idx]) + 2 * r
}
(ChildType::Value(l), ChildType::Subtree(idx)) => {
3 * l + 2 * inner(&tree, &tree[idx])
}
(ChildType::Subtree(l_idx), ChildType::Subtree(r_idx)) => {
3 * inner(&tree, &tree[l_idx]) + 2 * inner(&tree, &tree[r_idx])
}
_ => panic!("unhandled combo for magnitude"),
}
}
inner(self, &self[self.root])
}
fn split(&mut self) -> bool {
if let Some(split_idx) = self
.left_to_right()
.skip_while(|idx| {
let n = &self[*idx];
if let ChildType::Value(v) = n.left {
if v > 9 {
return false;
}
}
if let ChildType::Value(v) = n.right {
if v > 9 {
return false;
}
}
true
})
.next()
{
if let ChildType::Value(v) = self[split_idx].left {
if v > 9 {
let l = v / 2;
let r = if v % 2 == 1 { 1 + v / 2 } else { v / 2 };
let mut new_idx = self.add_node(ChildType::Value(l), ChildType::Value(r));
self[new_idx].parent = Some(split_idx);
self[split_idx].left = ChildType::Subtree(new_idx);
}
}
if let ChildType::Value(v) = self[split_idx].right {
if v > 9 {
let l = v / 2;
let r = if v % 2 == 1 { 1 + v / 2 } else { v / 2 };
let mut new_idx = self.add_node(ChildType::Value(l), ChildType::Value(r));
self[new_idx].parent = Some(split_idx);
self[split_idx].right = ChildType::Subtree(new_idx);
}
}
return true;
}
false
}
fn explode(&mut self) -> bool {
let mut changed = false;
if let Some(node) = self
.nodes
.iter()
.filter(|n| !n.deleted)
.find(|n| self.depth(n) >= 4)
{
changed = true;
let ex_idx = node.idx;
// Find spillover to the right
if let Some(spillover) = self
.left_to_right()
.skip_while(|idx| *idx != ex_idx)
.skip(1)
.find(|idx| {
let n = &self[*idx];
match (n.left, n.right) {
(ChildType::Subtree(_), ChildType::Subtree(_)) => false,
_ => true,
}
})
{
let src = self[ex_idx].right;
let tgt = &mut self[spillover];
if let (ChildType::Value(l), ChildType::Value(r)) = (src, tgt.left) {
tgt.left = ChildType::Value(l + r);
} else if let (ChildType::Value(l), ChildType::Value(r)) = (src, tgt.right) {
tgt.right = ChildType::Value(l + r);
} else {
unreachable!()
};
}
// Find spillover to the left
if let Some(spillover) = self
.right_to_left()
.skip_while(|idx| *idx != ex_idx)
.skip(1)
.find(|idx| {
let n = &self[*idx];
match (n.left, n.right) {
(ChildType::Subtree(_), ChildType::Subtree(_)) => false,
_ => true,
}
})
{
let src = self[ex_idx].left;
let tgt = &mut self[spillover];
if let (ChildType::Value(l), ChildType::Value(r)) = (src, tgt.right) {
tgt.right = ChildType::Value(l + r);
} else if let (ChildType::Value(l), ChildType::Value(r)) = (src, tgt.left) {
tgt.left = ChildType::Value(l + r);
} else {
unreachable!()
};
}
// Replace exploded node
self[ex_idx].deleted = true;
let p_idx = self[ex_idx].parent.expect("exploded root");
let p = &mut self[p_idx];
if let ChildType::Subtree(idx) = p.left {
if idx == ex_idx {
p.left = ChildType::Value(0);
}
}
if let ChildType::Subtree(idx) = p.right {
if idx == ex_idx {
p.right = ChildType::Value(0);
}
}
}
changed
}
fn depth(&self, node: &Node) -> usize {
if let Some(parent_idx) = node.parent {
1 + self.depth(&self[parent_idx])
} else {
0
}
}
fn find_root(&self, node: &Node) -> Idx {
match node.parent {
Some(parent_idx) => self.find_root(&self[parent_idx]),
None => node.idx,
}
}
fn add_node(&mut self, left: ChildType, right: ChildType) -> Idx {
let idx = Idx(self.nodes.len());
self.nodes.push(Node {
deleted: false,
idx,
parent: None,
left,
right,
});
idx
}
fn from_str_node(&mut self, r: &mut BufReader<Cursor<&[u8]>>) -> ChildType {
let mut parsing_left = true;
// Can this be rewritten to eliminate the need for `None`?
let mut left = ChildType::None;
let mut right = ChildType::None;
while let Ok(Some(b)) = read_byte(r) {
match b {
b'[' => {
let node = self.from_str_node(r);
if parsing_left {
left = node;
} else {
right = node;
}
}
b']' => {
let mut left_idx = None;
let mut right_idx = None;
if let ChildType::Subtree(idx) = left {
left_idx = Some(idx);
}
if let ChildType::Subtree(idx) = right {
right_idx = Some(idx);
}
let child_idx = self.add_node(left, right);
if let Some(idx) = left_idx {
self[idx].parent = Some(child_idx);
}
if let Some(idx) = right_idx {
self[idx].parent = Some(child_idx);
}
return ChildType::Subtree(child_idx);
}
b',' => parsing_left = false,
b'0'..=b'9' => {
let mut v = b - b'0';
if let Ok(Some(peek)) = read_byte(r) {
match peek {
b'0'..=b'9' => v = (peek - b'0') + 10 * v,
// Wasn't a number >9, push the byte back into the buffer.
_ => r.seek_relative(-1).expect("failed to seek"),
}
}
if parsing_left {
left = ChildType::Value(v.into());
parsing_left = false;
} else {
right = ChildType::Value(v.into());
}
continue;
}
_ => panic!("unknown byte '{}'", b),
}
}
unreachable!()
}
fn fmt_node(&self, f: &mut Formatter<'_>, node: &Node) -> std::fmt::Result {
write!(f, "[")?;
match node.left {
ChildType::None => panic!("left node was None"),
ChildType::Value(v) => write!(f, "{}", v)?,
ChildType::Subtree(idx) => self.fmt_node(f, &self[idx])?,
};
write!(f, ",")?;
match node.right {
ChildType::None => panic!("right node was None"),
ChildType::Value(v) => write!(f, "{}", v)?,
ChildType::Subtree(idx) => self.fmt_node(f, &self[idx])?,
};
write!(f, "]")?;
Ok(())
}
fn left_to_right(&mut self) -> TreeIter {
fn dfs(tree: &Tree, n: &Node, mut indices: &mut Vec<Idx>) {
if let ChildType::Subtree(idx) = n.left {
dfs(tree, &tree[idx], indices);
}
indices.push(n.idx);
if let ChildType::Subtree(idx) = n.right {
dfs(tree, &tree[idx], indices);
}
}
let mut indices = Vec::with_capacity(self.nodes.len());
dfs(self, &self[self.root], &mut indices);
TreeIter::new(&indices)
}
fn right_to_left(&mut self) -> TreeIter {
fn dfs(tree: &Tree, n: &Node, mut indices: &mut Vec<Idx>) {
if let ChildType::Subtree(idx) = n.right {
dfs(tree, &tree[idx], indices);
}
indices.push(n.idx);
if let ChildType::Subtree(idx) = n.left {
dfs(tree, &tree[idx], indices);
}
}
let mut indices = Vec::with_capacity(self.nodes.len());
dfs(self, &self[self.root], &mut indices);
TreeIter::new(&indices)
}
}
impl Add for Tree {
type Output = Tree;
fn add(self, other: Self) -> Self {
// This is lazy but works for simple any obvious reasons (if FromStr and Display work
// correctly).
format!("[{},{}]", self, other)
.parse()
.expect("failed to parse merge tree")
}
}
impl FromStr for Tree {
type Err = Infallible;
fn from_str(input: &str) -> std::result::Result<Tree, Infallible> {
let mut tree = Tree::default();
let mut bytes = input.as_bytes();
assert_eq!(
read_byte(&mut bytes).expect("couldn't read first byte"),
Some(b'[')
);
let mut b = BufReader::new(Cursor::new(bytes));
tree.from_str_node(&mut b);
tree.root = tree.find_root(&tree[Idx(0)]);
Ok(tree)
}
}
impl Index<Idx> for Tree {
type Output = Node;
fn index(&self, idx: Idx) -> &Self::Output {
&self.nodes[idx.0]
}
}
impl IndexMut<Idx> for Tree {
fn index_mut(&mut self, idx: Idx) -> &mut Self::Output {
&mut self.nodes[idx.0]
}
}
impl Display for Tree {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if self.nodes.is_empty() {
return write!(f, "[]");
}
let node = &self[self.root];
self.fmt_node(f, &node)?;
Ok(())
}
}
fn sum(input: &str) -> Tree {
input
.lines()
.map(|l| l.parse().expect("failed to parse"))
.reduce(|acc, t| acc + t)
.expect("failed to reduce")
}
#[aoc(day18, part1)]
fn part1(input: &str) -> Result<usize> {
let nums: Vec<Tree> = input
.lines()
.map(|l| {
dbg!(l);
l.parse().expect("failed to parse")
})
.collect();
let mut it = nums.into_iter();
let mut last = it.next().unwrap();
while let Some(next) = it.next() {
println!(" {}", last);
println!("+ {}", next);
last = last + next;
println!("= {}", last);
last.reduce();
println!("= {}\n", last);
}
Ok(last.magnitude())
}
/*
#[aoc(day18, part2)]
fn part2(input: &str) -> Result<usize> {
todo!("part2");
Ok(0)
}
*/
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_display() -> Result<()> {
for (i, s) in ["[1,2]", "[[1,2],3]", "[1,[2,3]]", "[[1,2],[3,4]]"]
.into_iter()
.enumerate()
{
let t = s.parse::<Tree>()?;
assert_eq!(&t.to_string(), s, "input {}: '{}'", i, s);
//assert_eq!(&t.to_string(), s, "input {}: '{}'\ntree: {:#?}", i, s, t);
}
Ok(())
}
#[test]
fn test_sum() -> Result<()> {
let l: Tree = "[1,2]".parse().unwrap();
let r: Tree = "[[3,4],5]".parse().unwrap();
assert_eq!(l + r, "[[1,2],[[3,4],5]]".parse().unwrap());
let input = r#"
[[[[4,3],4],4],[7,[[8,4],9]]]
[1,1]
"#
.trim();
let mut s = sum(input);
s.reduce();
assert_eq!(s.to_string(), "[[[[0,7],4],[[7,8],[6,0]]],[8,1]]");
Ok(())
}
#[test]
fn test_reduce() -> Result<()> {
for (input, want) in [
("[0,[0,[0,[0,[0,0]]]]]", "[0,[0,[0,[0,0]]]]"),
("[[[[[[[[0,0],0],0],0],0],0],0],0]", "[[[[0,0],0],0],0]"),
("[[[[[[[0,0],0],0],0],0],0],0]", "[[[[0,0],0],0],0]"),
] {
println!("== test_reduce: {}", input);
let mut tree: Tree = input.parse()?;
tree.reduce();
let want = want.parse()?;
assert_eq!(tree, want, "\nInput {} Got {} Want {}", input, tree, want);
}
Ok(())
}
#[test]
fn test_explode() -> Result<()> {
for (input, want) in [
("[[[[0,0],0],0],0]", "[[[[0,0],0],0],0]"),
("[[[0,0],0],0]", "[[[0,0],0],0]"),
("[[0,0],0]", "[[0,0],0]"),
("[0,[0,[0,[0,0]]]]", "[0,[0,[0,[0,0]]]]"),
("[0,[0,[0,0]]]", "[0,[0,[0,0]]]"),
("[0,[0,0]]", "[0,[0,0]]"),
("[[[[[9,8],1],2],3],4]", "[[[[0,9],2],3],4]"),
("[7,[6,[5,[4,[3,2]]]]]", "[7,[6,[5,[7,0]]]]"),
("[[6,[5,[4,[3,2]]]],1]", "[[6,[5,[7,0]]],3]"),
(
"[[3,[2,[1,[7,3]]]],[6,[5,[4,[3,2]]]]]",
"[[3,[2,[8,0]]],[9,[5,[4,[3,2]]]]]",
),
(
"[[3,[2,[8,0]]],[9,[5,[4,[3,2]]]]]",
"[[3,[2,[8,0]]],[9,[5,[7,0]]]]",
),
] {
println!("== test_explode: {}", input);
let mut tree: Tree = input.parse()?;
tree.explode();
let want = want.parse()?;
assert_eq!(tree, want, "\nInput {} Got {} Want {}", input, tree, want);
}
Ok(())
}
#[test]
fn test_split() -> Result<()> {
for (input, want) in [
("[10,0]", "[[5,5],0]"), //
("[0,11]", "[0,[5,6]]"),
("[[0,11],0]", "[[0,[5,6]],0]"),
("[11,0]", "[[5,6],0]"),
("[0,[11,0]]", "[0,[[5,6],0]]"),
("[12,0]", "[[6,6],0]"),
("[0,12]", "[0,[6,6]]"),
] {
println!("== test_split: {}", input);
let mut tree: Tree = input.parse()?;
dbg!(&tree);
tree.split();
let want = want.parse()?;
assert_eq!(tree, want, "\nInput {} Got {} Want {}", input, tree, want);
}
Ok(())
}
#[test]
fn test_magnitude() -> Result<()> {
for (input, want) in [
("[9,1]", 29),
("[1,9]", 21),
("[[9,1],[1,9]]", 129),
("[[1,2],[[3,4],5]]", 143),
("[[[[0,7],4],[[7,8],[6,0]]],[8,1]]", 1384),
("[[[[1,1],[2,2]],[3,3]],[4,4]]", 445),
("[[[[3,0],[5,3]],[4,4]],[5,5]]", 791),
("[[[[5,0],[7,4]],[5,5]],[6,6]]", 1137),
(
"[[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]]",
3488,
),
] {
let tree: Tree = input.parse()?;
assert_eq!(tree.magnitude(), want);
}
Ok(())
}
#[test]
fn test_add_and_reduce() -> Result<()> {
for (input, want) in [
(
r#"
[1,1]
[2,2]
[3,3]
[4,4]
"#,
"[[[[1,1],[2,2]],[3,3]],[4,4]]",
),
(
r#"
[1,1]
[2,2]
[3,3]
[4,4]
[5,5]
"#
.trim(),
"[[[[3,0],[5,3]],[4,4]],[5,5]]",
),
(
r#"
[1,1]
[2,2]
[3,3]
[4,4]
[5,5]
[6,6]
"#
.trim(),
"[[[[5,0],[7,4]],[5,5]],[6,6]]",
),
(
r#"
[[[[4,3],4],4],[7,[[8,4],9]]]
[1,1]
"#
.trim(),
"[[[[0,7],4],[[7,8],[6,0]]],[8,1]]",
),
] {
println!("== 1. test_add_and_reduce: {}", input);
let mut num = sum(input.trim());
println!("before reduce: {}", num);
num.reduce();
println!("after reduce: {}", num);
assert_eq!(num.to_string(), want);
}
for (l, r, eq) in [
(
"[[[[4,3],4],4],[7,[[8,4],9]]]",
"[1,1]",
"[[[[0,7],4],[[7,8],[6,0]]],[8,1]]",
),
(
"[[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]]",
"[7,[[[3,7],[4,3]],[[6,3],[8,8]]]]",
"[[[[4,0],[5,4]],[[7,7],[6,0]]],[[8,[7,7]],[[7,9],[5,0]]]]",
),
(
"[[[[4,0],[5,4]],[[7,7],[6,0]]],[[8,[7,7]],[[7,9],[5,0]]]]",
"[[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]]",
"[[[[6,7],[6,7]],[[7,7],[0,7]]],[[[8,7],[7,7]],[[8,8],[8,0]]]]",
),
(
"[[[[6,7],[6,7]],[[7,7],[0,7]]],[[[8,7],[7,7]],[[8,8],[8,0]]]]",
"[[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]]",
"[[[[7,0],[7,7]],[[7,7],[7,8]]],[[[7,7],[8,8]],[[7,7],[8,7]]]]",
),
(
"[[[[7,0],[7,7]],[[7,7],[7,8]]],[[[7,7],[8,8]],[[7,7],[8,7]]]]",
"[7,[5,[[3,8],[1,4]]]]",
"[[[[7,7],[7,8]],[[9,5],[8,7]]],[[[6,8],[0,8]],[[9,9],[9,0]]]]",
),
(
"[[[[7,7],[7,8]],[[9,5],[8,7]]],[[[6,8],[0,8]],[[9,9],[9,0]]]]",
"[[2,[2,2]],[8,[8,1]]]",
"[[[[6,6],[6,6]],[[6,0],[6,7]]],[[[7,7],[8,9]],[8,[8,1]]]]",
),
(
"[[[[6,6],[6,6]],[[6,0],[6,7]]],[[[7,7],[8,9]],[8,[8,1]]]]",
"[2,9]",
"[[[[6,6],[7,7]],[[0,7],[7,7]]],[[[5,5],[5,6]],9]]",
),
(
"[[[[6,6],[7,7]],[[0,7],[7,7]]],[[[5,5],[5,6]],9]]",
"[1,[[[9,3],9],[[9,0],[0,7]]]]",
"[[[[7,8],[6,7]],[[6,8],[0,8]]],[[[7,7],[5,0]],[[5,5],[5,6]]]]",
),
(
"[[[[7,8],[6,7]],[[6,8],[0,8]]],[[[7,7],[5,0]],[[5,5],[5,6]]]]",
"[[[5,[7,4]],7],1]",
"[[[[7,7],[7,7]],[[8,7],[8,7]]],[[[7,0],[7,7]],9]]",
),
(
"[[[[7,7],[7,7]],[[8,7],[8,7]]],[[[7,0],[7,7]],9]]",
"[[[[4,2],2],6],[8,7]]",
"[[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]]",
),
] {
let l: Tree = l.parse()?;
let r: Tree = r.parse()?;
let mut num = l + r;
println!("== 2. test_add_and_reduce: {}", num);
println!("before reduce: {}", num);
num.reduce();
println!("after reduce: {}", num);
assert_eq!(num.to_string(), eq);
}
Ok(())
}
#[test]
fn test_part1() -> Result<()> {
let input = r#"
[[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]]
[7,[[[3,7],[4,3]],[[6,3],[8,8]]]]
[[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]]
[[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]]
[7,[5,[[3,8],[1,4]]]]
[[2,[2,2]],[8,[8,1]]]
[2,9]
[1,[[[9,3],9],[[9,0],[0,7]]]]
[[[5,[7,4]],7],1]
[[[[4,2],2],6],[8,7]]
"#
.trim();
assert_eq!(part1(input)?, 3488);
let input = r#"
[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]]
[[[5,[2,8]],4],[5,[[9,9],0]]]
[6,[[[6,2],[5,6]],[[7,6],[4,7]]]]
[[[6,[0,7]],[0,9]],[4,[9,[9,0]]]]
[[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]]
[[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]]
[[[[5,4],[7,7]],8],[[8,3],8]]
[[9,3],[[9,9],[6,[4,9]]]]
[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]]
[[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]]
"#
.trim();
assert_eq!(part1(input)?, 4140);
Ok(())
}
/*
#[test]
fn test_part2()->Result<()> {
let input = r#"
"#
.trim();
assert_eq!(part2(input)?, usize::MAX);
Ok(())
}
*/
}

469
2021/src/day19.rs Normal file
View File

@@ -0,0 +1,469 @@
use advent::prelude::*;
use aoc_runner_derive::aoc;
use std::ops::{Add, Sub};
#[derive(Clone, Copy, Default, Eq, Hash, PartialEq, PartialOrd, Ord)]
struct Vec3([i64; 3]);
impl Add for Vec3 {
type Output = Self;
fn add(self, other: Self) -> Self::Output {
Vec3([
self.0[0] + other.0[0],
self.0[1] + other.0[1],
self.0[2] + other.0[2],
])
}
}
impl Sub for Vec3 {
type Output = Self;
fn sub(self, other: Self) -> Self::Output {
Vec3([
self.0[0] - other.0[0],
self.0[1] - other.0[1],
self.0[2] - other.0[2],
])
}
}
impl Debug for Vec3 {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
write!(f, "<{:4},{:4},{:4}>", self.0[0], self.0[1], self.0[2])
}
}
impl FromStr for Vec3 {
type Err = Infallible;
fn from_str(input: &str) -> std::result::Result<Vec3, Infallible> {
let v: Vec<_> = input.split(',').map(|s| s.parse().unwrap()).collect();
Ok(Vec3(v.try_into().unwrap()))
}
}
#[derive(Debug)]
struct Scanner {
id: usize,
offset: Option<Vec3>,
points: Vec<Vec3>,
}
impl Scanner {
fn translate(&mut self, distance: Vec3, orientation: [usize; 3], signs: [i64; 3]) {
for p in &mut self.points {
*p = Vec3([
signs[0] * p.0[orientation[0]] + distance.0[0],
signs[1] * p.0[orientation[1]] + distance.0[1],
signs[2] * p.0[orientation[2]] + distance.0[2],
]);
}
}
}
impl FromStr for Scanner {
type Err = Infallible;
fn from_str(input: &str) -> std::result::Result<Scanner, Infallible> {
let mut it = input.lines();
let id = it
.next()
.unwrap()
.split(' ')
.nth(2)
.unwrap()
.parse()
.unwrap();
Ok(Scanner {
id,
offset: None,
points: it.map(|l| l.parse().unwrap()).collect(),
})
}
}
#[derive(Debug, PartialEq)]
struct Match {
abs_points: Vec<Vec3>,
distance: Vec3,
orientation: [usize; 3],
signs: [i64; 3],
}
// Returns overlap, and in s1 space
fn find_overlap(s1: &Scanner, s2: &Scanner) -> Option<Match> {
let mut counts: HashMap<(Vec3, [usize; 3], [i64; 3]), Vec<Vec3>> = HashMap::new();
let orientations = [
[0, 1, 2],
[0, 2, 1],
[1, 0, 2],
[1, 2, 0],
[2, 0, 1],
[2, 1, 0],
];
let signs = [
[-1, -1, -1],
[1, -1, -1],
[-1, 1, -1],
[1, 1, -1],
[-1, -1, 1],
[1, -1, 1],
[-1, 1, 1],
];
for v1 in &s1.points {
for v2 in &s2.points {
for or in orientations {
for sign in signs {
let [x, y, z] = sign;
let v = Vec3([x * v2.0[or[0]], y * v2.0[or[1]], z * v2.0[or[2]]]);
let diff = *v1 - v;
counts.entry((diff, or, sign)).or_default().push(*v1);
}
}
}
}
if let Some(((distance, orientation, signs), list)) =
counts.into_iter().find(|(_k, v)| v.len() >= 12)
{
// s1's points should already be in absolute coords. s2 will be translated in
// part1().
return Some(Match {
abs_points: list,
distance,
orientation,
signs,
});
}
None
}
fn parse(input: &str) -> Result<Vec<Scanner>> {
input.split("\n\n").map(|s| Ok(s.parse()?)).collect()
}
#[aoc(day19, part1)]
fn part1(input: &str) -> Result<usize> {
let mut scanner = parse(input)?;
// Assign the first scanner to the origin (0,0,0).
// Put that in a list of recently registered scanners.
// In a loop
// - For each recently registered scanner, attempt to find overlap with each unregistered
// scanner.
// - Matches should be translated according to the offsets found during the match. This should
// put them in absolute space.
// - Each match should be added to the recently registered list for the next iteration.
// - Do this until all scanners are registered.
scanner[0].offset = Some(Vec3::default());
let (mut registered, mut unregistered): (VecDeque<_>, VecDeque<_>) =
scanner.into_iter().partition(|s| s.offset.is_some());
let mut becons = HashSet::new();
let mut done = Vec::new();
while let Some(reg) = registered.pop_front() {
let mut unregs = VecDeque::new();
for mut unreg in unregistered {
if let Some(mat) = find_overlap(&reg, &unreg) {
unreg.offset = Some(mat.distance);
unreg.translate(mat.distance, mat.orientation, mat.signs);
println!(
"scanner {} @ {:?} found {} hits",
&unreg.id,
&unreg.offset.unwrap(),
mat.abs_points.len()
);
registered.push_back(unreg);
for pt in mat.abs_points {
becons.insert(pt);
}
} else {
unregs.push_back(unreg);
}
}
done.push(reg);
unregistered = unregs;
}
println!("before pass 2: {}", becons.len());
for i in 0..registered.len() {
for j in i..registered.len() {
let s1 = &registered[i];
let s2 = &registered[j];
if let Some(mat) = find_overlap(s1, s2) {
for pt in mat.abs_points {
becons.insert(pt);
}
}
}
}
println!("after pass 2: {}", becons.len());
//assert_eq!(done.len(), 12);
let mut becons: Vec<_> = becons.iter().collect();
becons.sort();
dbg!(&becons);
Ok(becons.len())
}
/*
#[aoc(day19, part2)]
fn part2(input: &str) -> Result<usize> {
todo!("part2");
Ok(0)
}
*/
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_overlap() -> Result<()> {
use pretty_assertions::assert_eq;
let input = r#"
--- scanner 0 ---
404,-588,-901
528,-643,409
-838,591,734
390,-675,-793
-537,-823,-458
-485,-357,347
-345,-311,381
-661,-816,-575
-876,649,763
-618,-824,-621
553,345,-567
474,580,667
-447,-329,318
-584,868,-557
544,-627,-890
564,392,-477
455,729,728
-892,524,684
-689,845,-530
423,-701,434
7,-33,-71
630,319,-379
443,580,662
-789,900,-551
459,-707,401
--- scanner 1 ---
686,422,578
605,423,415
515,917,-361
-336,658,858
95,138,22
-476,619,847
-340,-569,-846
567,-361,727
-460,603,-452
669,-402,600
729,430,532
-500,-761,534
-322,571,750
-466,-666,-811
-429,-592,574
-355,545,-477
703,-491,-529
-328,-685,520
413,935,-424
-391,539,-444
586,-435,557
-364,-763,-893
807,-499,-711
755,-354,-619
553,889,-390
"#
.trim();
let mut abs_points: Vec<Vec3> = r#"
-618,-824,-621
-537,-823,-458
-447,-329,318
404,-588,-901
544,-627,-890
528,-643,409
-661,-816,-575
390,-675,-793
423,-701,434
-345,-311,381
459,-707,401
-485,-357,347
"#
.trim()
.lines()
.map(|l| l.parse().unwrap())
.collect();
abs_points.sort();
let orientation = [0, 1, 2];
let signs = [-1, 1, -1];
let distance = Vec3([68, -1246, -43]);
let want = Match {
distance,
abs_points,
orientation,
signs,
};
let scanners = parse(input)?;
let mut got = find_overlap(&scanners[0], &scanners[1]).unwrap();
got.abs_points.sort();
assert_eq!(want, got);
Ok(())
}
#[test]
fn test_part1() -> Result<()> {
let input = r#"
--- scanner 0 ---
404,-588,-901
528,-643,409
-838,591,734
390,-675,-793
-537,-823,-458
-485,-357,347
-345,-311,381
-661,-816,-575
-876,649,763
-618,-824,-621
553,345,-567
474,580,667
-447,-329,318
-584,868,-557
544,-627,-890
564,392,-477
455,729,728
-892,524,684
-689,845,-530
423,-701,434
7,-33,-71
630,319,-379
443,580,662
-789,900,-551
459,-707,401
--- scanner 1 ---
686,422,578
605,423,415
515,917,-361
-336,658,858
95,138,22
-476,619,847
-340,-569,-846
567,-361,727
-460,603,-452
669,-402,600
729,430,532
-500,-761,534
-322,571,750
-466,-666,-811
-429,-592,574
-355,545,-477
703,-491,-529
-328,-685,520
413,935,-424
-391,539,-444
586,-435,557
-364,-763,-893
807,-499,-711
755,-354,-619
553,889,-390
--- scanner 2 ---
649,640,665
682,-795,504
-784,533,-524
-644,584,-595
-588,-843,648
-30,6,44
-674,560,763
500,723,-460
609,671,-379
-555,-800,653
-675,-892,-343
697,-426,-610
578,704,681
493,664,-388
-671,-858,530
-667,343,800
571,-461,-707
-138,-166,112
-889,563,-600
646,-828,498
640,759,510
-630,509,768
-681,-892,-333
673,-379,-804
-742,-814,-386
577,-820,562
--- scanner 3 ---
-589,542,597
605,-692,669
-500,565,-823
-660,373,557
-458,-679,-417
-488,449,543
-626,468,-788
338,-750,-386
528,-832,-391
562,-778,733
-938,-730,414
543,643,-506
-524,371,-870
407,773,750
-104,29,83
378,-903,-323
-778,-728,485
426,699,580
-438,-605,-362
-469,-447,-387
509,732,623
647,635,-688
-868,-804,481
614,-800,639
595,780,-596
--- scanner 4 ---
727,592,562
-293,-554,779
441,611,-461
-714,465,-776
-743,427,-804
-660,-479,-426
832,-632,460
927,-485,-438
408,393,-506
466,436,-512
110,16,151
-258,-428,682
-393,719,612
-211,-452,876
808,-476,-593
-575,615,604
-485,667,467
-680,325,-822
-627,-443,-432
872,-547,-609
833,512,582
807,604,487
839,-516,451
891,-625,532
-652,-548,-490
30,-46,-14
"#
.trim();
assert_eq!(part1(input)?, 79);
Ok(())
}
/*
#[test]
fn test_part2()->Result<()> {
let input = r#"
"#
.trim();
assert_eq!(part2(input)?, usize::MAX);
Ok(())
}
*/
}

199
2021/src/day20.rs Normal file
View File

@@ -0,0 +1,199 @@
use advent::prelude::*;
use aoc_runner_derive::aoc;
use std::ops::RangeInclusive;
struct Image(HashSet<(isize, isize)>);
impl Image {
fn new(input: &str) -> Image {
let rows: Vec<_> = input.lines().collect();
let width = rows[0].len();
Image(
rows.iter()
.flat_map(|row| row.as_bytes().iter())
.enumerate()
.filter(|(_i, b)| *b == &b'#')
.map(|(i, _b)| ((i % width) as isize, (i / width) as isize))
.collect(),
)
}
fn lookup(
&self,
x: isize,
y: isize,
algo: &[bool],
odd: bool,
x_rng: &RangeInclusive<isize>,
y_rng: &RangeInclusive<isize>,
) -> usize {
assert_eq!(algo.len(), 512);
let mut idx = 0;
for y_off in -1..=1 {
for x_off in -1..=1 {
let x_idx = x + x_off;
let y_idx = y + y_off;
let out_of_bounds = !(x_rng.contains(&x_idx) && y_rng.contains(&y_idx));
let val = if (odd && out_of_bounds && algo[0]) || self.0.contains(&(x_idx, y_idx)) {
1
} else {
0
};
idx <<= 1;
idx |= val;
}
}
idx
}
fn extents(&self) -> (isize, isize, isize, isize) {
self.0.iter().fold(
(isize::MAX, isize::MIN, isize::MAX, isize::MIN),
|(min_x, max_x, min_y, max_y), (x, y)| {
(min_x.min(*x), max_x.max(*x), min_y.min(*y), max_y.max(*y))
},
)
}
fn enhance(&self, algo: &[bool], odd: bool) -> Image {
let (min_x, max_x, min_y, max_y) = self.extents();
let x_rng = min_x..=max_x;
let y_rng = min_y..=max_y;
let mut new_im = HashSet::new();
for y in min_y - 1..=max_y + 1 {
for x in min_x - 1..=max_x + 1 {
let idx = self.lookup(x, y, algo, odd, &x_rng, &y_rng);
if algo[idx] {
new_im.insert((x, y));
}
}
}
Image(new_im)
}
fn lights(&self) -> usize {
self.0.len()
}
fn crop(&self, min_x: isize, max_x: isize, min_y: isize, max_y: isize) -> Image {
let x_rng = min_x..=max_x;
let y_rng = min_y..=max_y;
Image(
self.0
.iter()
.filter(|(x, y)| x_rng.contains(x) && y_rng.contains(y))
.cloned()
.collect(),
)
}
}
impl Debug for Image {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
let (min_x, max_x, min_y, max_y) = self.extents();
writeln!(f, "({}..{})x({}..{})", min_x, max_x, min_y, max_y)?;
for y in min_y..=max_y {
for x in min_x..=max_x {
if self.0.contains(&(x, y)) {
write!(f, "#")?;
} else {
write!(f, ".")?;
}
}
writeln!(f)?;
}
Ok(())
}
}
fn process(im: Image, algo: &[bool], num_steps: isize) -> Image {
let mut im = im;
for step in 0..num_steps {
let (min_x, max_x, min_y, max_y) = im.extents();
im = im.enhance(algo, step % 2 == 1);
im = im.crop(min_x - 1, max_x + 1, min_y - 1, max_y + 1)
}
im
}
#[aoc(day20, part1)]
fn part1(input: &str) -> Result<usize> {
let (algo, im) = input.split_once("\n\n").unwrap();
let im = Image::new(im);
let algo: Vec<bool> = algo.as_bytes().iter().map(|c| c == &b'#').collect();
let im = process(im, &algo, 2);
dbg!(&im, im.lights());
let answer = im.lights();
assert!(answer == 5268 || answer == 35);
Ok(answer)
}
#[aoc(day20, part2)]
fn part2(input: &str) -> Result<usize> {
let (algo, im) = input.split_once("\n\n").unwrap();
let im = Image::new(im);
let algo: Vec<bool> = algo.as_bytes().iter().map(|c| c == &b'#').collect();
let im = process(im, &algo, 50);
dbg!(&im, im.lights());
let answer = im.lights();
assert!(answer < 19245);
Ok(answer)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lookup() -> Result<()> {
let input = r#"
..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#
#..#.
#....
##..#
..#..
..###
"#
.trim();
let (algo, im) = input.split_once("\n\n").unwrap();
let im = Image::new(im);
let algo: Vec<bool> = algo.as_bytes().iter().map(|c| c == &b'#').collect();
let (min_x, max_x, min_y, max_y) = im.extents();
assert_eq!(
im.lookup(2, 2, &algo, false, &(min_x..=max_x), &(min_y..=max_y)),
34,
);
Ok(())
}
#[test]
fn test_part1() -> Result<()> {
let input = r#"
..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#
#..#.
#....
##..#
..#..
..###
"#
.trim();
assert_eq!(part1(input)?, 35);
Ok(())
}
#[test]
fn test_part2() -> Result<()> {
let input = r#"
..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#
#..#.
#....
##..#
..#..
..###
"#
.trim();
assert_eq!(part2(input)?, 3351);
Ok(())
}
}

171
2021/src/day21.rs Normal file
View File

@@ -0,0 +1,171 @@
use advent::prelude::*;
use aoc_runner_derive::aoc;
#[derive(Copy, Clone, Debug)]
struct Player {
tally: usize,
score: usize,
}
impl Player {
fn space(&self) -> usize {
(self.tally % 10) + 1
}
}
#[derive(Debug, Default)]
struct Die {
roll_count: usize,
}
impl Die {
fn roll(&mut self) -> usize {
let val = (self.roll_count % 100) + 1;
self.roll_count += 1;
val
}
}
fn take_turn(p: &mut Player, die: &mut Die) -> bool {
p.tally += die.roll() + die.roll() + die.roll();
p.score += p.space();
if p.score >= 1000 {
return true;
}
false
}
#[aoc(day21, part1)]
fn part1(input: &str) -> Result<usize> {
let mut p: Vec<_> = input
.lines()
.map(|l| l.split_once(": ").unwrap())
.map(|(_, space)| space.parse().expect("couldn't parse starting spaceition"))
.map(|space: usize| Player {
tally: space - 1,
score: 0,
})
.collect();
let mut die = Die::default();
loop {
if take_turn(&mut p[0], &mut die) {
return Ok(die.roll_count * p[1].score);
}
//println!( "Player 1 space {} for a total score of {}.", p[0].space(), p[0].score);
if take_turn(&mut p[1], &mut die) {
return Ok(die.roll_count * p[0].score);
}
//println!( "Player 2 space {} for a total score of {}.", p[1].space(), p[1].score);
}
}
fn play_part2(p1: Player, p2: Player) -> (usize, usize) {
fn play_part2_rec(
mut p1: Player,
mut p2: Player,
r1: usize,
r2: usize,
r3: usize,
r4: usize,
r5: usize,
r6: usize,
) -> (usize, usize) {
//println!( "p1 {} {} p2 {} {} die {} {} {} {} {} {}", p1.score, p1.space(), p2.score, p2.space(), r1, r2, r3, r4, r5, r6,);
p1.tally += r1 + r2 + r3;
p1.score += p1.space();
if p1.score >= 21 {
return (1, 0);
}
p2.tally += r4 + r5 + r6;
p2.score += p2.space();
if p2.score >= 21 {
return (0, 1);
}
let mut p1_score = 0;
let mut p2_score = 0;
for i in [1, 2, 3] {
for j in [1, 2, 3] {
for k in [1, 2, 3] {
for x in [1, 2, 3] {
for y in [1, 2, 3] {
for z in [1, 2, 3] {
let (p1s, p2s) = play_part2_rec(p1, p2, i, j, k, x, y, z);
p1_score += p1s;
p2_score += p2s;
}
}
}
}
}
}
(p1_score, p2_score)
}
let mut p1_score = 0;
let mut p2_score = 0;
for i in [1, 2, 3] {
for j in [1, 2, 3] {
for k in [1, 2, 3] {
for x in [1, 2, 3] {
for y in [1, 2, 3] {
for z in [1, 2, 3] {
let (p1s, p2s) = play_part2_rec(p1, p2, i, j, k, x, y, z);
p1_score += p1s;
p2_score += p2s;
println!("Running score {} vs {}", p1_score, p2_score);
}
}
}
}
}
}
(p1_score, p2_score)
}
//#[aoc(day21, part2)]
fn part2(input: &str) -> Result<usize> {
let p: Vec<_> = input
.lines()
.map(|l| l.split_once(": ").unwrap())
.map(|(_, space)| space.parse().expect("couldn't parse starting spaceition"))
.map(|space: usize| Player {
tally: space - 1,
score: 0,
})
.collect();
let (p1_wins, p2_wins) = play_part2(p[0], p[1]);
Ok(if p1_wins > p2_wins { p1_wins } else { p2_wins })
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part1() -> Result<()> {
let input = r#"
Player 1 starting position: 4
Player 2 starting position: 8
"#
.trim();
assert_eq!(part1(input)?, 739785);
Ok(())
}
//#[test]
fn test_part2() -> Result<()> {
let input = r#"
Player 1 starting position: 4
Player 2 starting position: 8
"#
.trim();
assert_eq!(part2(input)?, 444356092776315);
Ok(())
}
}

214
2021/src/day22.rs Normal file
View File

@@ -0,0 +1,214 @@
use advent::prelude::*;
use aoc_runner_derive::aoc;
#[derive(Debug)]
struct Instruction {
on: bool,
x_rng: RangeInclusive<i64>,
y_rng: RangeInclusive<i64>,
z_rng: RangeInclusive<i64>,
}
impl FromStr for Instruction {
type Err = Infallible;
fn from_str(input: &str) -> std::result::Result<Instruction, Infallible> {
// on x=11..13,y=11..13,z=11..13
// off x=9..11,y=9..11,z=9..11
let (verb, rest) = input.split_once(' ').unwrap();
let on = match verb {
"on" => true,
"off" => false,
_ => unreachable!("unexpected instruction type"),
};
let parts: Vec<_> = rest.split(',').collect();
let parse_rng = |s: &str| -> RangeInclusive<i64> {
s.split_once('=')
.unwrap()
.1
.split_once("..")
.map(|(lo, hi)| (lo.parse().unwrap(), hi.parse().unwrap()))
.map(|(lo, hi)| lo..=hi)
.unwrap()
};
let x_rng = parse_rng(parts[0]);
let y_rng = parse_rng(parts[1]);
let z_rng = parse_rng(parts[2]);
Ok(Instruction {
on,
x_rng,
y_rng,
z_rng,
})
}
}
fn part1_apply(insts: Vec<Instruction>) -> usize {
let mut grid = HashSet::new();
for inst in &insts {
dbg!(&inst);
for x in inst.x_rng.clone() {
for y in inst.y_rng.clone() {
for z in inst.z_rng.clone() {
if inst.on {
grid.insert((x, y, z));
} else {
grid.remove(&(x, y, z));
}
}
}
}
}
grid.len()
}
fn inbounds(r: &RangeInclusive<i64>) -> bool {
// lazy but good enough for part1
r.start().abs() <= 50
}
#[aoc(day22, part1)]
fn part1(input: &str) -> Result<usize> {
let insts: Vec<Instruction> = input
.lines()
.map(|l| l.parse().expect("failed to parse instruction"))
.filter(|i: &Instruction| inbounds(&i.x_rng) && inbounds(&i.y_rng) && inbounds(&i.z_rng))
.collect();
dbg!(&insts);
Ok(part1_apply(insts))
}
//#[aoc(day22, part2)]
fn part2(input: &str) -> Result<usize> {
let insts: Vec<Instruction> = input
.lines()
.map(|l| l.parse().expect("failed to parse instruction"))
.collect();
dbg!(&insts);
for i in insts {
if i.x_rng.end()
- i.x_rng.start() * i.y_rng.end()
- i.y_rng.start() * i.z_rng.end()
- i.z_rng.start()
== 2758514936282235
{
println!("magic instructions {:?}", i)
}
}
Ok(0)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part1() -> Result<()> {
let input = r#"
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
"#
.trim();
assert_eq!(part1(input)?, 39);
let input = r#"
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
"#
.trim();
assert_eq!(part1(input)?, 590784);
Ok(())
}
//#[test]
fn test_part2() -> Result<()> {
let input = r#"
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
"#
.trim();
assert_eq!(part2(input)?, 2758514936282235);
Ok(())
}
}

41
2021/src/day23.rs Normal file
View File

@@ -0,0 +1,41 @@
use advent::prelude::*;
use aoc_runner_derive::aoc;
#[aoc(day23, part1)]
fn part1(input: &str) -> Result<usize> {
todo!("part1");
Ok(0)
}
/*
#[aoc(day23, part2)]
fn part2(input: &str) -> Result<usize> {
todo!("part2");
Ok(0)
}
*/
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part1() -> Result<()> {
let input = r#"
"#
.trim();
assert_eq!(part1(input)?, usize::MAX);
Ok(())
}
/*
#[test]
fn test_part2()->Result<()> {
let input = r#"
"#
.trim();
assert_eq!(part2(input)?, usize::MAX);
Ok(())
}
*/
}

View File

@@ -161,8 +161,7 @@ impl MarkerBoard {
let m = self.0;
// Bingo horizontally
false
|| (m & h == h)
(m & h == h)
|| ((m >> 5 & h) == h)
|| ((m >> 10 & h) == h)
|| ((m >> 15 & h) == h)

View File

@@ -3,8 +3,17 @@ pub mod day10;
pub mod day11;
pub mod day12;
pub mod day13;
pub mod day14;
//pub mod day14;
pub mod day15;
pub mod day16;
pub mod day17;
//pub mod day18;
pub mod day19;
pub mod day2;
pub mod day20;
pub mod day21;
pub mod day22;
//pub mod day23;
pub mod day3;
pub mod day4;
pub mod day5;

View File

@@ -1,13 +1,5 @@
use std::{
fmt::{Debug, Error, Formatter},
num::ParseIntError,
ops::{Index, IndexMut},
str::FromStr,
};
use anyhow::Result;
use aoc_runner_derive::{aoc, aoc_generator};
use thiserror::Error;
use advent::prelude::*;
use aoc_runner_derive::aoc;
#[aoc(dayX, part1)]
fn part1(input: &str) -> Result<usize> {

16
Cargo.toml Normal file
View File

@@ -0,0 +1,16 @@
[package]
name = "advent"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.45"
aoc-runner = "0.3.0"
aoc-runner-derive = "0.3.0"
pretty_assertions = "1.0.0"
thiserror = "1.0.30"
[lib]
name = "advent"

View File

@@ -1 +1,2 @@
imports_granularity = "Crate"
format_code_in_doc_comments = true

95
src/image.rs Normal file
View File

@@ -0,0 +1,95 @@
use crate::prelude::*;
pub struct Image {
width: usize,
height: usize,
pixels: Vec<u8>,
}
impl Image {
fn kernel3x3<F>(&mut self, (x, y): (usize, usize), func: F)
where
F: Fn(u8) -> u8,
{
if x > 0 {
self[(x - 1, y)] = func(self[(x - 1, y)]);
if y > 0 {
self[(x - 1, y - 1)] = func(self[(x - 1, y - 1)]);
}
if y < self.height - 1 {
self[(x - 1, y + 1)] = func(self[(x - 1, y + 1)]);
}
}
if y > 0 {
self[(x, y - 1)] = func(self[(x, y - 1)]);
}
self[(x, y)] = func(self[(x, y)]);
if y < self.height - 1 {
self[(x, y + 1)] = func(self[(x, y + 1)]);
}
if x < self.width - 1 {
self[(x + 1, y)] = func(self[(x + 1, y)]);
if y > 0 {
self[(x + 1, y - 1)] = func(self[(x + 1, y - 1)]);
}
if y < self.height - 1 {
self[(x + 1, y + 1)] = func(self[(x + 1, y + 1)]);
}
}
}
}
impl PartialEq for Image {
fn eq(&self, other: &Self) -> bool {
self.width == other.width && self.height == other.height && self.pixels == other.pixels
}
}
impl Debug for Image {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
writeln!(f)?;
for y in 0..self.height {
for x in 0..self.width {
write!(f, "{:2}", self[(x, y)] as char)?;
}
writeln!(f)?;
}
Ok(())
}
}
impl FromStr for Image {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let rows: Vec<_> = s.lines().collect();
let width = rows[0].len();
let height = rows.len();
let pixels = rows
.iter()
.flat_map(|row| row.as_bytes().iter())
.map(|b| *b)
.collect();
Ok(Image {
width,
height,
pixels,
})
}
}
impl Index<(usize, usize)> for Image {
type Output = u8;
fn index(&self, (x, y): (usize, usize)) -> &Self::Output {
&self.pixels[x + y * self.width]
}
}
impl IndexMut<(usize, usize)> for Image {
fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut Self::Output {
&mut self.pixels[x + y * self.width]
}
}

18
src/lib.rs Normal file
View File

@@ -0,0 +1,18 @@
pub mod prelude {
pub use std::{
collections::{HashMap, HashSet, VecDeque},
convert::Infallible,
fmt::{Debug, Display, Error, Formatter},
io::Read,
num::ParseIntError,
ops::{Index, IndexMut, RangeInclusive},
str::FromStr,
};
pub use anyhow::Result;
pub use thiserror::Error;
pub use crate::image::Image;
}
mod image;