From 66708a79bdaa1ad9bcd7fec6a73f834aa1569031 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Wed, 23 Dec 2020 21:05:42 +0100 Subject: [PATCH] Day 14, Bash & C --- OUTPUT | 234 +++++++++++--------- day14/INPUT.txt | 577 ++++++++++++++++++++++++++++++++++++++++++++++++ day14/Makefile | 30 +++ day14/OUTPUT | 16 ++ day14/README | 106 +++++++++ day14/TEST.txt | 4 + day14/TEST2.txt | 4 + day14/TEST3.txt | 12 + day14/ex1-c.c | 130 +++++++++++ day14/ex1.bash | 31 +++ day14/ex2-c.c | 163 ++++++++++++++ day14/ex2.bash | 61 +++++ 12 files changed, 1261 insertions(+), 107 deletions(-) create mode 100644 day14/INPUT.txt create mode 100644 day14/Makefile create mode 100644 day14/OUTPUT create mode 100644 day14/README create mode 100644 day14/TEST.txt create mode 100644 day14/TEST2.txt create mode 100644 day14/TEST3.txt create mode 100644 day14/ex1-c.c create mode 100755 day14/ex1.bash create mode 100644 day14/ex2-c.c create mode 100755 day14/ex2.bash diff --git a/OUTPUT b/OUTPUT index 7df925f..db945bf 100644 --- a/OUTPUT +++ b/OUTPUT @@ -4,42 +4,41 @@ +++++++++++++++++ ex1 ex1.bash : 33:1701 180:319 sum=2020 mul=542619 - time: 0:00.06 real, 0.06 user, 0.00 sys - context-switch: 8+4, page-faults: 0+308 + time: 0:00.12 real, 0.06 user, 0.00 sys + context-switch: 7+9, page-faults: 1+303 ex1-cob : res= 542619 - time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 1+1, page-faults: 0+172 + time: 0:00.08 real, 0.00 user, 0.00 sys + context-switch: 1+12, page-faults: 5+172 ex1-c : 33:1701 180:319 sum=2020 mul=542619 - time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+74 + time: 0:00.02 real, 0.00 user, 0.00 sys + context-switch: 0+2, page-faults: 0+74 +++++++++++++++++ ex2 -cobc -fmfcomment -free -x ex2-pure-sort-cob.cob ex2.bash : 80:1450 94:43 185:527 sum=2020 mul=32858450 - time: 0:10.26 real, 10.23 user, 0.00 sys - context-switch: 1360+4, page-faults: 0+304 + time: 0:10.01 real, 9.98 user, 0.00 sys + context-switch: 1060+5, page-faults: 0+303 ex2-sort.bash : 0:43 2:527 81:1450 sum=2020 mul=32858450 - time: 0:00.01 real, 0.01 user, 0.00 sys - context-switch: 2+7, page-faults: 0+477 + time: 0:00.04 real, 0.01 user, 0.00 sys + context-switch: 1+8, page-faults: 0+477 ex2-pure-sort.bash : 0:43 2:527 81:1450 sum=2020 mul=32858450 - time: 0:00.55 real, 0.54 user, 0.00 sys - context-switch: 126+4, page-faults: 0+309 + time: 0:00.50 real, 0.50 user, 0.00 sys + context-switch: 47+5, page-faults: 0+306 ex2-sort-cob : res= 32858450 - time: 0:00.01 real, 0.01 user, 0.00 sys - context-switch: 1+1, page-faults: 0+173 + time: 0:00.03 real, 0.01 user, 0.00 sys + context-switch: 1+3, page-faults: 1+174 ex2-pure-sort-cob : res= 32858450 - time: 0:00.01 real, 0.01 user, 0.00 sys - context-switch: 1+1, page-faults: 0+172 + time: 0:00.02 real, 0.02 user, 0.00 sys + context-switch: 2+3, page-faults: 1+173 ex2-c : 80:1450 94:43 185:527 sum=2020 mul=32858450 time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+73 + context-switch: 1+2, page-faults: 0+74 ========================================= ================= day02 ================= @@ -47,21 +46,21 @@ ex2-c : 80:1450 94:43 185:527 sum=2020 mul=32858450 +++++++++++++++++ ex1 ex1.bash : lines: 1000 matched:607 - time: 0:00.02 real, 0.02 user, 0.00 sys - context-switch: 2+1, page-faults: 0+153 + time: 0:00.06 real, 0.03 user, 0.00 sys + context-switch: 3+3, page-faults: 0+152 ex1-c : lines: 1000 matched:607 - time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+75 + time: 0:00.02 real, 0.00 user, 0.00 sys + context-switch: 0+2, page-faults: 0+73 +++++++++++++++++ ex2 ex2.bash : lines: 1000 matched:321 - time: 0:00.02 real, 0.01 user, 0.01 sys - context-switch: 5+1, page-faults: 0+153 + time: 0:00.03 real, 0.02 user, 0.00 sys + context-switch: 11+2, page-faults: 0+155 ex2-c : lines: 1000 matched:321 time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 1+0, page-faults: 0+73 + context-switch: 0+2, page-faults: 0+74 ========================================= ================= day03 ================= @@ -69,21 +68,21 @@ ex2-c : lines: 1000 matched:321 +++++++++++++++++ ex1 ex1.bash : lines:322 pos=966 found:169 - time: 0:00.01 real, 0.01 user, 0.00 sys - context-switch: 2+1, page-faults: 0+154 + time: 0:00.03 real, 0.01 user, 0.00 sys + context-switch: 1+3, page-faults: 0+152 ex1-c : lines:322 pos:966 found:169 time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+73 + context-switch: 0+2, page-faults: 0+72 +++++++++++++++++ ex2 ex2.bash : lines=323 res=6847128288 - time: 0:00.05 real, 0.05 user, 0.00 sys - context-switch: 5+1, page-faults: 0+154 + time: 0:00.06 real, 0.04 user, 0.00 sys + context-switch: 4+2, page-faults: 0+157 ex2-c : lines=323 res=6847128288 time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+72 + context-switch: 0+2, page-faults: 0+73 ========================================= ================= day04 ================= @@ -91,21 +90,21 @@ ex2-c : lines=323 res=6847128288 +++++++++++++++++ ex1 ex1.bash : valid=182/251 - time: 0:00.07 real, 0.06 user, 0.00 sys - context-switch: 9+1, page-faults: 0+159 + time: 0:00.10 real, 0.06 user, 0.00 sys + context-switch: 7+3, page-faults: 0+163 ex1-c : valid=182/251 time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 1+1, page-faults: 0+74 + context-switch: 0+2, page-faults: 0+73 +++++++++++++++++ ex2 ex2.bash : valid=109/251 - time: 0:00.17 real, 0.16 user, 0.00 sys - context-switch: 16+1, page-faults: 0+195 + time: 0:00.18 real, 0.17 user, 0.00 sys + context-switch: 19+2, page-faults: 0+192 ex2-c : valid=109/251 time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 1+1, page-faults: 0+73 + context-switch: 2+2, page-faults: 0+73 ========================================= ================= day05 ================= @@ -113,21 +112,21 @@ ex2-c : valid=109/251 +++++++++++++++++ ex1 ex1.bash : lines=743 max=838 - time: 0:00.99 real, 0.64 user, 0.45 sys - context-switch: 77+4438, page-faults: 0+149316 + time: 0:01.02 real, 0.65 user, 0.43 sys + context-switch: 91+4426, page-faults: 0+150059 ex1-c : lines=743 max=838 - time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+71 + time: 0:00.05 real, 0.00 user, 0.00 sys + context-switch: 1+2, page-faults: 0+73 +++++++++++++++++ ex2 ex2.bash : lines=743 seat=714 - time: 0:01.00 real, 0.70 user, 0.39 sys - context-switch: 66+4437, page-faults: 0+155941 + time: 0:01.12 real, 0.75 user, 0.45 sys + context-switch: 81+4425, page-faults: 0+155940 ex2-c : lines=743 seat=714 - time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+72 + time: 0:00.03 real, 0.00 user, 0.00 sys + context-switch: 0+2, page-faults: 0+73 ========================================= ================= day06 ================= @@ -135,29 +134,29 @@ ex2-c : lines=743 seat=714 +++++++++++++++++ ex1 ex1.bash : groups=484 count=6585 - time: 0:01.38 real, 1.20 user, 0.21 sys - context-switch: 138+1376, page-faults: 0+58975 + time: 0:01.33 real, 1.22 user, 0.15 sys + context-switch: 106+1388, page-faults: 0+58002 ex1-bis.bash : groups=484 count=6585 - time: 0:01.20 real, 1.03 user, 0.19 sys - context-switch: 110+1339, page-faults: 0+60090 + time: 0:01.23 real, 1.08 user, 0.17 sys + context-switch: 84+1309, page-faults: 0+59601 ex1-c : groups=484 count=6585 - time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+71 + time: 0:00.01 real, 0.00 user, 0.00 sys + context-switch: 0+2, page-faults: 0+71 +++++++++++++++++ ex2 ex2.bash : groups=484 count=3276 - time: 0:01.43 real, 1.27 user, 0.20 sys - context-switch: 117+1381, page-faults: 0+60568 + time: 0:01.33 real, 1.20 user, 0.15 sys + context-switch: 91+1391, page-faults: 0+60081 ex2-bis.bash : groups=484 count=3276 - time: 0:01.29 real, 1.16 user, 0.15 sys - context-switch: 159+1370, page-faults: 0+59366 + time: 0:01.28 real, 1.09 user, 0.19 sys + context-switch: 114+1370, page-faults: 0+58880 ex2-c : groups=484 count=3276 time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+72 + context-switch: 1+2, page-faults: 0+72 ========================================= ================= day07 ================= @@ -165,25 +164,25 @@ ex2-c : groups=484 count=3276 +++++++++++++++++ ex1 ex1.bash : target=shinygold nkeys=594 res=287 - time: 0:07.02 real, 7.01 user, 0.00 sys - context-switch: 715+1, page-faults: 0+408 + time: 0:07.04 real, 6.98 user, 0.01 sys + context-switch: 969+3, page-faults: 0+406 ex1-bis.bash : target=shinygold res=287 - time: 0:00.13 real, 0.13 user, 0.00 sys - context-switch: 13+1, page-faults: 0+208 + time: 0:00.13 real, 0.12 user, 0.00 sys + context-switch: 36+2, page-faults: 0+208 ex1-c : target=shinygold nkeys=594 res=287 time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+117 + context-switch: 0+2, page-faults: 0+118 +++++++++++++++++ ex2 ex2.bash : target=shinygold res=48160 - time: 0:00.34 real, 0.29 user, 0.05 sys - context-switch: 38+253, page-faults: 0+12134 + time: 0:00.36 real, 0.28 user, 0.06 sys + context-switch: 37+253, page-faults: 0+11816 ex2-c : target=shinygold nkeys=594 res=48160 time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+118 + context-switch: 0+2, page-faults: 0+117 ========================================= ================= day08 ================= @@ -191,21 +190,21 @@ ex2-c : target=shinygold nkeys=594 res=48160 +++++++++++++++++ ex1 ex1.bash : res=1594 - time: 0:00.02 real, 0.01 user, 0.01 sys - context-switch: 3+4, page-faults: 0+300 + time: 0:00.06 real, 0.03 user, 0.00 sys + context-switch: 3+6, page-faults: 0+303 ex1-c : res=1594 time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+80 + context-switch: 0+2, page-faults: 0+80 +++++++++++++++++ ex2 ex2.bash : res:758 - time: 0:02.44 real, 2.24 user, 0.23 sys - context-switch: 249+1093, page-faults: 0+43022 + time: 0:02.20 real, 1.95 user, 0.28 sys + context-switch: 270+1092, page-faults: 0+43751 ex2-c : res=758 - time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 1+1, page-faults: 0+78 + time: 0:00.02 real, 0.00 user, 0.00 sys + context-switch: 0+2, page-faults: 0+79 ========================================= ================= day09 ================= @@ -213,21 +212,21 @@ ex2-c : res=758 +++++++++++++++++ ex1 ex1.bash : res=167829540 - time: 0:00.41 real, 0.40 user, 0.00 sys - context-switch: 43+1, page-faults: 0+177 + time: 0:00.40 real, 0.37 user, 0.00 sys + context-switch: 40+3, page-faults: 0+177 ex1-c : res=167829540 - time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+74 + time: 0:00.02 real, 0.00 user, 0.00 sys + context-switch: 2+2, page-faults: 0+75 +++++++++++++++++ ex2 ex2.bash : res=167829540 sum=28045630 - time: 0:02.89 real, 2.67 user, 0.23 sys - context-switch: 322+931, page-faults: 0+49182 + time: 0:02.91 real, 2.67 user, 0.24 sys + context-switch: 235+936, page-faults: 0+48716 ex2-c : res=167829540 sum=28045630 time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 1+1, page-faults: 0+74 + context-switch: 1+2, page-faults: 0+74 ========================================= ================= day10 ================= @@ -235,21 +234,21 @@ ex2-c : res=167829540 sum=28045630 +++++++++++++++++ ex1 ex1.bash : diff1=71 diff2=27 res=1917 - time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 1+4, page-faults: 0+330 + time: 0:00.02 real, 0.00 user, 0.00 sys + context-switch: 4+6, page-faults: 0+328 ex1-c : diff1=71 diff2=27 res=1917 time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+74 + context-switch: 1+2, page-faults: 0+72 +++++++++++++++++ ex2 ex2.bash : size=99 res=113387824750592 - time: 0:00.01 real, 0.01 user, 0.00 sys - context-switch: 3+4, page-faults: 0+335 + time: 0:00.02 real, 0.01 user, 0.00 sys + context-switch: 4+5, page-faults: 0+331 ex2-c : size=99 res=113387824750592 time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+74 + context-switch: 0+2, page-faults: 0+72 ========================================= ================= day11 ================= @@ -257,21 +256,21 @@ ex2-c : size=99 res=113387824750592 +++++++++++++++++ ex1 ex1.bash : res=2386 - time: 0:40.97 real, 40.86 user, 0.05 sys - context-switch: 4671+11, page-faults: 0+1315 + time: 0:40.81 real, 40.25 user, 0.07 sys + context-switch: 6828+17, page-faults: 0+1317 ex1-c : res=2386 - time: 0:00.01 real, 0.01 user, 0.00 sys - context-switch: 2+1, page-faults: 0+89 + time: 0:00.03 real, 0.00 user, 0.00 sys + context-switch: 1+2, page-faults: 0+91 +++++++++++++++++ ex2 ex2.bash : res=2091 - time: 1:20.97 real, 80.52 user, 0.14 sys - context-switch: 12282+11, page-faults: 0+1716 + time: 1:19.37 real, 78.72 user, 0.08 sys + context-switch: 9445+14, page-faults: 0+1717 ex2-c : res=2091 - time: 0:00.02 real, 0.02 user, 0.00 sys - context-switch: 2+1, page-faults: 0+90 + time: 0:00.04 real, 0.02 user, 0.00 sys + context-switch: 2+2, page-faults: 0+91 ========================================= ================= day12 ================= @@ -279,21 +278,21 @@ ex2-c : res=2091 +++++++++++++++++ ex1 ex1.bash : res=1457 - time: 0:00.01 real, 0.01 user, 0.00 sys - context-switch: 1+1, page-faults: 0+154 + time: 0:00.03 real, 0.02 user, 0.00 sys + context-switch: 2+3, page-faults: 0+153 ex1-c : res=1457 - time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+74 + time: 0:00.02 real, 0.00 user, 0.00 sys + context-switch: 1+2, page-faults: 0+75 +++++++++++++++++ ex2 ex2.bash : res=106860 - time: 0:00.09 real, 0.08 user, 0.00 sys - context-switch: 10+1, page-faults: 0+154 + time: 0:00.11 real, 0.08 user, 0.00 sys + context-switch: 10+2, page-faults: 0+152 ex2-c : res=106860 - time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 1+1, page-faults: 0+73 + time: 0:00.02 real, 0.00 user, 0.00 sys + context-switch: 0+2, page-faults: 0+73 ========================================= ================= day13 ================= @@ -301,19 +300,40 @@ ex2-c : res=106860 +++++++++++++++++ ex1 ex1.bash : res=410 - time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 1+1, page-faults: 0+156 + time: 0:00.02 real, 0.00 user, 0.00 sys + context-switch: 1+3, page-faults: 0+155 ex1-c : res=410 time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+76 + context-switch: 1+2, page-faults: 0+76 +++++++++++++++++ ex2 ex2.bash : res=600691418730595 - time: 0:00.01 real, 0.00 user, 0.00 sys - context-switch: 1+1, page-faults: 0+156 + time: 0:00.01 real, 0.01 user, 0.00 sys + context-switch: 2+2, page-faults: 0+156 ex2-c : res=600691418730595 - time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+75 + time: 0:00.01 real, 0.00 user, 0.00 sys + context-switch: 1+2, page-faults: 0+76 +========================================= +================= day14 ================= +========================================= + ++++++++++++++++++ ex1 +ex1.bash : res=10035335144067 + time: 0:00.05 real, 0.05 user, 0.00 sys + context-switch: 5+1, page-faults: 0+202 + +ex1-c : res=10035335144067 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 2+1, page-faults: 0+79 + ++++++++++++++++++ ex2 +ex2.bash : res=3817372618036 + time: 0:06.83 real, 6.80 user, 0.00 sys + context-switch: 916+1, page-faults: 0+5951 + +ex2-c : res=3817372618036 + time: 0:00.03 real, 0.03 user, 0.00 sys + context-switch: 3+1, page-faults: 0+663 diff --git a/day14/INPUT.txt b/day14/INPUT.txt new file mode 100644 index 0000000..ed9db2e --- /dev/null +++ b/day14/INPUT.txt @@ -0,0 +1,577 @@ +mask = 0000011011111X1001100X0001X1001100X0 +mem[43805] = 6934 +mem[57564] = 3741 +mem[28260] = 67554 +mem[16151] = 813 +mem[3058] = 873646 +mem[51717] = 270314 +mask = 1001X1X011110XX111100001100111001110 +mem[16252] = 730 +mem[28701] = 63670997 +mem[28652] = 9219200 +mem[64761] = 488985928 +mask = 0X01X10X1XX1010111100100100111101001 +mem[2833] = 52082550 +mem[59636] = 6574572 +mem[26957] = 62766 +mask = 00X0XX101X111X1001011111000101000101 +mem[54455] = 839691 +mem[65381] = 2671752 +mem[8488] = 4031472 +mem[5618] = 549 +mem[41399] = 5232 +mem[62807] = 201160 +mem[63847] = 201082 +mask = X000X10011X1X100011X0001000010000110 +mem[47360] = 44151287 +mem[46338] = 79342985 +mem[6868] = 11520126 +mem[27037] = 20408 +mem[25014] = 758862851 +mask = 100001X01111110X0X110001X0101010010X +mem[21679] = 5106103 +mem[12306] = 690264 +mask = 0000011111X1XX1X0XX10000011000000110 +mem[19803] = 179348214 +mem[19095] = 125150056 +mem[34147] = 1407927 +mem[41272] = 5775647 +mask = X00X01001X0111X101110X0001X110000XXX +mem[53665] = 290143 +mem[20519] = 265 +mem[44017] = 834 +mem[41856] = 435 +mem[43805] = 2264115 +mask = 1X0X0111111X1X010110X10000011X1XX101 +mem[52128] = 1029842 +mem[27168] = 222084 +mask = 10011100111111X100110X0XX11111011100 +mem[62082] = 9135 +mem[14654] = 3686 +mem[25450] = 1593529 +mem[1497] = 5749950 +mem[30570] = 5450 +mem[2144] = 106125340 +mem[1099] = 9136 +mask = 1000110XX00X10110111X0X00011X0XX0000 +mem[14922] = 97221103 +mem[26301] = 11154 +mem[47381] = 94170015 +mem[31162] = 36266349 +mem[24087] = 2020 +mask = 1100111111X0XX0X00110000110001010000 +mem[45973] = 3120 +mem[38175] = 307 +mem[58216] = 332810821 +mem[14799] = 3922 +mem[29584] = 14393700 +mem[51240] = 61932467 +mask = 00010X1X10011110X110X101X00001010X11 +mem[58448] = 26283204 +mem[16738] = 466633 +mem[6096] = 369 +mem[6987] = 4902006 +mask = 110011XX101111110111X000001XX01101XX +mem[2710] = 34047107 +mem[63146] = 116986332 +mask = 0001110011110101111X1X001X011X0101X1 +mem[6096] = 5336 +mem[9192] = 44289 +mem[26857] = 13 +mem[54135] = 1490304 +mem[17220] = 3623 +mem[1210] = 50969 +mask = 00011X0011X111011X00000100X1X100X111 +mem[31556] = 293748 +mem[21644] = 60471 +mem[31567] = 972370 +mem[32315] = 104177 +mem[6429] = 10486 +mem[25892] = 62384267 +mask = 000001001111010110X01000XX01100110X0 +mem[51519] = 214 +mem[55256] = 1197991 +mem[65279] = 178690564 +mem[3173] = 1554206 +mem[10263] = 1704 +mem[55736] = 217069 +mask = X0011100X111X1011XX00000X011110X0101 +mem[52317] = 3581460 +mem[22238] = 92255 +mem[6688] = 29033188 +mem[7311] = 1092 +mem[1099] = 379272 +mem[5143] = 9437126 +mask = X0XX01111111111001X10X00110011101010 +mem[23050] = 9416 +mem[34100] = 821285 +mem[62807] = 1156515 +mask = 1001110010X11X111X11011001001X1111X1 +mem[50851] = 1266 +mem[23545] = 177017 +mem[12266] = 483 +mask = 0011X01001010X00101X000011X001110000 +mem[11872] = 12823 +mem[1831] = 38511 +mem[25974] = 553433 +mem[49217] = 1793537 +mem[37800] = 372 +mask = 10001101X0XX1011011101011XXX00000001 +mem[34122] = 466038899 +mem[775] = 32552509 +mem[32422] = 25983159 +mem[46550] = 4296 +mask = 1000X100111111XX01X1001X101010100110 +mem[31650] = 10524288 +mem[6322] = 140581 +mem[10638] = 3290724 +mem[35709] = 137485 +mem[20467] = 175698926 +mask = 1X100011X011X111X11X1001X10010X11X10 +mem[62710] = 140559632 +mem[26295] = 506581898 +mem[23934] = 2027 +mem[1770] = 1402 +mem[59755] = 3234994 +mem[27304] = 923348 +mask = 0000010X11111X00011010100X110010X111 +mem[6824] = 46336422 +mem[56833] = 41706 +mem[45316] = 1198 +mem[39426] = 48436 +mask = 100001001101X1X00111XXXX01X0100XX000 +mem[58691] = 16515016 +mem[11879] = 1449419 +mem[22694] = 1761 +mem[4517] = 6645515 +mem[25556] = 3758 +mem[6868] = 28390000 +mem[32545] = 304290 +mask = X00001X01XX111XX011101010X0X101X0110 +mem[49156] = 266240397 +mem[4497] = 84363 +mem[29232] = 126554202 +mem[12306] = 155188 +mem[48047] = 22264 +mask = X10X110111100X0X011XX0111111XX10001X +mem[31701] = 73338 +mem[31978] = 1151135 +mem[7483] = 479508851 +mem[36135] = 3964640 +mem[45859] = 62507 +mem[62676] = 99387068 +mask = 0100X11110101XX1011010001X000X111100 +mem[44119] = 222861 +mem[16458] = 1512948 +mem[45791] = 21526597 +mem[19808] = 2902702 +mem[6868] = 2678 +mem[21659] = 112735 +mask = X1001XX11110X1010X11X01101111XX0X101 +mem[30512] = 802102210 +mem[27203] = 3779646 +mem[16581] = 238152 +mem[16257] = 5959659 +mem[39296] = 63813 +mem[16174] = 87595 +mem[10104] = 1072 +mask = 0000011XX1X1X1X0X11X001X000110000010 +mem[28838] = 5264 +mem[42509] = 111014106 +mem[24905] = 521203049 +mem[9718] = 15454 +mem[16951] = 22743 +mask = 100X0101X1110101X0X0X0010101110100X1 +mem[7483] = 40730 +mem[9639] = 13929656 +mem[25817] = 2819 +mem[16257] = 274898612 +mem[55439] = 598143974 +mask = 110011111110X11101X0000X1010X110000X +mem[37397] = 3714 +mem[54979] = 794 +mem[1014] = 3463104 +mem[50612] = 194522027 +mem[4969] = 337504357 +mem[53665] = 766941 +mask = 1X00X111111X1111011110001X11100011XX +mem[24546] = 1830044 +mem[57219] = 94920 +mem[31992] = 29180 +mem[34078] = 227189 +mem[59660] = 111830573 +mem[58661] = 248870 +mem[15971] = 175220352 +mask = 00000XX0X11X01000XX00111000X0000X010 +mem[1571] = 215979 +mem[14641] = 30916273 +mem[14751] = 102869423 +mem[19244] = 8960072 +mask = 0X100X1001X10001101X0X00X0X0XX010100 +mem[21724] = 288136 +mem[28198] = 22553074 +mem[57765] = 2310045 +mask = 0101X1X1111000X0011X1010111000100110 +mem[10510] = 8313 +mem[5922] = 72921 +mem[64407] = 1556 +mem[25450] = 1777041 +mem[28796] = 2327 +mem[59755] = 21569957 +mem[60972] = 2783427 +mask = 100011X1100110110X1101011X100X010111 +mem[40715] = 959356 +mem[63146] = 122011 +mem[19713] = 1825336 +mem[45607] = 218 +mask = 100111001XX11X11101101XX1X110101X0X1 +mem[12007] = 98 +mem[26115] = 141260 +mem[54804] = 1053489 +mem[13804] = 14284 +mem[61114] = 912 +mask = 00X0001X01X10X0110X0010101000X0010XX +mem[17557] = 140533204 +mem[26033] = 2041132 +mask = 10X111010X1X11X0001101X10X1000111X01 +mem[38981] = 1634 +mem[18192] = 56914 +mem[59630] = 563 +mask = 1X0X110111110X01101X00011101X1001101 +mem[41221] = 497 +mem[56726] = 8181081 +mem[1971] = 188296 +mask = 100X11001111111X0X110X00101011010100 +mem[21216] = 381876688 +mem[34122] = 2467929 +mem[2144] = 96144 +mem[54135] = 13354 +mem[53720] = 150 +mem[32315] = 54538146 +mask = 0000X10X11111100011010010XXX0010X000 +mem[18215] = 31721 +mem[32721] = 9183703 +mem[19864] = 62401757 +mem[35244] = 775737 +mem[41775] = 8351542 +mask = 110X01X011X1111011XXX00011X000X11011 +mem[17028] = 8956 +mem[6912] = 1617661 +mem[15337] = 11644529 +mem[43968] = 14512 +mem[8379] = 45870175 +mem[27993] = 549587 +mask = 00X00XX0010100001010010X0110010000X0 +mem[14444] = 53809059 +mem[12350] = 50956019 +mem[26924] = 27242111 +mem[15497] = 25477955 +mem[11723] = 28495396 +mem[22036] = 6534 +mask = 11XX1101X1X00000011110111X110111011X +mem[23534] = 1008959 +mem[52268] = 340236 +mem[35387] = 1963826 +mem[51744] = 1389733 +mem[37913] = 626 +mem[50832] = 950206044 +mask = 1000X1XX110111X00X110XXX010111011X10 +mem[1361] = 63857600 +mem[52318] = 239923379 +mem[28591] = 568 +mem[40351] = 27729 +mem[19237] = 603 +mem[38475] = 59358178 +mask = 110001111X1111X1011X000010X0X00101X1 +mem[1940] = 44417 +mem[56833] = 11059 +mem[60379] = 5011 +mem[9192] = 59823 +mem[29224] = 357704 +mem[8154] = 13209 +mask = 1100100XX1100X01X1X10011011110001000 +mem[4930] = 145441 +mem[25421] = 12045 +mem[44108] = 46104 +mask = 1X00X1001111X110X11X1011001X01100X10 +mem[22768] = 3060 +mem[4912] = 261459309 +mem[30948] = 398446577 +mem[56062] = 104 +mask = X00XXX0XX11101011X1001000X0110X11111 +mem[51488] = 749 +mem[56696] = 3357828 +mem[14571] = 9057 +mem[5703] = 88 +mask = 1XX00111XX111111011X10X0101X101X1X11 +mem[22686] = 13095 +mem[59326] = 3001957 +mem[63953] = 71522183 +mem[35214] = 3703802 +mem[48938] = 11233495 +mask = 00X000X1X1X11111X110010X000X10100011 +mem[41999] = 936 +mem[58058] = 261362 +mem[54367] = 2216 +mem[61978] = 70816 +mask = X10X11X1111001000X1X00X011010X0XX110 +mem[45798] = 3907 +mem[6617] = 15512 +mem[56811] = 708 +mask = X0100X11X1111X11011011X1X0011X101X0X +mem[58419] = 3631 +mem[27483] = 38986315 +mem[59244] = 398710 +mem[23764] = 14373 +mem[14069] = 12712994 +mem[40576] = 40270 +mask = 1000110000X11X11011100111X1110000X00 +mem[57219] = 11562875 +mem[41221] = 13920777 +mem[36717] = 3819625 +mask = 1X00X1001X11111X011100XX10100X1100X0 +mem[56785] = 134175 +mem[8778] = 254917 +mem[12948] = 510951 +mem[128] = 860 +mem[11157] = 676314432 +mask = 00000X1X1X1110X00110010X001011001011 +mem[9240] = 1401 +mem[32110] = 1104 +mem[30506] = 88753652 +mem[6582] = 2187931 +mem[12627] = 139691 +mask = 10XX0111X0111111011010001010100X1100 +mem[13077] = 3934 +mem[50479] = 610 +mem[33713] = 72792134 +mem[24686] = 1299 +mem[33343] = 336598 +mask = X00XX10010111110XX00001011X0XX0110X0 +mem[65214] = 6485641 +mem[7967] = 15431108 +mem[54018] = 1706606 +mask = 1X0011X0111111X001110011X00X1111000X +mem[52561] = 13472068 +mem[56018] = 18723453 +mem[42466] = 85170787 +mem[37684] = 1550 +mem[8720] = 738919690 +mask = 100111001X1111XXX01X000X10101101X1XX +mem[53713] = 1151 +mem[50072] = 42179504 +mem[7557] = 536670970 +mem[53473] = 62 +mem[30512] = 1563 +mem[61203] = 123868045 +mem[12947] = 280992 +mask = X00X0X1010011110011X11000000X01X00X0 +mem[53713] = 178290029 +mem[36998] = 5983099 +mem[47628] = 5173 +mask = X000010101110101001000X1X10101000X00 +mem[46136] = 244458655 +mem[52984] = 4858009 +mem[22937] = 6676 +mem[23388] = 375417 +mem[64350] = 969 +mask = 000000X10X1101011010010X00001000X11X +mem[5143] = 27026 +mem[51643] = 38589 +mem[59365] = 1607 +mem[58054] = 1872 +mask = 100111X0101X1110X0100X11111X10010X00 +mem[40606] = 3049691 +mem[12350] = 14661 +mem[35882] = 61044 +mem[14176] = 38552 +mem[2496] = 450375 +mem[46933] = 44633552 +mask = X1X00X110XX1111101101001011000X0101X +mem[43177] = 822529775 +mem[28838] = 223 +mem[10298] = 849778 +mem[43038] = 105444375 +mem[42666] = 5936816 +mask = X001110010111110101X0X0011XX10001100 +mem[2328] = 76985 +mem[3801] = 1771180 +mem[19235] = 134961 +mem[53720] = 97593730 +mask = 1001110011X10XX1111010XX0X01X0111000 +mem[16129] = 1224773 +mem[18934] = 47178 +mem[17977] = 341 +mem[18583] = 2852768 +mask = 0000XX101111101X0101010X0X0X1000010X +mem[50612] = 862 +mem[21286] = 41878 +mem[31874] = 10360641 +mem[65381] = 195095 +mem[61682] = 9060 +mem[32198] = 7428006 +mem[32240] = 49912129 +mask = 001X00100101000X101X00X000XXX101X10X +mem[699] = 21649437 +mem[58127] = 1606 +mem[49337] = 31439847 +mem[3618] = 8032 +mem[29699] = 125520 +mem[29732] = 46761 +mask = 1000010X11X111000XX10101X1X01X100X10 +mem[31281] = 12657 +mem[44108] = 130509615 +mem[19496] = 60937467 +mem[56486] = 3157958 +mem[35746] = 27492 +mask = 1001XX01X111X1X0001X00X1011X00110001 +mem[30357] = 47012 +mem[18233] = 12393 +mem[1099] = 27135 +mask = X1000X111010X1X10110X000X0000011011X +mem[50296] = 1603392 +mem[35873] = 35369472 +mem[17028] = 200141095 +mem[49219] = 16711 +mem[35720] = 9247398 +mem[19911] = 144739658 +mem[7886] = 9679 +mask = 0010001X0111101101X01X0X010100X110X0 +mem[19896] = 217854 +mem[57355] = 922353 +mem[33713] = 141968390 +mem[41095] = 3604 +mem[12007] = 579276 +mask = 000001X011111XXX011XX0010101X00XX010 +mem[63211] = 223085398 +mem[49337] = 262164785 +mem[48047] = 1784038 +mem[50214] = 121056 +mem[16951] = 6863479 +mem[48471] = 5459 +mask = 1000011010011X0X01X1000X0100101XX110 +mem[8379] = 75563320 +mem[48049] = 2046786 +mem[51605] = 95555128 +mem[20519] = 3218 +mem[54018] = 298192086 +mem[52374] = 3157399 +mask = 1X1000110X1111110110100X0100XX1X110X +mem[35163] = 119779 +mem[23965] = 261934 +mem[26814] = 4046757 +mem[56811] = 47723376 +mem[26957] = 4810 +mask = 1X001100X011101X0X111010011010X0X111 +mem[37397] = 896 +mem[33586] = 151022681 +mem[51899] = 249489 +mask = 1000X00XX1X1111011110000110011101011 +mem[59834] = 3647285 +mem[9353] = 3923071 +mask = 0000011111X110110X1X010X0110X0X00110 +mem[42000] = 4165519 +mem[33425] = 36 +mem[1188] = 3503480 +mem[3603] = 532 +mem[4150] = 9701863 +mem[6458] = 11777 +mem[16174] = 55920277 +mask = 01100XXX011100011011011X10101X1X0101 +mem[14799] = 1948676 +mem[45433] = 3277 +mem[43873] = 258105154 +mem[26588] = 467687 +mem[36891] = 44643 +mask = 001X001X010100011011000X000X1000X100 +mem[53654] = 21544645 +mem[48482] = 255962506 +mem[39155] = 6486 +mem[5144] = 1463 +mem[1571] = 184289 +mem[32545] = 3427 +mask = 1X0X0100101XX111011X0X1010100110010X +mem[12534] = 19727 +mem[7245] = 989 +mem[54979] = 56838 +mask = X00111X010XX111000XX0011X01010010100 +mem[47330] = 3421396 +mem[48131] = 29616230 +mem[6868] = 29690 +mem[61114] = 167498 +mem[13174] = 266314582 +mem[46758] = 778753 +mem[6688] = 176052 +mask = 1101X1011110000XXXX100X000100010001X +mem[16129] = 15847806 +mem[56016] = 66002919 +mask = 1000110010111111XX1X0010101X1X0X0110 +mem[54135] = 10002 +mem[1555] = 16539906 +mem[10358] = 3453 +mask = 1000110010X11X1101111X100010101000X1 +mem[11157] = 279881 +mem[53713] = 9651034 +mem[36785] = 310702 +mem[34588] = 5487 +mem[12063] = 14550145 +mem[23545] = 4041181 +mem[51935] = 745 +mask = 100001XX11011100X0111101101X01000X0X +mem[8098] = 368361606 +mem[34147] = 2461575 +mem[59550] = 200895 +mem[11123] = 3128 +mem[47604] = 40815968 +mask = 1100X1111X1X11X1011X00001X1110X0010X +mem[52561] = 78059370 +mem[433] = 1907 +mem[35230] = 3585 +mem[12964] = 1879 +mask = 000X01111101X011X0111011011000X1001X +mem[35870] = 75432030 +mem[28796] = 5745 +mem[53430] = 17634037 +mem[62177] = 592 +mask = 1X0001X1X1XX11X001X1X00111X011100010 +mem[40863] = 65853640 +mem[20522] = 209364300 +mask = 1X000X0011XX1110XX110X00110X10101011 +mem[2158] = 24116032 +mem[22745] = 1609694 +mem[26857] = 3024935 +mem[30768] = 1210739 +mem[59961] = 4345089 +mask = 1001110XX111110X0011010X01X100101XX1 +mem[1555] = 39088167 +mem[11559] = 1142755 +mem[15057] = 1645973 +mem[36549] = 7583615 +mem[44440] = 969 +mem[50670] = 5038261 +mask = 110010011110010100X10X0XX110X0100001 +mem[35244] = 16194790 +mem[58608] = 62816 +mem[27803] = 13303703 +mem[9172] = 1355842 +mem[26644] = 1340 +mem[49396] = 1051 +mem[12058] = 27414753 +mask = 10XX1100111111100111000010XX00100010 +mem[35746] = 6513 +mem[2226] = 1202233 +mem[50790] = 54270 +mem[8778] = 199450327 +mask = 000001XX1111X011X1X101010X1X101X010X +mem[31992] = 8657 +mem[646] = 45280 +mem[57264] = 585 +mask = 1000110000X0101XX11100110010X0000101 +mem[6912] = 78358309 +mem[43538] = 229 +mask = 101X00110X111111X1X0101X1100X1X01X0X +mem[30357] = 438097061 +mem[16494] = 52142984 +mem[7423] = 171318353 +mem[4516] = 1114353 +mem[37939] = 3836 diff --git a/day14/Makefile b/day14/Makefile new file mode 100644 index 0000000..0ea01ad --- /dev/null +++ b/day14/Makefile @@ -0,0 +1,30 @@ +INPUT := INPUT.txt +SHELL := /bin/bash +#CFLAGS := -w -g +#CFLAGS := -w -g -pg +CFLAGS := -w -O3 +TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n" +export PATH := .:$(PATH) + +.PHONY: clean all compile deploy ex1 ex2 + +all: ex1 ex2 + +output: + @$(MAKE) --no-print-directory all 2>&1 > OUTPUT + +compile: ex1-c ex2-c + +ex1: ex1-c + @$(TIME) ex1.bash < $(INPUT) 2>&1 + @$(TIME) ex1-c < $(INPUT) 2>&1 + +ex2: ex2-c + @$(TIME) ex2.bash < $(INPUT) 2>&1 + @$(TIME) ex2-c < $(INPUT) 2>&1 + +clean: + @rm -f ex1-c ex2-c core + +deploy: + @$(MAKE) -C .. deploy diff --git a/day14/OUTPUT b/day14/OUTPUT new file mode 100644 index 0000000..6d34bcd --- /dev/null +++ b/day14/OUTPUT @@ -0,0 +1,16 @@ +ex1.bash : res=10035335144067 + time: 0:00.06 real, 0.04 user, 0.01 sys + context-switch: 5+1, page-faults: 0+200 + +ex1-c : res=10035335144067 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+80 + +ex2.bash : res=3817372618036 + time: 0:06.69 real, 6.68 user, 0.00 sys + context-switch: 689+1, page-faults: 0+5949 + +ex2-c : res=3817372618036 + time: 0:00.06 real, 0.05 user, 0.00 sys + context-switch: 6+1, page-faults: 0+662 + diff --git a/day14/README b/day14/README new file mode 100644 index 0000000..fc75e44 --- /dev/null +++ b/day14/README @@ -0,0 +1,106 @@ +--- Day 14: Docking Data --- + +As your ferry approaches the sea port, the captain asks for your help again. The computer system that runs this port isn't compatible with the docking program on the ferry, so the docking parameters aren't being correctly initialized in the docking program's memory. + +After a brief inspection, you discover that the sea port's computer system uses a strange bitmask system in its initialization program. Although you don't have the correct decoder chip handy, you can emulate it in software! + +The initialization program (your puzzle input) can either update the bitmask or write a value to memory. Values and memory addresses are both 36-bit unsigned integers. For example, ignoring bitmasks for a moment, a line like mem[8] = 11 would write the value 11 to memory address 8. + +The bitmask is always given as a string of 36 bits, written with the most significant bit (representing 2^35) on the left and the least significant bit (2^0, that is, the 1s bit) on the right. The current bitmask is applied to values immediately before they are written to memory: a 0 or 1 overwrites the corresponding bit in the value, while an X leaves the bit in the value unchanged. + +For example, consider the following program: + +mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X +mem[8] = 11 +mem[7] = 101 +mem[8] = 0 + +This program starts by specifying a bitmask (mask = ....). The mask it specifies will overwrite two bits in every written value: the 2s bit is overwritten with 0, and the 64s bit is overwritten with 1. + +The program then attempts to write the value 11 to memory address 8. By expanding everything out to individual bits, the mask is applied as follows: + +value: 000000000000000000000000000000001011 (decimal 11) +mask: XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X +result: 000000000000000000000000000001001001 (decimal 73) + +So, because of the mask, the value 73 is written to memory address 8 instead. Then, the program tries to write 101 to address 7: + +value: 000000000000000000000000000001100101 (decimal 101) +mask: XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X +result: 000000000000000000000000000001100101 (decimal 101) + +This time, the mask has no effect, as the bits it overwrote were already the values the mask tried to set. Finally, the program tries to write 0 to address 8: + +value: 000000000000000000000000000000000000 (decimal 0) +mask: XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X +result: 000000000000000000000000000001000000 (decimal 64) + +64 is written to address 8 instead, overwriting the value that was there previously. + +To initialize your ferry's docking program, you need the sum of all values left in memory after the initialization program completes. (The entire 36-bit address space begins initialized to the value 0 at every address.) In the above example, only two values in memory are not zero - 101 (at address 7) and 64 (at address 8) - producing a sum of 165. + +Execute the initialization program. What is the sum of all values left in memory after it completes? (Do not truncate the sum to 36 bits.) + +Your puzzle answer was 10035335144067. +--- Part Two --- + +For some reason, the sea port's computer system still can't communicate with your ferry's docking program. It must be using version 2 of the decoder chip! + +A version 2 decoder chip doesn't modify the values being written at all. Instead, it acts as a memory address decoder. Immediately before a value is written to memory, each bit in the bitmask modifies the corresponding bit of the destination memory address in the following way: + + If the bitmask bit is 0, the corresponding memory address bit is unchanged. + If the bitmask bit is 1, the corresponding memory address bit is overwritten with 1. + If the bitmask bit is X, the corresponding memory address bit is floating. + +A floating bit is not connected to anything and instead fluctuates unpredictably. In practice, this means the floating bits will take on all possible values, potentially causing many memory addresses to be written all at once! + +For example, consider the following program: + +mask = 000000000000000000000000000000X1001X +mem[42] = 100 +mask = 00000000000000000000000000000000X0XX +mem[26] = 1 + +When this program goes to write to memory address 42, it first applies the bitmask: + +address: 000000000000000000000000000000101010 (decimal 42) +mask: 000000000000000000000000000000X1001X +result: 000000000000000000000000000000X1101X + +After applying the mask, four bits are overwritten, three of which are different, and two of which are floating. Floating bits take on every possible combination of values; with two floating bits, four actual memory addresses are written: + +000000000000000000000000000000011010 (decimal 26) +000000000000000000000000000000011011 (decimal 27) +000000000000000000000000000000111010 (decimal 58) +000000000000000000000000000000111011 (decimal 59) + +Next, the program is about to write to memory address 26 with a different bitmask: + +address: 000000000000000000000000000000011010 (decimal 26) +mask: 00000000000000000000000000000000X0XX +result: 00000000000000000000000000000001X0XX + +This results in an address with three floating bits, causing writes to eight memory addresses: + +000000000000000000000000000000010000 (decimal 16) +000000000000000000000000000000010001 (decimal 17) +000000000000000000000000000000010010 (decimal 18) +000000000000000000000000000000010011 (decimal 19) +000000000000000000000000000000011000 (decimal 24) +000000000000000000000000000000011001 (decimal 25) +000000000000000000000000000000011010 (decimal 26) +000000000000000000000000000000011011 (decimal 27) + +The entire 36-bit address space still begins initialized to the value 0 at every address, and you still need the sum of all values left in memory at the end of the program. In this example, the sum is 208. + +Execute the initialization program using an emulator for a version 2 decoder chip. What is the sum of all values left in memory after it completes? + +Your puzzle answer was 3817372618036. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. + +If you still want to see it, you can get your puzzle input. + +You can also [Share] this puzzle. diff --git a/day14/TEST.txt b/day14/TEST.txt new file mode 100644 index 0000000..e15150a --- /dev/null +++ b/day14/TEST.txt @@ -0,0 +1,4 @@ +mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X +mem[8] = 11 +mem[7] = 101 +mem[8] = 0 diff --git a/day14/TEST2.txt b/day14/TEST2.txt new file mode 100644 index 0000000..2e961e3 --- /dev/null +++ b/day14/TEST2.txt @@ -0,0 +1,4 @@ +mask = 000000000000000000000000000000X1001X +mem[42] = 100 +mask = 00000000000000000000000000000000X0XX +mem[26] = 1 diff --git a/day14/TEST3.txt b/day14/TEST3.txt new file mode 100644 index 0000000..6f8790e --- /dev/null +++ b/day14/TEST3.txt @@ -0,0 +1,12 @@ +mask = 0000011011111X1001100X0001X1001100X0 +mem[43805] = 6934 +mem[57564] = 3741 +mem[28260] = 67554 +mem[16151] = 813 +mem[3058] = 873646 +mem[51717] = 270314 +mask = 1001X1X011110XX111100001100111001110 +mem[16252] = 730 +mem[28701] = 63670997 +mem[28652] = 9219200 +mem[64761] = 488985928 diff --git a/day14/ex1-c.c b/day14/ex1-c.c new file mode 100644 index 0000000..5dd9120 --- /dev/null +++ b/day14/ex1-c.c @@ -0,0 +1,130 @@ +/* ex1-c: Advent2020 game, day 14/game 1 + */ + +#include +#include +#include + +struct memcell { + unsigned long long num; + unsigned long long val; + struct memcell *next; /* if collision */ +}; +struct hashtable { + struct memcell *first; +}; + +#define HASHSIZE 1024 +#define HASH(i) ((i)%HASHSIZE) + +static struct hashtable mem[HASHSIZE]; +static char maskstr[128]; + +struct memcell *get_cell(cell) + unsigned long long cell; +{ + struct memcell *p=mem[HASH(cell)].first; + + while (p && p->num != cell) + p=p->next; + return p; +} + +struct memcell *set_cell(cell, val) + unsigned long long cell; + unsigned long long val; +{ + struct memcell *p=get_cell(cell); + unsigned hash=HASH(cell); + + if (!p) { + p=malloc(sizeof (struct memcell)); + p->next=mem[hash].first; + mem[hash].first=p; + } + p->num=cell; + p->val=val; + return p; +} + +unsigned long long print_cells() +{ + unsigned i; + unsigned long long res=0; + struct memcell *p; + + puts("CELLS :"); + for (i=0; inum, p->val); + res+=p->val; + p=p->next; + } + } + return res; +} + +unsigned long long calc_cells() +{ + unsigned i; + unsigned long long res=0; + struct memcell *p; + + for (i=0; ival; + p=p->next; + } + } + return res; +} + +unsigned long long ormask(str) + char *str; +{ + char *p; + unsigned long long mask=0, exp=1; + + for (p=str+strlen(str)-1; p>=str; p--, exp<<=1) + if (*p == '1') + mask+=exp; + return mask; +} + +unsigned long long andmask(str) + char *str; +{ + char *p; + unsigned long long mask=0, exp=1; + + for (p=str+strlen(str)-1; p>=str; p--, exp<<=1) + if (*p != '0') + mask+=exp; + return mask; +} + +int main(ac, av) + int ac; + char **av; +{ + char line[1024]; + unsigned long long val, mempos, or, and; + + while (fgets(line, sizeof line, stdin)) { + if (!strncmp(line, "mask", 4)) { + sscanf(line, "%*[^01X]%s", maskstr); + or=ormask(maskstr); + and=andmask(maskstr); + } else { + sscanf(line, "%*[^0-9]%llu] = %llu", &mempos, &val); + //printf("mem[%llu]=%llu\n", mempos, val); + set_cell(mempos, (val&and)|or); + } + } + //print_cells(); + + printf("%s : res=%llu\n", *av, calc_cells()); + exit (0); +} diff --git a/day14/ex1.bash b/day14/ex1.bash new file mode 100755 index 0000000..2276bc2 --- /dev/null +++ b/day14/ex1.bash @@ -0,0 +1,31 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 14/game 1. + +CMD=${0##*/} +shopt -s extglob +declare -A mem +declare -i pos +declare ORMASK ANDMASK +declare mask="" + +while read -r line; do + line=${line// /} + eval "$line" + + if [[ $line =~ ^mask ]]; then + ORMASK=2\#${mask//X/0} + ANDMASK=2\#${mask//X/1} + else + [[ $line =~ ^.*\[([0-9]+)\].* ]] + pos=${BASH_REMATCH[1]} + ((mem[$pos] &= ANDMASK)) + ((mem[$pos] |= ORMASK)) + fi +done +SUM=0 +for i in "${!mem[@]}"; do + ((SUM+=${mem[$i]})) +done +printf "%s : res=%d\n" "$CMD" "$SUM" +exit 0 diff --git a/day14/ex2-c.c b/day14/ex2-c.c new file mode 100644 index 0000000..a497eef --- /dev/null +++ b/day14/ex2-c.c @@ -0,0 +1,163 @@ +/* ex1-c: Advent2020 game, day 14/game 1 + */ + +#include +#include +#include + +struct memcell { + unsigned long long num; + unsigned long long val; + struct memcell *next; /* if collision */ +}; +struct hashtable { + struct memcell *first; +}; + +#define HASHSIZE 1024 +#define HASH(i) ((i)%HASHSIZE) + +static struct hashtable mem[HASHSIZE]; +static char maskstr[128]; +static unsigned long long exp[128]; + +struct memcell *get_cell(cell) + unsigned long long cell; +{ + struct memcell *p=mem[HASH(cell)].first; + + while (p && p->num != cell) + p=p->next; + return p; +} + +struct memcell *set_cell(cell, val) + unsigned long long cell; + unsigned long long val; +{ + struct memcell *p=get_cell(cell); + unsigned hash=HASH(cell); + + if (!p) { + p=malloc(sizeof (struct memcell)); + p->next=mem[hash].first; + mem[hash].first=p; + } + p->num=cell; + p->val=val; + return p; +} + +unsigned long long print_cells() +{ + unsigned i; + unsigned long long res=0; + struct memcell *p; + + puts("CELLS :"); + for (i=0; inum, p->val); + res+=p->val; + p=p->next; + } + } + return res; +} + +unsigned long long calc_cells() +{ + unsigned i; + unsigned long long res=0; + struct memcell *p; + + for (i=0; ival; + p=p->next; + } + } + return res; +} + +unsigned long long ormask(str) + char *str; +{ + char *p; + unsigned long long mask=0, exp=1; + + for (p=str+strlen(str)-1; p>=str; p--, exp<<=1) + if (*p == '1') + mask+=exp; + return mask; +} + +unsigned long long andmask(str) + char *str; +{ + char *p; + unsigned long long mask=0, exp=1; + + for (p=str+strlen(str)-1; p>=str; p--, exp<<=1) + if (*p != 'X') + mask+=exp; + return mask; +} + +void print_exp() +{ + int i; + for (i=0; exp[i]; ++i) { + printf("%2d: %llu", i, exp[i]); + } + +} + +void calc_masks(mempos, val, cur) + unsigned long mempos, val; + int cur; +{ + unsigned long long mask=exp[cur]; + + if (mask == 0) { + set_cell(mempos, val); + return; + } + calc_masks(mempos, val, cur+1); + calc_masks(mempos+mask, val, cur+1); +} + +int main(ac, av) + int ac; + char **av; +{ + char line[1024]; + unsigned long long val, mempos, or, and; + int i=0, j; + + while (fgets(line, sizeof line, stdin)) { + if (!strncmp(line, "mask", 4)) { + sscanf(line, "%*[^01X]%s", maskstr); + or=ormask(maskstr); + and=andmask(maskstr); + val=1; j=0; + for (i=strlen(maskstr)-1; i>=0; --i) { + if (maskstr[i]=='X') { + exp[j]=val; + j++; + } + + val<<=1; + } + exp[j]=0; + } else { + sscanf(line, "%*[^0-9]%llu] = %llu", &mempos, &val); + mempos=(mempos|or)∧ + calc_masks(mempos, val, 0); + } + } + printf("%s : res=%llu\n", *av, calc_cells()); + exit (0); +} diff --git a/day14/ex2.bash b/day14/ex2.bash new file mode 100755 index 0000000..819f358 --- /dev/null +++ b/day14/ex2.bash @@ -0,0 +1,61 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 14/game 1. + +CMD=${0##*/} +shopt -s extglob +declare -A mem +declare -i pos exp lenmask +declare ORMASK ANDMASK +declare -a xlist + +function applymask() { + local -i curmem val mask cur + if (($# < 3)); then + mem[$1]=$2 + return + fi + + curmem="$1" + val="$2" + mask="$3" + shift 3 + applymask "$curmem" "$val" $* + applymask $((curmem+mask)) "$val" $* +} + +while read -r line; do + line=${line// /} + + if [[ $line =~ ^mask ]]; then + eval "$line" + ORMASK=2\#${mask//X/0} + ANDMASK=2\#${mask//0/1} + ANDMASK=${ANDMASK//X/0} + lenmask=${#mask} + xlist=() + exp=1 + for ((i=1; i<=lenmask; ++i)); do + c=${mask: -i:1} + if [[ "$c" == "X" ]]; then + xlist+=("$exp") + fi + ((exp<<=1)) + done + else + [[ $line =~ ^.*\[([0-9]+)\][^0-9]*([0-9]+)$ ]] + pos=${BASH_REMATCH[1]} + val=${BASH_REMATCH[2]} + ((pos |= ORMASK)) + ((pos &= ANDMASK)) + applymask "$pos" "$val" ${xlist[*]} + + fi + +done +SUM=0 +for i in ${!mem[@]}; do + ((SUM+=${mem[$i]})) +done +printf "%s : res=%d\n" "$CMD" "$SUM" +exit 0