diff options
author | Ian C <ianc@noddybox.co.uk> | 2021-12-09 13:32:48 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2021-12-09 13:32:48 +0000 |
commit | 8833b7a5b246cad02e253838c07ba1689834e523 (patch) | |
tree | de8ae5cbb97a802a7d2ce30e7db37798ac02be00 /3a.c |
Initial checkin
Diffstat (limited to '3a.c')
-rw-r--r-- | 3a.c | 182 |
1 files changed, 182 insertions, 0 deletions
@@ -0,0 +1,182 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +static int FromBin(const char *p) +{ + size_t b = strlen(p) - 1; + int v = 0; + int f; + + for(f = 0; p[f]; f++, b--) + { + if (p[f] == '1') + { + v |= 1 << b; + } + } + + return v; +} + +static void Chomp(char *p) +{ + size_t l = strlen(p); + + while(l && p[l-1] == '\n') + { + p[--l] = 0; + } +} + +int main(void) +{ + char nums[1000][14] = {0}; + char work[1000][14] = {0}; + char buff[80]; + int bit[12] = {0}; + int count = 0; + char str[13] = {0}; + int b; + int f; + int n; + int oxygen; + int co2; + int lines = 0; + + count = 0; + + while(fgets(buff, sizeof buff, stdin)) + { + Chomp(buff); + strcpy(nums[count++], buff); + } + + lines = count; + + count = 0; + memcpy(work, nums, sizeof nums); + b = 0; + + while(count != 1) + { + for(f = 0; f < 12; f++) + { + bit[f] = 0; + } + + count = 0; + + for(n = 0; n < lines; n++) + { + if (work[n][0]) + { + for(f = 0; f < 12; f++) + { + if (work[n][f] == '1') + { + bit[f]++; + } + } + + count++; + } + } + + if (count == 1) + { + for(n = 0; n < lines; n++) + { + if (work[n][0]) + { + oxygen = FromBin(work[n]); + } + } + } + else + { + char c; + + c = bit[b] >= count/2.0 ? '1':'0'; + + for(n = 0; n < lines; n++) + { + if (work[n][b] != c) + { + work[n][0] = 0; + } + } + + if (b < 11) + { + b++; + } + } + } + + count = 0; + memcpy(work, nums, sizeof nums); + b = 0; + + while(count != 1) + { + for(f = 0; f < 12; f++) + { + bit[f] = 0; + } + + count = 0; + + for(n = 0; n < lines; n++) + { + if (work[n][0]) + { + for(f = 0; f < 12; f++) + { + if (work[n][f] == '1') + { + bit[f]++; + } + } + + count++; + } + } + + if (count == 1) + { + for(n = 0; n < lines; n++) + { + if (work[n][0]) + { + co2 = FromBin(work[n]); + } + } + } + else + { + char c; + + c = (bit[b] < count/2.0) ? '1':'0'; + + for(n = 0; n < lines; n++) + { + if (work[n][b] != c) + { + work[n][0] = 0; + } + } + + if (b < 11) + { + b++; + } + } + } + + printf("oxygen %d\n", oxygen); + printf("co2 %d\n", co2); + printf("product %d\n", oxygen * co2); + + return 0; +} |