Initial commit.
This commit is contained in:
14
.clang-format
Normal file
14
.clang-format
Normal file
@@ -0,0 +1,14 @@
|
||||
---
|
||||
AlignConsecutiveShortCaseStatements:
|
||||
Enabled: true
|
||||
AcrossEmptyLines: true
|
||||
AcrossComments: true
|
||||
IndentCaseLabels: true
|
||||
AllowShortBlocksOnASingleLine: Always
|
||||
AllowShortCaseLabelsOnASingleLine: true
|
||||
AllowShortEnumsOnASingleLine: true
|
||||
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
|
||||
AllowShortLoopsOnASingleLine: true
|
||||
IndentWidth: 4
|
||||
PointerAlignment: Left
|
||||
AlignAfterOpenBracket: BlockIndent
|
||||
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
*.o
|
||||
*.so
|
||||
tags
|
||||
*.out
|
||||
.cache
|
||||
build/*
|
||||
vgcore.*
|
||||
compile_commands.json
|
||||
1
2015/one/input.txt
Normal file
1
2015/one/input.txt
Normal file
File diff suppressed because one or more lines are too long
50
2015/one/solution.c
Normal file
50
2015/one/solution.c
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
/* Read the data. */
|
||||
if (!(argc - 1)) exit(1);
|
||||
FILE* f = fopen(argv[1], "rb");
|
||||
if (!f) exit(2); // No file.
|
||||
|
||||
fseek(f, 0, SEEK_END);
|
||||
long fsz = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
if (fsz < 0) {
|
||||
fclose(f);
|
||||
exit(3); // Bad stream.
|
||||
}
|
||||
|
||||
char* buf = malloc(fsz + 1);
|
||||
if (!buf) {
|
||||
fclose(f);
|
||||
exit(4); // Bad malloc.
|
||||
}
|
||||
|
||||
size_t nch = fread(buf, 1, fsz, f);
|
||||
if (nch != fsz) {
|
||||
free(buf);
|
||||
exit(5); // Incomplete read.
|
||||
}
|
||||
|
||||
buf[fsz] = '\0'; // Terminate.
|
||||
|
||||
int floor = 0;
|
||||
int basement_pos = -1;
|
||||
|
||||
/* Parse the data. */
|
||||
for (int i = 0; i < fsz; i++) {
|
||||
switch (buf[i]) {
|
||||
case '(': floor++; break;
|
||||
case ')': floor--; break;
|
||||
default: continue;
|
||||
}
|
||||
|
||||
if (floor == -1 && basement_pos == -1) { basement_pos = i + 1; }
|
||||
}
|
||||
|
||||
printf("Part 1 solution: %d\n", floor);
|
||||
printf("Part 2 solution: %d\n", basement_pos);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user