this repo has no description
3
fork

Configure Feed

Select the types of activity you want to include in your feed.

ft: add day 9

+242 -36
+100
2021/day9.txt
··· 1 + 5656921987125678979998760234965456789998768789239876323457896545467894567987232345679209876567998656 2 + 4349899876234599568987654345894345999987656678945965212348965434356943459876101956798912987459876541 3 + 5498789854345689467898765456789297898999736567899874323489654321248932398765219897987893498598765432 4 + 6989598765459789356989878767896345987432125456789985434578965432357953499954398789376789569679876543 5 + 9875459876569893249876989898965476976521012345699876645689876543456894689875679643234598998789987656 6 + 8764345987678989139765393959796567965432543457789987887792987654569965678976798432123467899899998867 7 + 9773239898789678999878212345697679876543656598999999998901298767678976789598987621012345943978929998 8 + 9652198789894567989992101556789799989854567679659898999992399899789987893459996532136756964569919999 9 + 8543999674923469879989232345899899998765678789798787899989989929896798932345987643245697897679897899 10 + 7659896543212359768978943456789997889876789896987656789878679210945679651267898789976789989998786789 11 + 8798789954563498957767894567899986678989898965432345678964567921234989743456989891988995679876545856 12 + 9987679899874987843456965678979875567999987654321239789443459432345899954589876910999434678995432545 13 + 9877578789989876532567899789765983458999898965410198999321598996476789875678965439876323789789541234 14 + 8765467678999997673458978997654901269998769876322397898945976989987899986789876598765212345678930347 15 + 4323234589989998789569769898769892978987654987439986787899865678998998997899989999874347456789821236 16 + 4310123689878999897678958789898799899987743498598765696789987799569987898999998789985456789897642345 17 + 6421234578956899998989345678987688789996532987699764545698998912499876949998989698976587899998759487 18 + 7834545789345778999997236789876567678987653698789843234567899943989989234997678587989798968789898998 19 + 8945656793234567899876345698765434589998974569898762126789999899879894349876583456899899655698997899 20 + 9658789932123458999765458999654323678909895689987651015897998789965789498765432345699921234987846789 21 + 8769894321012367899876567987721012789219789797896543123456789679874397679876321256789430349876734599 22 + 9889965439143456789987679876432125699998679896987654234899896598765498989997442345678921467965325678 23 + 3999877998956567897898999876543234789896598965498765856789965459876789899998656576789939569876566789 24 + 4599989867898789956789789989854345699765497894349976767897654367987895698999767678997898978987977892 25 + 5689998756999892347898698998768456789754346789234989878998796456798964787899898989445677899298998901 26 + 6899899845890901239986567769877687897673235692123999989449987567899763456789999894323456910149989992 27 + 7998789956791912398765436456998998987542124989239898793234598978999854667899898765212347891239878989 28 + 9898598767989899459954324365679549099993235978998769654395989899898765678998769986323456789398765678 29 + 8767459879876788969843210234568932198789549867989659875989876789769877889987653295434568996987654567 30 + 9856345998765657899764321246789845987699998759878943989878965678954989998998854598546878945699543458 31 + 9943235987654345678976532347899659876587899548767892398767954569893292677999765987657899234598754589 32 + 8632047998775457899876545498998998765456798732455921019655343456789101456899876798768910123459897678 33 + 7542123989896668999987756789787899654349986621434893198743212347893252367967987899979439294998998999 34 + 9753239876998789998999887897656798743298765410123789987654323458954543478945598999989598989897659599 35 + 7654449995979893987899998976545679854349874321245678999995934567895676569123499789999987678789343489 36 + 8765998784569932396998769987437789967656985432356789987889895678976987878994987678999876545679212978 37 + 9899887643458921985489954599545699878787898765467899996579789789987898989789986568998765432389343459 38 + 1998798532467939874367893987657899989898979887598969875468678993098999997656542456989876546456954568 39 + 0129654321256899765459932498768989692929567998989657965376578992129899876542101349876987687767897678 40 + 9298999432345678976578943569879976541012468999876549874212456789234799997653212498765498898878998889 41 + 8987678956756799698689654698998765432123567893986432987301234894345689998765323989874349999989329998 42 + 7676567898767894598798765987969876865238678992195410196513346965576796989865439876543236799893213567 43 + 8543458999879932989899878976845987974347889989954329987325457896678895678976545987754545698764301348 44 + 3212345789989549878942989895434598765456998876899498765436567987989934568987656899865656789965492349 45 + 2101676789397698767891098789423459876667897794768999887687678998999912347899767999876767899877989499 46 + 3212567893219985456932987679554567987798976643456899998798989999129893456789879987987878999989878989 47 + 4323456789998764387899876568965679698899765532345698779899294989098789567894989976598989789998757679 48 + 5434567899899765678989875457976789549989654341234987665989123978987678998903496987439395679986545567 49 + 6545678988789876789976982349897895434678943210129876534578939865654567899212345698921234598765432498 50 + 8766989876578987898995431298789954323567954321239884325989097654523458989423566999890123459878521389 51 + 9877899765467898967984310345698965212456895592398765456789198763012369878954879898791234567987632478 52 + 9989978984358999549865321234567892101569986989989879877998999832125489767895998787679987678996543567 53 + 9898867893267899432979832345698943452978998979878989989567899944345678956789987654567898989987654578 54 + 8767456792125678921098765456789654567889999767467899895456798767856789345991986543456789993398765689 55 + 7654346789334569432399886667896987698996987654348998754345679878969891239890995432545898942109876797 56 + 8743234579456696543989987778975698999785698765459879543234789989878992349789894321234567893299987896 57 + 9652125678967789654976599899654579987654569876767965432126567895989989498658789432356778954989999945 58 + 8721014567898998769865456976543434976543456987879896943013478953997878987545696545467899899878901234 59 + 9832123456789659978954348987632123987654568998998789892134567899876767897676789656578935698767892545 60 + 8743234568995443989875467896541015699879878949998678789255778998765455698797898789678926989659789676 61 + 7654345789654321295996568987432123456998989439876589678969899999654364759898919899799019976545689797 62 + 8965676898743210134987699898943296587897998919887434567899989788963213345999323999898998965434568989 63 + 9876789997654329245698789789894989799986897898776623456789876587892101296789454599987987654323679979 64 + 4987995789975998956789895699789678989875766987654512345696567456789742679896567989876398766554789568 65 + 3298954679899887967899934987654567878964345696543203456965435345897653498987979978964239987667895457 66 + 9989763456798776798989429898543476567895256987653212569896321256789776567899898769879125698998912345 67 + 8765432345987654789878998769452323479792123498765323456789432345899987678956789656998934569979101236 68 + 9899321296998765699869987654321012345689234789765434767896543656789398789547897997987897698654312377 69 + 0998910989899876986545699876542123467894345679876545678987656767891239895438456789876789798765425458 70 + 1987899876789987899634569987643444578965467989987687989698767878932367994321387899965678969886534567 71 + 9876798985678998998785678998754555678996879897698789094579978989973456789210998999864567954997545678 72 + 6765987654589899219896899679865766789987998789999892123467989698764567894339899598765899543987659889 73 + 5434599543456789995987954589976778997899987659899943234579692569875698965998789439976798762399767996 74 + 4323498432345899884398943478987889656768999548789867455698543459876789879887678945797897653989888945 75 + 5419987521236789765459654569998995443459998929689978996987656767987894998776569896898998769876999236 76 + 6598996432345799896598767679989764312379876213567899989698967878998932989675456679999889899965210123 77 + 7987987643457899989679898789876532101999954101377999877549878989999549876543234578998778989874321256 78 + 9896599754598999978989969898989953219889893215456789765434989991987657987832124567989656578995452345 79 + 8789459895689988767897654947898764598767789326767999874323497890198798998775012579876545456789678456 80 + 5678968987899877658989932236789879987656698987978999985212646789259999659654323456987434368999989567 81 + 6989879498998765434567890125678998798544567898989989432101239894349898745965434569874321239989998698 82 + 7897989369987654323456921234578987654123456789999978999219398965498789659876756998765442398978999799 83 + 8996593254598987312347892347689897543244789899878867878998997896987678967987987899876599976767899892 84 + 9989432123459876201234989498996798798765679998765756767987876789876567898999898913987988765356789921 85 + 9878941012367965312345678999345699899879789459854543459876565698985458799898789109899875434267896540 86 + 9767892125679878323456789987656789999989897698763212598765434597654345689798678998756964320134589432 87 + 8856999234569999474567898998967896798995979899854443679872123789986456895676569899549876534565678993 88 + 7545898945678998765678987899898965987654168998766558789983234895497587894123456789434987655676799789 89 + 6534767898799439976989876799789654496543054579878669893494545679398998973236789896545999768789897678 90 + 5423456999896521989898765687678943219832123567989778912965876893219349765445678987659878978897934569 91 + 7312346789995432398789954554567894398753234578999889999876987932101239876576789998798967989976545778 92 + 6101237799989545986566893213458965989964545679432999889987898993299545987689899999987652397999758889 93 + 4232345678978959875455799102348979877899656789599989678998929989988956798797999899876541656789767896 94 + 5643458989769898765344568923456798765678997899987878567899019876567897899896789789987630345689878945 95 + 6756567895456789654123579434568998654567989999996759456789198765458998998965675679898321234678999234 96 + 7887678943379899763234678945689876543234878998875642345689987654349899987654324598765432345899899165 97 + 8998789321234789854345789656999987632123569987984551234793498875456789998783213459876543656789678976 98 + 9549895452375699965656798767899874321012478976543210159892109986767899989654302345998754567897567897 99 + 0234976565467789798767899988998765434566567897865421767899212987898909876543213566789766678998486789 100 + 1655989876988894569989910199019876545679788998976562345678924598999212987654424587899887889239345678
+142 -36
2021/solutions.livemd
··· 10 10 ## Setup 11 11 12 12 ```elixir 13 - Mix.install([]) 13 + Mix.install([ 14 + {:nx, github: "elixir-nx/nx", sparse: "nx"}, 15 + {:kino, github: "livebook-dev/kino"} 16 + ]) 17 + ``` 18 + 19 + ```output 20 + * Getting nx (https://github.com/elixir-nx/nx.git) 21 + remote: Enumerating objects: 10786, done. 22 + remote: Counting objects: 100% (2841/2841), done. 23 + remote: Compressing objects: 100% (635/635), done. 24 + remote: Total 10786 (delta 2376), reused 2515 (delta 2195), pack-reused 7945 25 + origin/HEAD set to main 26 + * Getting kino (https://github.com/livebook-dev/kino.git) 27 + remote: Enumerating objects: 488, done. 28 + remote: Counting objects: 100% (488/488), done. 29 + remote: Compressing objects: 100% (351/351), done. 30 + remote: Total 488 (delta 269), reused 274 (delta 118), pack-reused 0 31 + origin/HEAD set to main 32 + ==> kino 33 + Compiling 19 files (.ex) 34 + Generated kino app 35 + ==> nx 36 + Compiling 23 files (.ex) 37 + Generated nx app 14 38 ``` 15 39 16 40 ```output ··· 565 589 566 590 max = Enum.max(input) 567 591 568 - 0..max 569 - |> Enum.reduce(:infinity, fn n, acc -> 570 - sum = 571 - input 572 - |> Enum.map(&arith_sum.(abs(&1 - n))) 573 - |> Enum.sum() 592 + mean = Enum.sum(input) / length(input) 574 593 575 - if sum < acc, do: sum, else: acc 594 + [floor(mean), ceil(mean)] 595 + |> Enum.map(fn n -> 596 + input 597 + |> Enum.map(&arith_sum.(abs(&1 - n))) 598 + |> Enum.sum() 576 599 end) 600 + |> Enum.min() 577 601 ``` 578 602 579 603 ```output ··· 593 617 |> String.trim() 594 618 |> String.split(" ") 595 619 |> Enum.map(fn disp -> 620 + # Sort characters in each entry to simplify later work 596 621 disp 597 622 |> String.to_charlist() 598 623 |> Enum.sort() ··· 636 661 637 662 ```elixir 638 663 defmodule Day8.Task2 do 639 - def a --- b do 640 - MapSet.difference(a, b) 641 - end 642 - 643 - def a +++ b do 644 - MapSet.union(a, b) 645 - end 664 + defp a --- b, do: MapSet.difference(a, b) 646 665 647 - def a <~> b do 648 - MapSet.intersection(a, b) 649 - end 666 + defp a +++ b, do: MapSet.union(a, b) 650 667 651 - def a <|> b do 652 - (a +++ b) --- (a <~> b) 653 - end 668 + defp a <~> b, do: MapSet.intersection(a, b) 654 669 655 670 # 1. 7. 4. 2|3|5. 2|3|5. 2|3|5. 6|9|0. 6|9|0. 6|9|0. 8. 656 - def deduce({cf, acf, bcdf, acdeg, acdfg, abdfg, abdefg, abcdfg, abcefg, abcdefg}) do 657 - a = acf --- cf 671 + def deduce([cf, acf, bcdf, acdeg, acdfg, abdfg, abdefg, abcdfg, abcefg, abcdefg]) do 658 672 eg = abcdefg --- (acf +++ bcdf) 659 673 bd = bcdf --- cf 660 - abfg = abdefg <|> abcdfg <|> abcefg 674 + adg = acdeg <~> acdfg <~> abdfg 675 + abfg = abdefg <~> abcdfg <~> abcefg 676 + a = acf --- cf 661 677 b = abfg <~> bd 662 678 f = abfg <~> cf 663 - g = abfg --- (a +++ b +++ f) 664 - d = bd --- b 679 + g = adg <~> eg 680 + d = adg <~> bd 665 681 c = cf --- f 666 682 e = eg --- g 667 683 668 - {a, b, c, d, e, f, g} = 684 + [a, b, c, d, e, f, g] = 669 685 [a, b, c, d, e, f, g] 670 686 |> Enum.map(&extract/1) 671 - |> List.to_tuple() 672 687 673 688 [ 674 689 # 0 ··· 698 713 end 699 714 700 715 defp extract(a) do 716 + # Just additional sanity check 701 717 [v] = MapSet.to_list(a) 702 718 703 719 v ··· 715 731 input 716 732 |> Enum.sort_by(&byte_size/1) 717 733 |> Enum.map(&MapSet.new(String.to_charlist(&1))) 718 - |> List.to_tuple() 719 734 |> Day8.Task2.deduce() 720 735 |> Day8.Task2.decode(output) 721 736 end) ··· 723 738 ``` 724 739 725 740 ```output 726 - warning: variable "abdfg" is unused (if the variable is not meant to be used, prefix it with an underscore) 727 - solutions.livemd#cell:19: Day8.Task2.deduce/1 741 + 1011785 742 + ``` 728 743 729 - warning: variable "acdeg" is unused (if the variable is not meant to be used, prefix it with an underscore) 730 - solutions.livemd#cell:19: Day8.Task2.deduce/1 744 + ## Day 9 731 745 732 - warning: variable "acdfg" is unused (if the variable is not meant to be used, prefix it with an underscore) 733 - solutions.livemd#cell:19: Day8.Task2.deduce/1 746 + ```elixir 747 + input = 748 + File.read!("day9.txt") 749 + # ~S[110 750 + # 101 751 + # 110] 752 + |> String.split("\n", trim: true) 753 + |> Enum.map(&String.to_charlist(String.trim(&1))) 754 + |> Nx.tensor(names: [:y, :x]) 755 + |> Nx.subtract(?0) 756 + |> Nx.add(1) 734 757 758 + {width, height} = shape = Nx.shape(input) 735 759 ``` 736 760 737 761 ```output 738 - 1011785 762 + {100, 100} 763 + ``` 764 + 765 + ### Task 1 766 + 767 + ```elixir 768 + padded = Nx.pad(input, 99, [{0, 0, 0}, {1, 1, 0}]) 769 + 770 + shifted = Nx.slice_axis(padded, 0, height, :x) 771 + 772 + x1 = Nx.less(input, shifted) 773 + 774 + shifted = Nx.slice_axis(padded, 2, height, :x) 775 + 776 + x2 = Nx.less(input, shifted) 777 + 778 + x = Nx.logical_and(x1, x2) 779 + 780 + padded = Nx.pad(input, 99, [{1, 1, 0}, {0, 0, 0}]) 781 + 782 + shifted = Nx.slice_axis(padded, 0, width, :y) 783 + 784 + y1 = Nx.less(input, shifted) 785 + 786 + shifted = Nx.slice_axis(padded, 2, width, :y) 787 + 788 + y2 = Nx.less(input, shifted) 789 + 790 + y = Nx.logical_and(y1, y2) 791 + 792 + minimas = Nx.logical_and(x, y) 793 + 794 + input 795 + |> Nx.multiply(minimas) 796 + |> Nx.sum() 797 + |> Nx.to_number() 798 + ``` 799 + 800 + ```output 801 + 452 802 + ``` 803 + 804 + ### Task 2 805 + 806 + ```elixir 807 + input 808 + |> Nx.equal(10) 809 + |> Nx.logical_not() 810 + |> Nx.select(Nx.iota(shape), 9999) 811 + |> Nx.to_flat_list() 812 + |> Enum.reject(&(&1 == 9999)) 813 + |> Enum.map(fn point -> {div(point, width), rem(point, width)} end) 814 + |> Enum.reduce([], fn {y, x} = point, basins -> 815 + basin_left = Enum.find_index(basins, &({y, x - 1} in &1)) 816 + basin_up = Enum.find_index(basins, &({y - 1, x} in &1)) 817 + 818 + case {basin_left, basin_up} do 819 + {nil, nil} -> 820 + [MapSet.new([point]) | basins] 821 + 822 + {idx, nil} -> 823 + List.update_at(basins, idx, &MapSet.put(&1, point)) 824 + 825 + {nil, idx} -> 826 + List.update_at(basins, idx, &MapSet.put(&1, point)) 827 + 828 + {idx, idx} -> 829 + List.update_at(basins, idx, &MapSet.put(&1, point)) 830 + 831 + {idx1, idx2} -> 832 + {old, basins} = List.pop_at(basins, max(idx1, idx2)) 833 + 834 + List.update_at(basins, min(idx1, idx2), &(&1 |> MapSet.union(old) |> MapSet.put(point))) 835 + end 836 + end) 837 + |> Enum.map(&MapSet.size/1) 838 + |> Enum.sort(:desc) 839 + |> Enum.take(3) 840 + |> Enum.reduce(&*/2) 841 + ``` 842 + 843 + ```output 844 + 1263735 739 845 ```