From 401b567e20132a9c188000fd0f0271cd4dd7f13d Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Sat, 4 Dec 2021 16:04:51 +0100 Subject: [PATCH] day 3 part 2 (C) --- 2021/OUTPUT | 23 +++++++++-- 2021/day03/README.txt | 9 ++-- 2021/day03/aoc-c.c | 96 ++++++++++++++++++++++++++++++++++++++----- 3 files changed, 108 insertions(+), 20 deletions(-) diff --git a/2021/OUTPUT b/2021/OUTPUT index c7b5b42..af28929 100644 --- a/2021/OUTPUT +++ b/2021/OUTPUT @@ -5,12 +5,12 @@ +++++++++++++++++ part 1 aoc-c : res=1195 time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+88 + context-switch: 2+1, page-faults: 0+86 +++++++++++++++++ part 2 aoc-c : res=1235 time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+87 + context-switch: 0+1, page-faults: 0+86 ========================================= ================= day02 ================= @@ -19,10 +19,25 @@ aoc-c : res=1235 +++++++++++++++++ part 1 aoc-c : res=1636725 time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+88 + context-switch: 0+1, page-faults: 0+89 +++++++++++++++++ part 2 aoc-c : res=1872757425 time: 0:00.00 real, 0.00 user, 0.00 sys - context-switch: 0+1, page-faults: 0+86 + context-switch: 0+1, page-faults: 0+90 + +========================================= +================= day03 ================= +========================================= + ++++++++++++++++++ part 1 +compiling aoc-c.c +aoc-c : res=3901196 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+89 + ++++++++++++++++++ part 2 +aoc-c : res=4412188 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+92 diff --git a/2021/day03/README.txt b/2021/day03/README.txt index 8613283..b360515 100644 --- a/2021/day03/README.txt +++ b/2021/day03/README.txt @@ -33,12 +33,7 @@ The epsilon rate is calculated in a similar way; rather than use the most common Use the binary numbers in your diagnostic report to calculate the gamma rate and epsilon rate, then multiply them together. What is the power consumption of the submarine? (Be sure to represent your answer in decimal, not binary.) - Your puzzle answer was 3901196. - -The first half of this puzzle is complete! It provides one gold star: * - - --- Part Two --- Next, you should verify the life support rating, which can be determined by multiplying the oxygen generator rating by the CO2 scrubber rating. @@ -73,3 +68,7 @@ Then, to determine the CO2 scrubber rating value from the same example above: Finally, to find the life support rating, multiply the oxygen generator rating (23) by the CO2 scrubber rating (10) to get 230. Use the binary numbers in your diagnostic report to calculate the oxygen generator rating and CO2 scrubber rating, then multiply them together. What is the life support rating of the submarine? (Be sure to represent your answer in decimal, not binary.) + +Your puzzle answer was 4412188. + +Both parts of this puzzle are complete! They provide two gold stars: ** diff --git a/2021/day03/aoc-c.c b/2021/day03/aoc-c.c index 817c22c..387ba8c 100644 --- a/2021/day03/aoc-c.c +++ b/2021/day03/aoc-c.c @@ -56,15 +56,90 @@ static int ex1() static int ex2() { - /* idea : Given 3 bits input - * 101 - * 010 - * 110 - * - * we create an ushort array of size 2⁴ initialized at 2⁴ - * - */ - return 1; + u32 length, size, *values, min, max, oxygen, co2; + u32 zero, one, lastone, lastzero, bit; + char buffer[80]; + u64 val; + + /* get length of binary number */ + scanf("%s", buffer); + length = strlen(buffer); + size = 1 << length; + + values = calloc(size, sizeof(*values)); + + do { + val = strtoul(buffer, NULL, 2); + values[val] = val; + } while (scanf("%s", buffer) != EOF); + + min = 0; + max = size; + bit = size >> 1; + while (1) { + zero = 0; + one = 0; + for (u32 i = min; i < max; ++i) { + if (values[i]) { + if (values[i] & bit) { + one++; + lastone = i; + } else { + zero++; + lastzero = i; + } + } + } + if (one >= zero) { + if (one == 1) { + oxygen = lastone; + break; + } + min = (max + min) / 2; + } else { + if (zero == 1) { + oxygen = lastzero; + break; + } + max = (max + min) / 2; + } + bit >>= 1; + } + + min = 0; + max = size; + bit = size >> 1; + while (1) { + zero = 0; + one = 0; + for (u32 i = min; i < max; ++i) { + if (values[i]) { + if (values[i] & bit) { + one++; + lastone = i; + } else { + zero++; + lastzero = i; + } + } + } + if (zero <= one) { + if (zero == 1) { + co2 = lastzero; + break; + } + max = (max + min) / 2; + } else { + if (one == 1) { + co2 = lastone; + break; + } + min = (max + min) / 2; + } + bit >>= 1; + } + free(values); + return oxygen * co2; } static int usage(char *prg) @@ -97,8 +172,7 @@ int main(int ac, char **av) if (exercise == 1) { res = ex1(); printf ("%s : res=%d\n", *av, res); - } - if (exercise == 2) { + } else { res = ex2(); printf ("%s : res=%d\n", *av, res); }