From 519aa25c89bc5f372b9ca6727e8026fbba319e4f Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Tue, 15 Dec 2020 02:02:00 -0500 Subject: Update day 14 part 2 with the clever way --- 2020/day14.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) (limited to '2020') diff --git a/2020/day14.c b/2020/day14.c index 07bab22..78f1885 100644 --- a/2020/day14.c +++ b/2020/day14.c @@ -38,18 +38,10 @@ int main(void) { writes[len++] = (struct Write) { addr, (val & mask) | bits }; addr |= bits; addr &= ~mask; - ul count = 1UL << __builtin_popcountl(mask); - for (ul i = 0; i < count; ++i) { - ul a = addr; - ul w = i; - for (int j = 0; j < 36; ++j) { - if (mask & (1UL << j)) { - a |= (w & 1) << j; - w >>= 1; - } - } - writes2[len2++] = (struct Write) { a, val }; + for (ul m = mask; m; m = (m - 1) & mask) { + writes2[len2++] = (struct Write) { addr | m, val }; } + writes2[len2++] = (struct Write) { addr, val }; } } mergesort(writes, len, sizeof(writes[0]), compar); -- cgit 1.4.1