From facf77e829612d04dfcf28d68d79acca144cffe4 Mon Sep 17 00:00:00 2001 From: Adam Millerchip Date: Mon, 14 Dec 2020 20:57:38 +0900 Subject: [PATCH] Day 14 Part 1 --- day14/README | 25 ++ day14/day14part1.exs | 50 ++++ day14/input | 562 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 637 insertions(+) create mode 100644 day14/README create mode 100644 day14/day14part1.exs create mode 100644 day14/input diff --git a/day14/README b/day14/README new file mode 100644 index 0000000..d6d5aa9 --- /dev/null +++ b/day14/README @@ -0,0 +1,25 @@ +Day 14 Notes + ++--------+ +| Part 1 | ++--------+ + +$ elixir day14part1.exs + +Thoughts: + + ++--------+ +| Part 2 | ++--------+ + +$ elixir day14part2.exs + +Thoughts: + + ++------------------+ +| Overall Thoughts | ++------------------+ + + diff --git a/day14/day14part1.exs b/day14/day14part1.exs new file mode 100644 index 0000000..57dffb0 --- /dev/null +++ b/day14/day14part1.exs @@ -0,0 +1,50 @@ +defmodule Day14Part1 do + use Bitwise + + def run do + File.read!("input") + |> String.split("\n", trim: true) + |> Enum.map(&parse_command/1) + |> Enum.reduce({nil, %{}}, &execute/2) + |> sum_memory() + |> IO.inspect() + end + + def execute({:mask, mask}, {_mask, mem}), do: {mask, mem} + + def execute({:mem, addr, value}, {mask, mem}) do + masked_value = apply_mask(value, mask) + {mask, Map.put(mem, addr, masked_value)} + end + + def sum_memory({_mask, mem}), do: Map.values(mem) |> Enum.sum() + + def apply_mask(value, {andmask, ormask}), do: (value &&& andmask) ||| ormask + + def parse_command("mask = " <> mask), do: {:mask, decode_mask(mask)} + + def parse_command(mem_command) do + [addr, value] = + mem_command + |> String.split(["mem[", "] = "], trim: true) + |> Enum.map(&String.to_integer/1) + + {:mem, addr, value} + end + + def decode_mask(mask) when byte_size(mask) == 36 do + <> = for <>, into: <<>>, do: andmask_bit(bit) + <> = for <>, into: <<>>, do: ormask_bit(bit) + {andmask, ormask} + end + + def andmask_bit("X"), do: <<1::1>> + def andmask_bit("1"), do: <<1::1>> + def andmask_bit("0"), do: <<0::1>> + + def ormask_bit("X"), do: <<0::1>> + def ormask_bit("1"), do: <<1::1>> + def ormask_bit("0"), do: <<0::1>> +end + +Day14Part1.run() diff --git a/day14/input b/day14/input new file mode 100644 index 0000000..060f030 --- /dev/null +++ b/day14/input @@ -0,0 +1,562 @@ +mask = 11X01X101X10000110110101X100000011XX +mem[30904] = 804 +mem[25640] = 58672415 +mem[44254] = 829902099 +mask = 0100X100101000X0X01100X011100X000011 +mem[16446] = 3614672 +mem[60345] = 12470188 +mem[56197] = 3362 +mem[16887] = 516 +mem[15481] = 98479236 +mem[35964] = 5253451 +mem[6365] = 7426 +mask = 110X0X00100110101011110X1101100X1000 +mem[8832] = 13005766 +mem[55115] = 950 +mem[41989] = 552 +mem[61913] = 823 +mem[20675] = 22741 +mem[63918] = 733663 +mem[27098] = 12221134 +mask = 1101000X0011100000101XX0000X1100XX00 +mem[6537] = 8038445 +mem[1980] = 10059427 +mem[14520] = 4008996 +mask = 00X11X000000X000X1100011000110001000 +mem[34817] = 444921 +mem[2819] = 5673 +mem[22931] = 1528224 +mem[3026] = 5672 +mask = 1X0000001111X0X1111X1000100X0X0X10X0 +mem[35040] = 132191053 +mem[10721] = 754223 +mem[56671] = 2754806 +mem[30791] = 10367 +mem[19792] = 147 +mem[10716] = 44773 +mem[33657] = 364 +mask = 0X00000XX00X0X01101111000111010X11XX +mem[54264] = 252 +mem[20924] = 4662 +mem[30011] = 13994357 +mem[32315] = 3582763 +mem[49802] = 55546 +mem[60634] = 12285193 +mem[35945] = 423 +mask = 010000X010000001100X100X1X0001110110 +mem[54639] = 1114347 +mem[14221] = 648 +mem[48913] = 130830803 +mask = 0111100011011X0110000X1X01X101100000 +mem[54240] = 49462136 +mem[30740] = 2366 +mem[65284] = 4006 +mem[6233] = 507389094 +mem[19877] = 141 +mask = 110110X0X001010X1100010XX01101111X10 +mem[42920] = 210585 +mem[1758] = 291573 +mem[11740] = 1753119 +mem[62804] = 1121 +mem[54639] = 872 +mem[35142] = 1916057 +mask = 100X10000001100101000011XX10101000X0 +mem[40548] = 560555 +mem[62482] = 7506653 +mem[49966] = 575 +mem[30575] = 1220439 +mem[28113] = 5563 +mem[41047] = 65293771 +mask = X1010000X1XX01X110100X01101X01010XX0 +mem[1366] = 71278590 +mem[45316] = 873708 +mem[43138] = 2041 +mem[46594] = 607 +mem[15467] = 127558 +mask = X100100000100000101X00011X11X0110001 +mem[549] = 1814393 +mem[59563] = 865 +mem[34570] = 175093 +mem[21815] = 841005500 +mem[46823] = 517 +mem[62134] = 2068575 +mask = XX0000X110000101101X00X1X111X111X110 +mem[9644] = 81473 +mem[55094] = 103187 +mask = 010X00001XXX0X01101XX001101111X1X010 +mem[43476] = 39027003 +mem[25072] = 225 +mem[37182] = 1497390 +mem[22440] = 170498 +mask = 0XXXX0001000001110X1101X111X10101000 +mem[16871] = 92184017 +mem[22567] = 9298973 +mem[38861] = 3583194 +mem[46161] = 3411721 +mem[55420] = 382 +mem[25462] = 174027 +mem[41626] = 275108 +mask = 1X01000X11X0X11110100X11110X0101X001 +mem[21342] = 14984152 +mem[52268] = 435036 +mem[23071] = 13378 +mem[58137] = 63436471 +mem[23072] = 4203436 +mem[38405] = 479843353 +mask = 0000X001100X0001X011X10X010101101011 +mem[52773] = 64752275 +mem[3789] = 898152 +mem[12600] = 7124813 +mask = 100110XX000110X001101X10100XX000000X +mem[58350] = 693 +mem[2651] = 163278 +mem[24130] = 13797006 +mem[62784] = 23369612 +mem[6912] = 1301 +mask = 11010X00100XX101101111111111X101101X +mem[53888] = 194671 +mem[3670] = 8817412 +mem[58068] = 16156 +mem[64215] = 11891 +mask = 01X1000XX101XX0110100001X1000X010X0X +mem[59576] = 237039 +mem[30009] = 957 +mem[60486] = 961 +mem[38241] = 18092946 +mem[32300] = 81527 +mem[43235] = 361326555 +mask = X01110100001110X0000X0X11X0X110X00X1 +mem[34347] = 63169660 +mem[23916] = 2842 +mem[14819] = 2078726 +mem[38982] = 54808 +mem[36227] = 494 +mem[17114] = 7834 +mask = 0XX10XX0000011011111X01111100010XX00 +mem[21874] = 304925 +mem[56696] = 950088 +mask = 1100X0X110111011XX111101X10X110X1111 +mem[59256] = 178304 +mem[10638] = 155668 +mem[6818] = 58192 +mask = 01X1000011110X11X010101110101X000100 +mem[23723] = 7575 +mem[62088] = 17274981 +mem[6864] = 21516071 +mask = 11X10000X101X00X10110X01111110X00111 +mem[31049] = 491767185 +mem[27270] = 7118669 +mem[4580] = 65758 +mem[1039] = 14254 +mem[32658] = 9716 +mem[5162] = 842785 +mem[24455] = 5673467 +mask = 110X0X0X110X1000XX1X0000001010000X11 +mem[46755] = 6502241 +mem[7424] = 20934554 +mem[23337] = 678828183 +mem[64828] = 805263 +mask = X10X00001111X0101011010110X010000100 +mem[5481] = 288565 +mem[10519] = 1209 +mem[5509] = 4081599 +mask = 10X000001XX01101X111XX00101111000111 +mem[31289] = 23840740 +mem[60675] = 2716 +mem[549] = 32627037 +mem[56788] = 47252 +mem[42610] = 1204108 +mask = 00XXXX00100000X110010010101110100111 +mem[38288] = 675950673 +mem[4580] = 3440 +mem[29394] = 633235 +mem[28343] = 8737 +mask = 1101100000001X0010X10110X0011X111101 +mem[56644] = 40733165 +mem[15071] = 32842495 +mem[42317] = 31612899 +mem[11060] = 31791 +mem[32740] = 144488 +mem[59117] = 84496 +mem[48853] = 2789 +mask = 1001X0000X1X10000011010000011XX01001 +mem[41834] = 13008 +mem[44254] = 506032399 +mem[47240] = 104934982 +mem[59603] = 908964 +mem[27127] = 7816195 +mem[9442] = 6967530 +mem[6049] = 130571712 +mask = 11010000101110X0X11X01100XXX11X01001 +mem[55552] = 538 +mem[45294] = 5968 +mem[57770] = 5074932 +mem[14709] = 620 +mask = 1X000X0011001X01X1111XX1111100010010 +mem[22567] = 111589993 +mem[27442] = 7345501 +mem[1366] = 693 +mem[10716] = 67389 +mem[34007] = 7669704 +mem[42994] = 424182225 +mask = 00X1101000011X010X0X1001100000110100 +mem[17366] = 1744816 +mem[49802] = 26865386 +mem[33107] = 315222523 +mem[22972] = 1000 +mem[40792] = 296402778 +mem[15571] = 119687927 +mask = 0111X0X0X10111X110X00010XX0101X0010X +mem[28584] = 20338342 +mem[60490] = 7353702 +mem[47627] = 238170 +mem[65284] = 98512808 +mem[21880] = 209876 +mem[45316] = 17898 +mem[23937] = 67926 +mask = 110110000011X000X0XX0000X110000XX000 +mem[6049] = 603777 +mem[35598] = 8391793 +mem[36909] = 87535271 +mem[39369] = 15004 +mask = 01010X10110010011111X110100000010X0X +mem[7560] = 1428145 +mem[60796] = 150097 +mem[7572] = 402 +mem[26354] = 156030 +mem[38982] = 792942 +mem[43594] = 29726 +mem[62747] = 70726731 +mask = 1X01X00000X1X000X01X1X0110X010000001 +mem[21629] = 9076643 +mem[52203] = 6306 +mem[22984] = 248989 +mem[24904] = 5381 +mask = X1X10XX00000011X11010X0101XX1X111001 +mem[46483] = 15635 +mem[59552] = 13183491 +mem[41572] = 5477 +mem[44254] = 20911415 +mem[13067] = 3094 +mem[6827] = 2156 +mem[13117] = 9019 +mask = 1101000X0X0X1001X1110011100000010101 +mem[7097] = 11433841 +mem[64940] = 1502724 +mem[63191] = 27213771 +mask = X1010X000000XXX11X1X111111X10011100X +mem[65480] = 13438723 +mem[33632] = 81817290 +mem[7966] = 28099266 +mem[52682] = 35609347 +mem[14221] = 73182911 +mask = 0101X000X110X001101000011X111100111X +mem[41444] = 7685 +mem[2096] = 94796 +mem[18256] = 71874 +mask = 100X00000011XX000X1110011001X1101010 +mem[3789] = 14774133 +mem[62406] = 226 +mem[4451] = 136465 +mask = 010100000X0001XX1XX1X01101011X0010X0 +mem[3660] = 3970041 +mem[52464] = 1608598 +mem[9853] = 221778 +mem[50180] = 200623 +mask = 0101X000X000X00110X111X111X110111011 +mem[36227] = 6953473 +mem[3660] = 207401 +mem[549] = 66817 +mem[59807] = 3577 +mem[13761] = 4685324 +mem[21629] = 53428878 +mem[46431] = 1686 +mask = 01001000XX1X011110111101100X101XX01X +mem[38241] = 239351742 +mem[30851] = 116 +mem[57771] = 10156 +mem[52691] = 5553451 +mask = 1X01000011X1X0011011001111X100100110 +mem[1980] = 934 +mem[28565] = 22842096 +mem[65272] = 1283 +mem[3239] = 843939 +mem[17041] = 507297277 +mask = 01001000011101X010111011X001X00X1100 +mem[44712] = 2005 +mem[47569] = 627819534 +mem[22757] = 4903910 +mask = 1X01000000001101101101100X1X0101100X +mem[40808] = 134469 +mem[38359] = 5460 +mem[12034] = 124445441 +mask = 00011000000X0000011010X10XX01000X001 +mem[27442] = 85520780 +mem[39169] = 2706511 +mem[63093] = 65082 +mem[38333] = 21146 +mem[22757] = 9565 +mem[55395] = 575 +mem[30420] = 6655 +mask = 1101000X00X1X00X0011000101X001011XX1 +mem[13633] = 2443 +mem[40637] = 14161841 +mask = 0100X00001100000101XX10101011X001101 +mem[14221] = 763800 +mem[36636] = 74643 +mem[60609] = 942 +mask = 0XX10000100X00XX10011X10101010110X10 +mem[26500] = 14129085 +mem[20357] = 419 +mem[7587] = 53492 +mem[60812] = 914199 +mask = 1101X000X0XX1XXX101111111011110010X0 +mem[21629] = 96321089 +mem[31049] = 5542520 +mem[28565] = 2711108 +mask = 100110X0000110000XX01000100X0X00XXXX +mem[14509] = 3728576 +mem[34266] = 564529794 +mem[39304] = 476496 +mem[4938] = 41380863 +mask = XX010000010X01X110101X101100X1010010 +mem[24130] = 1934 +mem[58734] = 10309 +mem[40221] = 1598 +mem[15977] = 184066469 +mem[21816] = 11450486 +mask = 0X00X100X0001001101X00X110100X101X10 +mem[34141] = 57556 +mem[30791] = 1291662 +mem[58838] = 7014 +mem[24341] = 3067 +mask = X10000X0100001011XX100X1001011110010 +mem[4394] = 987 +mem[6958] = 251375845 +mem[30011] = 3732389 +mask = 11XX000010011010111X0100XXX011010110 +mem[52203] = 68721 +mem[45364] = 59466717 +mem[31045] = 21598944 +mem[50395] = 4231 +mem[51592] = 8111 +mem[63504] = 8329134 +mem[58734] = 754 +mask = 110100001XX110XX1X1101110XX11X0X0001 +mem[16279] = 811 +mem[10716] = 55019036 +mem[51529] = 24243 +mem[14709] = 3794 +mask = 1X010100XX00110111111X0X101X11101X1X +mem[46385] = 264566 +mem[15571] = 3070 +mem[36393] = 614536 +mem[47391] = 115102 +mem[16612] = 1449 +mem[39001] = 7341623 +mask = X10X1000X01000001011XX0010X000110001 +mem[31360] = 245394 +mem[10346] = 2844 +mem[59256] = 26640464 +mem[40731] = 9549 +mask = 110X0000XX0XX001111111X11101X00X1000 +mem[20489] = 3679 +mem[36850] = 48368 +mem[43476] = 9536 +mem[42317] = 794736 +mem[64360] = 3041633 +mem[20374] = 182769351 +mask = 11XX000X1X111X1X1X11011110XX11001101 +mem[34207] = 918112464 +mem[30124] = 22887363 +mem[23337] = 5194 +mem[64654] = 2482244 +mem[13633] = 52721017 +mem[53541] = 611 +mask = X0X1101000011X0XX00X100X0X00X1010011 +mem[35029] = 31476 +mem[29797] = 8305 +mem[11060] = 1711 +mem[3509] = 370841 +mask = 1100000X11010X0X111X1X1X0000XX001010 +mem[12166] = 2826105 +mem[13096] = 3515 +mem[10844] = 31293 +mem[47528] = 4666248 +mem[30022] = 2602 +mask = 1101X00100011001X01X1001X0X010001010 +mem[7409] = 228726 +mem[44042] = 5963265 +mem[7966] = 6413 +mem[32315] = 2668 +mask = 110X000X00X1X000X0111001100111000001 +mem[21629] = 372179 +mem[12594] = 3250987 +mem[51528] = 11238 +mem[32964] = 432 +mask = 11X10000001X000X0010X0111X0110001001 +mem[22646] = 1609877 +mem[2120] = 252 +mem[12600] = 90688426 +mem[26126] = 5793298 +mem[52659] = 17234 +mem[59801] = 14287590 +mem[7917] = 16520660 +mask = 11X00000110X10011X110X0111011101X001 +mem[38536] = 60532 +mem[38879] = 61201 +mem[9818] = 9333026 +mem[48276] = 10329920 +mask = 110X1000X01X00001011X0X10X0110001XX1 +mem[39369] = 5666 +mem[12594] = 361449 +mem[35634] = 1157722 +mem[11186] = 3299696 +mem[15380] = 5526081 +mem[4541] = 328 +mask = 010X0X00X0X0X00X10X1000010100011X111 +mem[13134] = 37334 +mem[60703] = 1171 +mem[6504] = 9516 +mem[46431] = 246333598 +mem[26155] = 21043 +mem[55195] = 125930 +mem[58790] = 11080295 +mask = 10011000X0011X0001001X0XX1XXX00110X1 +mem[344] = 499699 +mem[56449] = 20803473 +mem[60345] = 52387 +mem[46126] = 1697507 +mem[19090] = 52858555 +mem[20723] = 26263343 +mem[63191] = 3 +mask = X00110000001X00X01X01001101XX0000XX0 +mem[33281] = 489201 +mem[28482] = 1249916 +mem[44260] = 977965 +mem[45827] = 90243487 +mask = X10100000111X1X11010XX11X001X1X10010 +mem[39556] = 151117 +mem[42947] = 34920 +mem[59025] = 2013682 +mem[57008] = 4913 +mem[6773] = 406 +mask = X10X00001X010XX1111010XX100X00101010 +mem[57004] = 33597 +mem[24038] = 57028199 +mem[8014] = 10576959 +mem[30740] = 118961 +mem[32106] = 2411 +mem[43274] = 3984 +mem[3179] = 716 +mask = 1101000X00001X011X11X01011XX1011110X +mem[20572] = 8223195 +mem[3670] = 309 +mem[57224] = 3646 +mask = 0101000X00X00X01111X1111110X00100X10 +mem[27896] = 2498 +mem[10519] = 629600 +mem[35133] = 15030807 +mask = 0X01X0X00X0X01111010110X10110X010100 +mem[62482] = 20472364 +mem[64016] = 8001446 +mem[61547] = 12063 +mask = 11010XX011100X0110X00X1110010111X0X0 +mem[59444] = 2341 +mem[10049] = 53787074 +mem[50395] = 3290 +mask = 100110100X01100X001X100110001000010X +mem[4227] = 24426052 +mem[16502] = 23444792 +mem[22294] = 3709904 +mem[10773] = 12722 +mask = X10010000X1X0X0XX01110110X011100XX01 +mem[7257] = 1613 +mem[7489] = 1856 +mem[24160] = 61998242 +mem[56644] = 8079639 +mem[59111] = 151253 +mask = 11010000000100011XX101111X0100001X01 +mem[52224] = 252594 +mem[10569] = 79481269 +mem[12724] = 627 +mask = 11X0000011X1100111111X0X1XX10110X000 +mem[48476] = 44915498 +mem[28502] = 987462 +mem[28907] = 17654 +mem[30984] = 527202957 +mask = 110X000000X0110110101110XX1101111001 +mem[14737] = 57864548 +mem[63545] = 62156 +mask = 1XX1100000010X00X100XX01X11101100110 +mem[23890] = 27294 +mem[50906] = 46891220 +mem[34574] = 597 +mem[20622] = 2981498 +mem[10272] = 303 +mask = 01X1000XX00010X1101101X1X01110XX1X11 +mem[18342] = 206778755 +mem[10346] = 412 +mem[19158] = 20387791 +mask = 010X00001X100111101X1X1X1X11X10101X0 +mem[1901] = 158999 +mem[41794] = 54125676 +mem[22931] = 89400316 +mem[19522] = 833 +mask = 110XX000001X00001011X001111100010100 +mem[21629] = 1212 +mem[5305] = 9321884 +mem[23237] = 31772642 +mem[15481] = 30790 +mask = 01001000011X01X1XXX10001XXX11X101011 +mem[50281] = 1031850 +mem[55170] = 152566 +mem[62088] = 2825 +mem[55498] = 39448855 +mask = 010X0000XX0XX0011011000100X110111XXX +mem[55115] = 91 +mem[27127] = 204950 +mem[29412] = 99341996 +mem[30840] = 32617407 +mem[52681] = 17904254 +mem[28755] = 910986 +mask = 010000X0111X0X01X01000011X101X000X10 +mem[61913] = 86487 +mem[23890] = 1409414 +mem[18153] = 122261 +mask = 01X1000010001011X01100X1XX1110101010 +mem[32043] = 1038190 +mem[35909] = 154 +mask = 0111X00XXXX0101110XX010000X11X100001 +mem[37916] = 4881390 +mem[13226] = 443002 +mem[22646] = 64137936 +mem[31097] = 94510556 +mem[3570] = 119428 +mem[3311] = 292624 +mask = X10X1000000XX000X011011110X1000XX101 +mem[25147] = 646727 +mem[7075] = 143058300 +mem[50395] = 661 +mem[12058] = 11478790 +mask = X0011000X0111000X010110100X1X10010X0 +mem[38241] = 752832217 +mem[33284] = 1355491 +mem[32536] = 344526925 +mem[6345] = 989912 +mask = 01001X0001X1010100111111100X110X1101 +mem[16833] = 1778173 +mem[22135] = 3254 +mem[43235] = 84325 +mask = X10X00X01100100111111111110X1XX11XX0 +mem[26414] = 3191 +mem[62592] = 1205580 +mem[24576] = 82621 +mem[65177] = 4041591 +mem[20140] = 2396 +mask = 11X010X0X0100X011X11XX01110000001101 +mem[23934] = 79377022 +mem[22183] = 77353599 +mem[4451] = 229619 +mem[9249] = 2021235 +mem[47560] = 472016181