Piotr Czajkowski 3 years ago
parent
commit
7703fb5a49
3 changed files with 331 additions and 0 deletions
  1. 37 0
      day1/challenge.txt
  2. 94 0
      day1/day1.c
  3. 200 0
      day1/input.txt

+ 37 - 0
day1/challenge.txt

@@ -0,0 +1,37 @@
+--- Day 1: Report Repair ---
+
+After saving Christmas five years in a row, you've decided to take a vacation at a nice resort on a tropical island. Surely, Christmas will go on without you.
+
+The tropical island has its own currency and is entirely cash-only. The gold coins used there have a little picture of a starfish; the locals just call them stars. None of the currency exchanges seem to have heard of them, but somehow, you'll need to find fifty of these coins by the time you arrive so you can pay the deposit on your room.
+
+To save your vacation, you need to get all fifty stars by December 25th.
+
+Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
+
+Before you leave, the Elves in accounting just need you to fix your expense report (your puzzle input); apparently, something isn't quite adding up.
+
+Specifically, they need you to find the two entries that sum to 2020 and then multiply those two numbers together.
+
+For example, suppose your expense report contained the following:
+
+1721
+979
+366
+299
+675
+1456
+
+In this list, the two entries that sum to 2020 are 1721 and 299. Multiplying them together produces 1721 * 299 = 514579, so the correct answer is 514579.
+
+Of course, your expense report is much larger. Find the two entries that sum to 2020; what do you get if you multiply them together?
+
+Your puzzle answer was 445536.
+--- Part Two ---
+
+The Elves in accounting are thankful for your help; one of them even offers you a starfish coin they had left over from a past vacation. They offer you a second one if you can find three numbers in your expense report that meet the same criteria.
+
+Using the above example again, the three entries that sum to 2020 are 979, 366, and 675. Multiplying them together produces the answer, 241861950.
+
+In your expense report, what is the product of the three entries that sum to 2020?
+
+Your puzzle answer was 138688160.

+ 94 - 0
day1/day1.c

@@ -0,0 +1,94 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+int arraySize = 0;
+
+int **readInput(FILE *fp) {
+	int **array = NULL;
+	int index = 0;
+
+	while (1) {
+		if (feof(fp)) break;
+
+		int *p = malloc(sizeof(int));
+		if (!p) return NULL;
+
+		if (1 != fscanf(fp, "%d\n", p))
+			return NULL;
+
+		arraySize++;
+		int **newArray = realloc(array, sizeof(int*)*arraySize);
+		if (!newArray) return NULL;
+		array = newArray;
+		array[index] = p;
+		index++;
+	}
+
+	return array;
+}
+
+void freeArray(int **array) {
+	for (int i = 0; i < arraySize; i++)
+		free(array[i]);
+
+	free(array);
+}
+
+void part1(int **array, int target) {
+	for (int i = 0; i < arraySize; i++) {
+		int first = *array[i];
+		int second = target - first;
+
+		for (int j = i + 1; j < arraySize; j++) {
+			if (*array[j] == second) {
+				printf("Part1: %d\n", first * second);
+				return;
+			}
+		}
+	}
+}
+
+void part2(int **array, int target) {
+	for (int i = 0; i < arraySize; i++) {
+		int first = *array[i];
+		int threshold = target - first;
+
+		for (int j = i + 1; j < arraySize; j++) {
+			if (*array[j] < threshold) {
+				int third = threshold - *array[j];
+				for (int k = j + 1; k < arraySize; k++) {
+					if (*array[k] == third) {
+						printf("Part1: %d\n", first * *array[j] *third);
+						return;
+					}
+
+				}
+			}
+		}
+	}
+}
+
+int main(int argc, char **argv)
+{
+	if (argc < 2) {
+		puts("You need to specify path to file!");
+		return -1;
+	}
+
+	FILE *fp = fopen(argv[1], "r");
+	if (!fp) {
+		puts("Can't read");
+		return -1;
+	}
+
+	int **array = readInput(fp);
+	if (!array) {
+		puts("Can't read array!");
+		return -1;
+	}
+
+	part1(array, 2020);
+	part2(array, 2020);
+	fclose(fp);
+	freeArray(array);
+}

+ 200 - 0
day1/input.txt

@@ -0,0 +1,200 @@
+1511
+1344
+1925
+1970
+1864
+1951
+1557
+1984
+1743
+1526
+1972
+1945
+1969
+1760
+2008
+1592
+736
+1963
+1994
+2009
+1777
+1856
+1899
+1926
+1850
+687
+2005
+1094
+1949
+1326
+2002
+1805
+1493
+1341
+1828
+1778
+1767
+1364
+1973
+1768
+1929
+1377
+2000
+1726
+1913
+2001
+1574
+1859
+1793
+1957
+1959
+1388
+1593
+1392
+724
+1962
+1999
+252
+1982
+1662
+1892
+1610
+1343
+1831
+1862
+1991
+1394
+1946
+1935
+1986
+1911
+1358
+1322
+1956
+1988
+1758
+1490
+1998
+1744
+1844
+1294
+1764
+1543
+1560
+1562
+1747
+1870
+1292
+1989
+1752
+1471
+1980
+1897
+1544
+1914
+1923
+1944
+1375
+1987
+1993
+1742
+1975
+1479
+1977
+1934
+1939
+1950
+1992
+1983
+1474
+1643
+2010
+1814
+1942
+322
+1425
+1646
+1878
+1410
+1927
+1761
+1948
+1779
+1753
+1847
+274
+1659
+1773
+1960
+1772
+1674
+1809
+1568
+1978
+1952
+1947
+1976
+1953
+1961
+1937
+1932
+1781
+2007
+1941
+1393
+1573
+1745
+169
+89
+1408
+1974
+1810
+1979
+1967
+890
+1958
+1930
+1954
+1759
+720
+1936
+1576
+1407
+2004
+1964
+1462
+1875
+1943
+1938
+2006
+1739
+1378
+1922
+1924
+2003
+1792
+1985
+1729
+1966
+1355
+1940
+1928
+1357
+1955
+1896
+1115
+1836
+1971
+1329
+1807
+1997
+1359
+1801
+1933
+1965
+1981
+1711
+1905
+1625
+1968