day 3 part 2 (C)
This commit is contained in:
23
2021/OUTPUT
23
2021/OUTPUT
@@ -5,12 +5,12 @@
|
|||||||
+++++++++++++++++ part 1
|
+++++++++++++++++ part 1
|
||||||
aoc-c : res=1195
|
aoc-c : res=1195
|
||||||
time: 0:00.00 real, 0.00 user, 0.00 sys
|
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
|
+++++++++++++++++ part 2
|
||||||
aoc-c : res=1235
|
aoc-c : res=1235
|
||||||
time: 0:00.00 real, 0.00 user, 0.00 sys
|
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 =================
|
================= day02 =================
|
||||||
@@ -19,10 +19,25 @@ aoc-c : res=1235
|
|||||||
+++++++++++++++++ part 1
|
+++++++++++++++++ part 1
|
||||||
aoc-c : res=1636725
|
aoc-c : res=1636725
|
||||||
time: 0:00.00 real, 0.00 user, 0.00 sys
|
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
|
+++++++++++++++++ part 2
|
||||||
aoc-c : res=1872757425
|
aoc-c : res=1872757425
|
||||||
time: 0:00.00 real, 0.00 user, 0.00 sys
|
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
|
||||||
|
|
||||||
|
@@ -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.)
|
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.
|
Your puzzle answer was 3901196.
|
||||||
|
|
||||||
The first half of this puzzle is complete! It provides one gold star: *
|
|
||||||
|
|
||||||
|
|
||||||
--- Part Two ---
|
--- 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.
|
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.
|
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.)
|
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: **
|
||||||
|
@@ -56,15 +56,90 @@ static int ex1()
|
|||||||
|
|
||||||
static int ex2()
|
static int ex2()
|
||||||
{
|
{
|
||||||
/* idea : Given 3 bits input
|
u32 length, size, *values, min, max, oxygen, co2;
|
||||||
* 101
|
u32 zero, one, lastone, lastzero, bit;
|
||||||
* 010
|
char buffer[80];
|
||||||
* 110
|
u64 val;
|
||||||
*
|
|
||||||
* we create an ushort array of size 2⁴ initialized at 2⁴
|
/* get length of binary number */
|
||||||
*
|
scanf("%s", buffer);
|
||||||
*/
|
length = strlen(buffer);
|
||||||
return 1;
|
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)
|
static int usage(char *prg)
|
||||||
@@ -97,8 +172,7 @@ int main(int ac, char **av)
|
|||||||
if (exercise == 1) {
|
if (exercise == 1) {
|
||||||
res = ex1();
|
res = ex1();
|
||||||
printf ("%s : res=%d\n", *av, res);
|
printf ("%s : res=%d\n", *av, res);
|
||||||
}
|
} else {
|
||||||
if (exercise == 2) {
|
|
||||||
res = ex2();
|
res = ex2();
|
||||||
printf ("%s : res=%d\n", *av, res);
|
printf ("%s : res=%d\n", *av, res);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user