Add Solution for 9
This commit is contained in:
1 parent
65010487b7
commit
69bf933f03
2 files changed
+269
No files matched your search
@@ -0,0 +1,268 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 26,
|
||||
"metadata": {
|
||||
"id": "CdxCMssgVGSz"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open('input', 'r') as infile:\n",
|
||||
" input = infile.read().strip()\n",
|
||||
"#input = r'''2333133121414131402'''\n",
|
||||
"#input"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 27,
|
||||
"metadata": {
|
||||
"id": "-T-QxruZkBUl"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def expand(diskmap):\n",
|
||||
" expanded_output = []\n",
|
||||
" for idx,char in enumerate(diskmap):\n",
|
||||
" id = idx//2 if (idx%2) == 0 else -(idx//2)-1\n",
|
||||
" length = int(char)\n",
|
||||
" expanded_output.append((id,length))\n",
|
||||
" return expanded_output\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 28,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "nsDNp82qk4pE",
|
||||
"outputId": "0478d7c1-d327-4d01-dd05-852dd1d7c54f"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"0.....111111.......222222.........33333........444444445.....66......77.........88......99999999.101010101010101010111111.......121212.........131313.....141414141414......151515151515151515........161616....17171717....18.1919..202020202020.......21.222222222222....2323232323232323....242424....252525.....2626262626.........27272727......2828282828282828.....292929292929292929....303030303030.313131.......323232323232..33333333333333...3434343434.......35..363636363636.........373737373737373737..3838........393939...40404040404040.....4141414141.....424242424242424242........434343.........44.....45454545454545.46...4747474747....484848.....4949495050505050505050...51515151515151.......5252525252525252.535353.545454545454545454....555555555555........5656565656565656..57575757575757.........585858585858595959595959595959........6060606060606060.....6161616161....6262626262626262........6363636363......646464646464646464......656565656565.....66666666666666........6767676767.686868686868686868....696969696969696969.........7070707070...717171717171.........7272727273...74747474747474...7575757575......76767676767676767677...787878787878.....79797979......8080808080......81818181.....8282828282828282828383838383....848484...8585....868686...878787878787878787.888888888888.....898989898989.....90909090909090.919191.92...9393939393........94949494........95959595959595.....969696969696969696.97979797979797979898989898989898.....99999999999999.........100101101101101101101.........102102102102102......103103103103103103103103103......104104104104104104104.........105105105105106106....107107107107107107107....108108108108108108109109109109109109109109......110110110110110110110110110....111111111111112112112112112112112112112.113113113113113113113113....114114.........115115115115115115115115115..116116116116...117117117117117118118118118118118118.......119.120..121121121121121121121..122.......123123123123123123123124124124124.....125125125125125125126126126126126126126126......127127127127.......128128128128........129129129129129129129........130130130130130130130131131131........132132132132132132132132132........133134.....135135135135135135..136136136136136136.........137137137137137......138138138138....139139139139139139139........140140........141.......142142142142142142142142..143143143143143143143....144144144144144144......145145145145145145145145...146146...147147147147...148148148148148........149149149149149149.150150150150150150150150150.151151151151151151151151151.152152152152......153153153153153153153154154154154154154154....155..156156156156156157157.....158158158158158158...159159159159.....160160160......161161161161.162162.....163163163163163163163.....164........165165165165165165........166166166166..167167167167167..168168.....169.........170....171171171171..172........173173173173173........174174174.........175175175175175175175175.......176176176176176176176176...177177177177177177177..178178178178178178178......179179179179179179179179180180180180180180180.........181181181181.....182182182182182......183183183183183183183183183.........184184184184184184184...185185185185185185185186186186186186..187187187187187187.....188188..189189189189189......190190190190191......192192.....193....194194........195.......196197197197197197197.....198198198198198198198198........199199199199199199.200200200200200......201201201.202202202202202202202202202.203203......204204204204204......205205205205.206206206206206206206206206...207207207207207207207208208208208208208.........209209209..210210......211211211211211..212212212212212212212..213........214214214214214..215215215215........216216216216216216216216.217217217217217217217..218218218218.........219219220220220220220220220220..221221221221.......222222222222222222222222.....223223223223223223223223223.224224224224224....225225225225225225225225........226226......227227227227227227......228228228228228228228228228..229..230230230230230230.........231231231231231231.232232232232........233233233233233233..234234...235235235235235235235235236236236236.237.238238238238...239239239239239.....240240240240240240240240240241241241241241241.......242242242242242242........243......244.........245245245245245245245245245....246246246246246........247247247.......248.....249249249249....250250250250250250250250.251251251251251...252252252252252..253253...254254254..255255255255255255255255255..256256256256..257257.....258258258258..259259259259259259.........260260.........261261.262262262262262262..263263263263263263263263.........264264264264264264264...265265265265265265...266........267267267267267..268268268..269270....271271271271.......272272272272272.....273273273273273273273..274274274274274274274......275275275275275275275275..276276276...277277277.....278278278.279279279279279279279279...280280280280280280280.......281282282282282282282.........283283283283283283284284284284284284.......2852852852852852852852862862862862862Line truncated
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def printdisk(diskmap):\n",
|
||||
" for element in diskmap:\n",
|
||||
" char = \".\" if element[0] < 0 else f\"{element[0]}\"\n",
|
||||
" print(f\"{char*element[1]}\",end=\"\")\n",
|
||||
"\n",
|
||||
"printdisk(expand(input))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 29,
|
||||
"metadata": {
|
||||
"id": "7DDQ6sMNmYD-"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def defrag(diskmap):\n",
|
||||
" while diskmap:\n",
|
||||
" current = diskmap.pop(0)\n",
|
||||
" if current[0] >= 0:\n",
|
||||
" yield current\n",
|
||||
" elif current[0] < 0: #empty > try to fill in\n",
|
||||
" if diskmap:\n",
|
||||
" last = diskmap.pop(-1) # pop last\n",
|
||||
" if last[0] >= 0: # if data\n",
|
||||
" yield (last[0],min(current[1],last[1])) # yield as much data as fits in the gap\n",
|
||||
" if current[1] > last[1]: # put rest of empty back on front\n",
|
||||
" diskmap.insert(0,(-1,current[1]-last[1]))\n",
|
||||
" elif current[1] < last[1]: # put rest of data back at end\n",
|
||||
" diskmap.append((last[0],last[1]-current[1]))\n",
|
||||
" elif last[0]<0: # if last is empty > discard empty put back data and retry\n",
|
||||
" diskmap.insert(0,current)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 30,
|
||||
"metadata": {
|
||||
"id": "ylVF3ucJ-hVd"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def checksum(diskmap):\n",
|
||||
" values = [value for value, count in diskmap for _ in range(count)]\n",
|
||||
" return sum([i*x for i,x in enumerate(values) if x>0])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "geM-oK32TyGe"
|
||||
},
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "wgvPNj8WT6jY"
|
||||
},
|
||||
"source": [
|
||||
"# Solution for first part"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 31,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "IpLDHvcE1YIa",
|
||||
"outputId": "7821df1c-38a3-42c0-95a2-b09f59b71766"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"0.....111111.......222222.........33333........444444445.....66......77.........88......99999999.101010101010101010111111.......121212.........131313.....141414141414......151515151515151515........161616....17171717....18.1919..202020202020.......21.222222222222....2323232323232323....242424....252525.....2626262626.........27272727......2828282828282828.....292929292929292929....303030303030.313131.......323232323232..33333333333333...3434343434.......35..363636363636.........373737373737373737..3838........393939...40404040404040.....4141414141.....424242424242424242........434343.........44.....45454545454545.46...4747474747....484848.....4949495050505050505050...51515151515151.......5252525252525252.535353.545454545454545454....555555555555........5656565656565656..57575757575757.........585858585858595959595959595959........6060606060606060.....6161616161....6262626262626262........6363636363......646464646464646464......656565656565.....66666666666666........6767676767.686868686868686868....696969696969696969.........7070707070...717171717171.........7272727273...74747474747474...7575757575......76767676767676767677...787878787878.....79797979......8080808080......81818181.....8282828282828282828383838383....848484...8585....868686...878787878787878787.888888888888.....898989898989.....90909090909090.919191.92...9393939393........94949494........95959595959595.....969696969696969696.97979797979797979898989898989898.....99999999999999.........100101101101101101101.........102102102102102......103103103103103103103103103......104104104104104104104.........105105105105106106....107107107107107107107....108108108108108108109109109109109109109109......110110110110110110110110110....111111111111112112112112112112112112112.113113113113113113113113....114114.........115115115115115115115115115..116116116116...117117117117117118118118118118118118.......119.120..121121121121121121121..122.......123123123123123123123124124124124.....125125125125125125126126126126126126126126......127127127127.......128128128128........129129129129129129129........130130130130130130130131131131........132132132132132132132132132........133134.....135135135135135135..136136136136136136.........137137137137137......138138138138....139139139139139139139........140140........141.......142142142142142142142142..143143143143143143143....144144144144144144......145145145145145145145145...146146...147147147147...148148148148148........149149149149149149.150150150150150150150150150.151151151151151151151151151.152152152152......153153153153153153153154154154154154154154....155..156156156156156157157.....158158158158158158...159159159159.....160160160......161161161161.162162.....163163163163163163163.....164........165165165165165165........166166166166..167167167167167..168168.....169.........170....171171171171..172........173173173173173........174174174.........175175175175175175175175.......176176176176176176176176...177177177177177177177..178178178178178178178......179179179179179179179179180180180180180180180.........181181181181.....182182182182182......183183183183183183183183183.........184184184184184184184...185185185185185185185186186186186186..187187187187187187.....188188..189189189189189......190190190190191......192192.....193....194194........195.......196197197197197197197.....198198198198198198198198........199199199199199199.200200200200200......201201201.202202202202202202202202202.203203......204204204204204......205205205205.206206206206206206206206206...207207207207207207207208208208208208208.........209209209..210210......211211211211211..212212212212212212212..213........214214214214214..215215215215........216216216216216216216216.217217217217217217217..218218218218.........219219220220220220220220220220..221221221221.......222222222222222222222222.....223223223223223223223223223.224224224224224....225225225225225225225225........226226......227227227227227227......228228228228228228228228228..229..230230230230230230.........231231231231231231.232232232232........233233233233233233..234234...235235235235235235235235236236236236.237.238238238238...239239239239239.....240240240240240240240240240241241241241241241.......242242242242242242........243......244.........245245245245245245245245245....246246246246246........247247247.......248.....249249249249....250250250250250250250250.251251251251251...252252252252252..253253...254254254..255255255255255255255255255..256256256256..257257.....258258258258..259259259259259259.........260260.........261261.262262262262262262..263263263263263263263263.........264264264264264264264...265265265265265265...266........267267267267267..268268268..269270....271271271271.......272272272272272.....273273273273273273273..274274274274274274274......275275275275275275275275..276276276...277277277.....278278278.279279279279279279279279...280280280280280280280.......281282282282282282282.........283283283283283283284284284284284284.......2852852852852852852852862862862862862Line truncated
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"6399153661894"
|
||||
]
|
||||
},
|
||||
"execution_count": 31,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"a = expand(input)\n",
|
||||
"printdisk(a)\n",
|
||||
"b=[x for x in defrag(a)]\n",
|
||||
"printdisk(b)\n",
|
||||
"checksum(b)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "n8EQmBtlT8_a"
|
||||
},
|
||||
"source": [
|
||||
"# Second Part"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 32,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "fsCCqTbXU-1u",
|
||||
"outputId": "c74ac648-af20-419a-b54a-06f57fc4cb6f"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def get_empty(diskmap,size):\n",
|
||||
" for idx,element in enumerate(diskmap):\n",
|
||||
" if element[0] < 0:\n",
|
||||
" if element[1] >= size:\n",
|
||||
" return idx,element\n",
|
||||
"\n",
|
||||
"def defrag_part2(diskmap):\n",
|
||||
" data = [x for x in diskmap if x[0]>=0]\n",
|
||||
" for dat in reversed(data):\n",
|
||||
" min_id = min([x[0] for x in diskmap])-1\n",
|
||||
" dat_index = diskmap.index(dat)\n",
|
||||
" emtpy = get_empty(diskmap,dat[1])\n",
|
||||
" if emtpy:\n",
|
||||
" empty_index,empty = emtpy\n",
|
||||
" if empty_index < dat_index:\n",
|
||||
" empty_index = diskmap.index(empty)\n",
|
||||
" diskmap.insert(dat_index,(min_id,dat[1]))\n",
|
||||
" diskmap.remove(dat)\n",
|
||||
" diskmap.remove(empty)\n",
|
||||
" if empty[1] > dat[1]:\n",
|
||||
" diskmap.insert(empty_index,(empty[0],empty[1]-dat[1]))\n",
|
||||
" diskmap.insert(empty_index,dat)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 33,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"a = expand(input)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 34,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"defrag_part2(a)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 35,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "HgXQPF9CeHKF",
|
||||
"outputId": "a04ac99e-fcd2-4d65-b438-7bd2eb78f0b3"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"6421724645083"
|
||||
]
|
||||
},
|
||||
"execution_count": 35,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"checksum(a)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"provenance": []
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.10"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
1567695880152629268190373935669834441122671164843435594685946137627357126992283375559839157113543530837781319468827960988554885696657851949953694013735690136546564590543324339165657131135848759180857910695696794024746086944091842992435077111272177045608647487870389810156269564478281782746683234358619191467074125025634536412575186842522519144218583987837276807945569973705265225640162514281710658861563191265641937069322652721852488172492082478591548826669212696148622380411143559067681619945837154481535223329242254269292162897363185232101447557276823335318377106960677073128910388064829684559151545828269720244827964284853979732858274183198757286526887065427445535346102242214018913495285895792733497062779373646513478816371584779496951116998760422134488790326625909072902419106771414296366633568871447412579192513822296612397044478132673036608092781844101950935927373844479484764349673771237643161867121747989082642570249422345381114465343484303217723359266841223356548877415757165440795558854772893855439472757118262837832127777592507388173370918638801368833115852818267595191034432478589486177273482474129795588137931153386727879879425222402211928644988486632067709744851363953077678980838818647539614484279112974373107923631558733898924620591613617657969080112893367682322330295596566214443428993865321959318825794720422492226250634137894049613627575129413554945392137958646166179549136843553743156516207475542923833287845424643685448177351651856826107695987587988962613396813478746446838972697742858147924323544324615987601330399937647950367952513289357222527354898569724815706187912642162057805463559671619143527568361516417275655999292146542496644996543731304463412223791081445784155575468749585650868536965596362045496075654328772696149179149453891999827074505919925584454788576797867553276980228056335088248399667998567036347212767888478670421390579973946898546486392043225254739283381471899612539231139870929887608026119988765023283720134910855883104266585125445465121171152078927772864445195490501987563572218598518271981425621314457064958714846286916329763859596732259351793246209714737648618756965962693475655545961771161335408265321471369764386848364270793215825073139536509042602125579794811378463824782610433366773036759576821775109649912511383327195948814071583867759131913626447221455786547488513012442782588249349939361382397231401675627681937828186226281552544595547865541851341090604097608247547763662592429660746856365087502414339972579114934667953533678260923418845324502031548131949138135451508754222431126559231577976413891866837069956456144585101289762044352188675878851437356675492797646026495699283239165267205684167394499217256345964084633920585242859642322016401211132312871656241189946049824346406750575187627063427482566983115111513861596677264264992446797264119824982177651984881038999753295657611246148346876169396448149048507236388055779852622636101561635340203288915832647128752662561276193377912186558646139230888067326635806583935827872719701290999135942374838481976992737944104982627140587293133391901732259073471810817538894842908266963147941022691062486988162457787527413581451444736031543259317385391985115123442244338151939850102829471799696386947558355916806231378468411981366971574421958318101514783319317489694235669112619836894021274362819452149881801364742715462615111925536820844653806632228240486771975829797841874838668048253065634849589799736218817249322922241324297513679268711010273340449065603995179667626166209762428891385092358898319328944530595311788582699719305762429619365864612590653742671129286675283086293928805631865477561667149124674177262828419668175916667290874378781069744434403550186432811139372844133570583151746492218554447836665858864923366173205236371254673318246896892532348029123098264789391921319195841363463955529819179870633347785029109562943989846994231244787440712791164289204980287836861458368224442930537545954150922425353280822122641154341396596270566438917993824612994723205926148971534733809166503429347191189125997887387592742567673439925532661631601991225465288467926644224867421565943515626555421883581159254412182691311839834482184940782753965868374544446917128627168365894564308991128188159680476259149597641074127072443767718824101031327310888594608344392087819537507954394748828658284973381667178017162225656834709987138331785311556122711419433565971040684630534322839298308573265587417018366729161241138869631432523844128447946555501493595711969963527143754378203050397845857963174890934589108777838880826312614182459275568898613349921267837992427471938270616814294663754337831427144522986954897286298380516994105475349072558932381171917181126029336182847884294738814243359199999057242575343672508941978952726210883378125835892738233574212086567483533374555918819428699958301756862729521775338886988742708570582730555991364241658824733687196887461852925658701781472271848537585888176313742511114411804981417997795894535428999422852099329355359712315261565345819736956757119167872134176724597453555895728Line truncated
|
||||
Reference in new issue
Block a user