Browse Source

Initial commit

master
Robby Zambito 2 months ago
commit
ecfc2474c9
14 changed files with 2644 additions and 0 deletions
  1. +1
    -0
      .gitignore
  2. +7
    -0
      README.md
  3. +65
    -0
      day1/1/c/main.c
  4. +200
    -0
      day1/1/input
  5. +11
    -0
      day1/1/scala/main.scala
  6. +66
    -0
      day1/2/c/main.c
  7. +200
    -0
      day1/2/input
  8. +11
    -0
      day1/2/scala/main.scala
  9. +27
    -0
      day2/1/c/main.c
  10. +1000
    -0
      day2/1/input
  11. +17
    -0
      day2/1/scala/main.scala
  12. +22
    -0
      day2/2/c/main.c
  13. +1000
    -0
      day2/2/input
  14. +17
    -0
      day2/2/scala/main.scala

+ 1
- 0
.gitignore View File

@ -0,0 +1 @@
**/build/*

+ 7
- 0
README.md View File

@ -0,0 +1,7 @@
# Advent Of Code 2020
These are my solutions for the Advent Of Code 2020 challenges.
All challenges operate over stdin, and can be tested by using `<` to redirect
the input file to the program.

+ 65
- 0
day1/1/c/main.c View File

@ -0,0 +1,65 @@
#include <stdio.h>
#include <stdlib.h>
size_t getNums(long int**, FILE*);
int main(int argc, char *argv[]) {
long int *nums = NULL;
size_t numsLen = getNums(&nums, stdin);
for (size_t i = 0; i < numsLen; i++)
for (size_t j = i; j < numsLen; j++)
if (nums[i] + nums[j] == 2020)
printf("%ld\n", nums[i] * nums[j]);
free(nums);
return EXIT_SUCCESS;
}
/**
* Precondition: stream is the input stream
*
* Postcondition: nums is an array of ints read from stream
* return the length of the nums array
*
*/
size_t getNums(long int **nums, FILE *stream) {
// nums should be NULL if it isn't already
if ((*nums) != NULL) {
free(*nums);
*nums = NULL;
}
size_t resLen = 0;
size_t resCapacity = 10;
*nums = (long int*) malloc(resCapacity * sizeof(long int));
if ((*nums) == NULL) {
perror("Failed to allocate nums\n");
exit(EXIT_FAILURE);
}
long int input = 0;
while (scanf("%ld", &input) > 0) {
// Double capacity of the result array
if (resCapacity == resLen) {
resCapacity *= 2;
long int *temp = (long int*) malloc(resCapacity * sizeof(long int));
if (nums == NULL) {
perror("Failed to allocate temp\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < resLen; i++) {
temp[i] = (*nums)[i];
}
free(*nums);
*nums = temp;
}
(*nums)[resLen++] = input;
}
return resLen;
}

+ 200
- 0
day1/1/input View File

@ -0,0 +1,200 @@
1918
1869
1821
1852
1981
1987
1989
1597
1634
1537
1991
1603
1543
1949
1879
1800
1761
2010
1810
1458
1755
1938
1500
2005
818
1493
1618
1923
1283
1952
1373
1605
1230
1321
1873
1853
1886
1372
1913
1871
159
733
1400
1880
1462
2007
1801
1982
1728
1640
1442
1525
1953
1791
1906
1946
1641
2009
1627
1935
1936
1908
1826
1291
1978
1884
1799
1464
1538
1365
1678
1872
1964
1863
1388
1912
1928
1471
1632
1336
1396
1948
1974
1331
1290
1992
1096
1889
1874
1341
757
1351
1876
1674
2002
1504
1865
1469
1792
1920
1925
1898
1899
1435
405
1488
1319
1322
1749
1867
1957
1944
1727
1293
1739
1285
1917
1897
1844
1966
1921
1905
1830
1484
1364
1782
1415
1412
1413
833
1857
1370
1890
1393
1262
1311
2008
1545
1896
1362
1862
1617
1864
608
1868
1689
1784
1806
1976
1870
1973
1993
1986
1998
1414
1858
1990
1841
1895
1779
1850
2001
1590
1902
1487
943
1808
782
1546
2004
1656
154
1661
1688
1731
1466
1927
1438
1387
1576
1793
1797
1979
1903
1638
1940
1911
1301
1669
1409
1578
1887
1860
1619
1939
1625
1941
1374
1766
1407

+ 11
- 0
day1/1/scala/main.scala View File

@ -0,0 +1,11 @@
import scala.io.Source
object Main extends App {
val input = Source.stdin.getLines().toList.map(_.toInt)
for ((x, i) <- input.zipWithIndex)
for (y <- input.drop(i))
if (x + y == 2020)
println(x * y)
}

+ 66
- 0
day1/2/c/main.c View File

@ -0,0 +1,66 @@
#include <stdio.h>
#include <stdlib.h>
size_t getNums(long int**, FILE*);
int main(int argc, char *argv[]) {
long int *nums = NULL;
size_t numsLen = getNums(&nums, stdin);
for (size_t i = 0; i < numsLen; i++)
for (size_t j = i; j < numsLen; j++)
for (size_t k = j; k < numsLen; k++)
if (nums[i] + nums[j] + nums[k] == 2020)
printf("%ld\n", nums[i] * nums[j] * nums[k]);
free(nums);
return EXIT_SUCCESS;
}
/**
* Precondition: stream is the input stream
*
* Postcondition: nums is an array of ints read from stream
* return the length of the nums array
*
*/
size_t getNums(long int **nums, FILE *stream) {
// nums should be NULL if it isn't already
if ((*nums) != NULL) {
free(*nums);
*nums = NULL;
}
size_t resLen = 0;
size_t resCapacity = 10;
*nums = (long int*) malloc(resCapacity * sizeof(long int));
if ((*nums) == NULL) {
perror("Failed to allocate nums\n");
exit(EXIT_FAILURE);
}
long int input = 0;
while (scanf("%ld", &input) > 0) {
// Double capacity of the result array
if (resCapacity == resLen) {
resCapacity *= 2;
long int *temp = (long int*) malloc(resCapacity * sizeof(long int));
if (nums == NULL) {
perror("Failed to allocate temp\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < resLen; i++) {
temp[i] = (*nums)[i];
}
free(*nums);
*nums = temp;
}
(*nums)[resLen++] = input;
}
return resLen;
}

+ 200
- 0
day1/2/input View File

@ -0,0 +1,200 @@
1918
1869
1821
1852
1981
1987
1989
1597
1634
1537
1991
1603
1543
1949
1879
1800
1761
2010
1810
1458
1755
1938
1500
2005
818
1493
1618
1923
1283
1952
1373
1605
1230
1321
1873
1853
1886
1372
1913
1871
159
733
1400
1880
1462
2007
1801
1982
1728
1640
1442
1525
1953
1791
1906
1946
1641
2009
1627
1935
1936
1908
1826
1291
1978
1884
1799
1464
1538
1365
1678
1872
1964
1863
1388
1912
1928
1471
1632
1336
1396
1948
1974
1331
1290
1992
1096
1889
1874
1341
757
1351
1876
1674
2002
1504
1865
1469
1792
1920
1925
1898
1899
1435
405
1488
1319
1322
1749
1867
1957
1944
1727
1293
1739
1285
1917
1897
1844
1966
1921
1905
1830
1484
1364
1782
1415
1412
1413
833
1857
1370
1890
1393
1262
1311
2008
1545
1896
1362
1862
1617
1864
608
1868
1689
1784
1806
1976
1870
1973
1993
1986
1998
1414
1858
1990
1841
1895
1779
1850
2001
1590
1902
1487
943
1808
782
1546
2004
1656
154
1661
1688
1731
1466
1927
1438
1387
1576
1793
1797
1979
1903
1638
1940
1911
1301
1669
1409
1578
1887
1860
1619
1939
1625
1941
1374
1766
1407

+ 11
- 0
day1/2/scala/main.scala View File

@ -0,0 +1,11 @@
import scala.io.Source
object Main extends App {
val input = Source.stdin.getLines().toList.map(_.toInt)
for ((x, i) <- input.zipWithIndex)
for ((y, j) <- input.zipWithIndex.drop(i))
for (z <- input.drop(j))
if (x + y + z == 2020)
println(x * y * z)
}

+ 27
- 0
day2/1/c/main.c View File

@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int lower = 0, upper = 0;
char letter = '\0';
char *password = NULL;
int count = 0;
while (scanf("%d-%d %c: %ms", &lower, &upper, &letter, &password) > 0) {
int letterCount = 0;
for (char *c = password; *c != '\0'; c++)
if (*c == letter)
letterCount++;
if (letterCount >= lower && letterCount <= upper)
count++;
free(password);
}
printf("%d\n", count);
return EXIT_SUCCESS;
}

+ 1000
- 0
day2/1/input
File diff suppressed because it is too large
View File


+ 17
- 0
day2/1/scala/main.scala View File

@ -0,0 +1,17 @@
import scala.io.Source
object Main extends App {
val input = Source.stdin.getLines()
val pattern = raw"(\d+)-(\d+) ([A-z]): (\w+)".r
println(
input.count(_ match {
case pattern(lower, upper, letter, password) =>
val c = password.count(_ == letter.head)
c >= lower.toInt && c <= upper.toInt
case _ => false
})
)
}

+ 22
- 0
day2/2/c/main.c View File

@ -0,0 +1,22 @@
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int firstPos = 0, secondPos = 0;
char letter = '\0';
char *password = NULL;
int count = 0;
while (scanf("%d-%d %c: %ms", &firstPos, &secondPos, &letter, &password) > 0) {
/* - 1 to account for 1 indexing*/
if ((password[firstPos - 1] == letter) != (password[secondPos - 1] == letter))
count++;
free(password);
}
printf("%d\n", count);
return EXIT_SUCCESS;
}

+ 1000
- 0
day2/2/input
File diff suppressed because it is too large
View File


+ 17
- 0
day2/2/scala/main.scala View File

@ -0,0 +1,17 @@
import scala.io.Source
object Main extends App {
val input = Source.stdin.getLines()
val pattern = raw"(\d+)-(\d+) ([A-z]): (\w+)".r
println(
input.count(_ match {
case pattern(firstPos, secondPos, letter, password) =>
(password(firstPos.toInt - 1) == letter.head) !=
(password(secondPos.toInt - 1) == letter.head)
case _ => false
})
)
}

Loading…
Cancel
Save