diff options
author | Marvin Borner | 2020-12-10 18:13:44 +0100 |
---|---|---|
committer | Marvin Borner | 2020-12-10 18:13:44 +0100 |
commit | a31e759755fe8b6cc4ab7e18cacb7e36265ae191 (patch) | |
tree | 3999b7b85c276d7129391136c2d5dcf1743cb023 /2020 | |
parent | c7f0f98d295a8b240f5519d6e339e6ee053a74ae (diff) |
Awesomeness
Diffstat (limited to '2020')
-rw-r--r-- | 2020/10/Makefile | 10 | ||||
-rw-r--r-- | 2020/10/Program.cs | 37 | ||||
-rw-r--r-- | 2020/10/input | 128 | ||||
-rw-r--r-- | 2020/10/solve.c | 80 |
4 files changed, 194 insertions, 61 deletions
diff --git a/2020/10/Makefile b/2020/10/Makefile new file mode 100644 index 0000000..769d06d --- /dev/null +++ b/2020/10/Makefile @@ -0,0 +1,10 @@ +.PHONY: solve.c + +solve.o: solve.c + @gcc $+ -o $@ + +clean: + @rm -f *.o + +run: solve.o + @./solve.o diff --git a/2020/10/Program.cs b/2020/10/Program.cs deleted file mode 100644 index 16ef0ff..0000000 --- a/2020/10/Program.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System;
-using System.IO;
-using System.Linq;
-
-namespace AOC
-{
- internal class Run
- {
- private static int PartOne(int[] data)
- {
- int prev = 0, three = 1, one = 0;
- for (int i = 0; i < data.Length; i++)
- {
- if (data[i] - prev == 3) three++;
- else one++;
-
- prev = data[i];
- }
-
- return one * three;
- }
-
- private static int PartTwo(int[] data)
- {
- return 0;
- }
-
- public static void Main(string[] args)
- {
- string text = File.ReadAllText(@"C:\Users\bornerma\Desktop\input.txt");
- string[] numsStr = text.Split('\n');
- int[] nums = Array.ConvertAll(numsStr, int.Parse).OrderBy(i => i).ToArray();
- Console.WriteLine(PartOne(nums));
- Console.WriteLine(PartTwo(nums));
- }
- }
-}
\ No newline at end of file diff --git a/2020/10/input b/2020/10/input index be5c492..922358c 100644 --- a/2020/10/input +++ b/2020/10/input @@ -1,31 +1,111 @@ -28 -33 +71 +183 +111 +89 +92 +142 +25 +101 +52 +86 18 -42 -31 -14 -46 -20 -48 -47 +22 +70 +2 +135 +163 +34 +143 +153 +35 +144 24 23 -49 -45 -19 +94 +100 +102 +17 +57 +76 +182 +134 38 -39 +7 +103 +66 +31 11 -1 +121 +77 +113 +128 +82 +99 +148 +137 +41 32 -25 -35 -8 -17 -7 -9 -4 -2 -34 +48 +131 +60 +127 +138 +73 +28 10 -3
\ No newline at end of file +84 +180 +63 +125 +53 +176 +165 +114 +145 +152 +72 +107 +167 +59 +164 +78 +126 +118 +136 +83 +79 +58 +14 +106 +69 +51 +39 +157 +42 +177 +173 +93 +141 +3 +33 +13 +19 +45 +154 +95 +170 +54 +181 +6 +151 +1 +112 +96 +115 +85 +108 +166 +160 +40 +122 +12
\ No newline at end of file diff --git a/2020/10/solve.c b/2020/10/solve.c new file mode 100644 index 0000000..83c5535 --- /dev/null +++ b/2020/10/solve.c @@ -0,0 +1,80 @@ +#include <stdio.h> +#include <stdlib.h> + +static int LENGTH = 0; + +int part_one(int *data) +{ + int prev = 0, three = 1, one = 0; + + for (int i = 0; i < LENGTH; i++) { + if (data[i] == 0) + continue; + if (data[i] - prev == 3) + three++; + else + one++; + prev = data[i]; + } + + return one * three; +} + +long part_two(int *data) +{ + long a, b, c; + a = 1; + b = 0; + c = 0; + for (int i = LENGTH - 2; i > 0; i--) { + long next = (i + 1 < LENGTH && data[i + 1] - data[i] <= 3 ? a : 0) + + (i + 2 < LENGTH && data[i + 2] - data[i] <= 3 ? b : 0) + + (i + 3 < LENGTH && data[i + 3] - data[i] <= 3 ? c : 0); + c = b; + b = a; + a = next; + } + + return a; +} + +int cmp(const void *a, const void *b) +{ + int int_a = *((int *)a); + int int_b = *((int *)b); + + if (int_a == int_b) + return 0; + else if (int_a < int_b) + return -1; + else + return 1; +} + +int *to_int(FILE *fp, int *arr) +{ + int d = 0, i = 0; + while (fscanf(fp, "%d\n", &d) != EOF) + arr[i++] = d; + LENGTH = i + 2; + + return arr; +} + +int main() +{ + FILE *fp = fopen("input", "r"); + if (!fp) + exit(EXIT_FAILURE); + + int data[128] = { 0 }; + to_int(fp, data); + qsort(data, LENGTH, sizeof(int), cmp); + data[LENGTH] = data[LENGTH - 1] + 3; + + printf("%d\n", part_one(data)); + printf("%lu\n", part_two(data)); + + fclose(fp); + return 0; +} |