Restore aoc backup.
This commit is contained in:
53
2015/day02/answer01.c
Normal file
53
2015/day02/answer01.c
Normal file
@@ -0,0 +1,53 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int32_t compint(const void *a, const void *b) {
|
||||
uint16_t tmp_a = *((uint16_t *)a);
|
||||
uint16_t tmp_b = *((uint16_t *)b);
|
||||
|
||||
if (tmp_a == tmp_b) return 0;
|
||||
else if (tmp_a > tmp_b) return 1;
|
||||
else return -1;
|
||||
}
|
||||
|
||||
int main() {
|
||||
FILE *file = fopen("input", "r");
|
||||
if (file == NULL) {
|
||||
perror("main: Error opening file.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint16_t l, w, h, sa_tmp;
|
||||
uint32_t sa_cum = 0;
|
||||
int8_t buffer[256];
|
||||
while (fgets(buffer, sizeof(buffer), file) != NULL) {
|
||||
if (sscanf(buffer, "%hux%hux%hu", &l, &w, &h) != 3) {
|
||||
perror("main: Error parsing string.");
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
//calculate surface area
|
||||
sa_tmp = 2*l*w + 2*w*h + 2*l*h;
|
||||
|
||||
//calculate smallest side
|
||||
uint16_t array[3] = {l, w, h};
|
||||
uint16_t n = sizeof(array)/sizeof(array[0]);
|
||||
qsort(array, n, sizeof(array[0]), compint);
|
||||
|
||||
//calculate cumulative surface area
|
||||
sa_cum = sa_cum + sa_tmp + (array[0]*array[1]);
|
||||
}
|
||||
}
|
||||
if (ferror(file)) {
|
||||
perror("main: Error reading from file.");
|
||||
return 1;
|
||||
}
|
||||
fclose(file);
|
||||
|
||||
printf("cumulative surface area: %u\n", sa_cum);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
56
2015/day02/answer02.c
Normal file
56
2015/day02/answer02.c
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int32_t compint(const void *a, const void *b) {
|
||||
uint16_t tmp_a = *((uint16_t *)a);
|
||||
uint16_t tmp_b = *((uint16_t *)b);
|
||||
|
||||
if (tmp_a == tmp_b) return 0;
|
||||
else if (tmp_a > tmp_b) return 1;
|
||||
else return -1;
|
||||
}
|
||||
|
||||
int main() {
|
||||
FILE *file = fopen("input", "r");
|
||||
if (file == NULL) {
|
||||
perror("main: Error opening file.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint16_t l, w, h, sp_tmp;
|
||||
uint32_t ribbon = 0;
|
||||
int8_t buffer[256];
|
||||
while (fgets(buffer, sizeof(buffer), file) != NULL) {
|
||||
if (sscanf(buffer, "%hux%hux%hu", &l, &w, &h) != 3) {
|
||||
perror("main: Error parsing string.");
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
//calculate shortest perimeter
|
||||
uint16_t array[3] = {l, w, h};
|
||||
uint16_t n = sizeof(array)/sizeof(array[0]);
|
||||
qsort(array, n, sizeof(array[0]), compint);
|
||||
sp_tmp = 2*array[0] + 2*array[1];
|
||||
|
||||
//calculate cubic volume
|
||||
uint16_t cv = l*w*h;
|
||||
|
||||
//calculate cumulative ribbon length
|
||||
ribbon = ribbon + sp_tmp + cv;
|
||||
}
|
||||
}
|
||||
if (ferror(file)) {
|
||||
perror("main: Error reading from file.");
|
||||
return 1;
|
||||
}
|
||||
fclose(file);
|
||||
|
||||
printf("total ribbon length: %u\n", ribbon);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
1000
2015/day02/input
Normal file
1000
2015/day02/input
Normal file
File diff suppressed because it is too large
Load Diff
10
2015/day02/makefile
Normal file
10
2015/day02/makefile
Normal file
@@ -0,0 +1,10 @@
|
||||
CC = gcc
|
||||
CFLAGS = -std=c99 -Wall -g
|
||||
LDFLAGS =
|
||||
|
||||
answer01: answer01.c
|
||||
$(CC) answer01.c -o answer01
|
||||
|
||||
answer02: answer02.c
|
||||
$(CC) answer02.c -o answer02
|
||||
|
||||
Reference in New Issue
user.block.title