summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-12-15 02:02:00 -0500
committerJune McEnroe <june@causal.agency>2020-12-15 02:02:00 -0500
commit519aa25c89bc5f372b9ca6727e8026fbba319e4f (patch)
tree643e212d5ac90b7ad18426ce13efa749651e0780
parentSolve day 15 part 2 (diff)
downloadaoc-519aa25c89bc5f372b9ca6727e8026fbba319e4f.tar.gz
aoc-519aa25c89bc5f372b9ca6727e8026fbba319e4f.zip
Update day 14 part 2 with the clever way
Diffstat (limited to '')
-rw-r--r--2020/day14.c14
1 files changed, 3 insertions, 11 deletions
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);