Browse Source

Initial commit

main
J. Sims 7 months ago
commit
191721ca7d
  1. 99
      2019/IntComp.c
  2. 54
      2019/day1/AoC1.c
  3. 100
      2019/day1/mass-input.txt
  4. 114
      2019/day2/AoC2.c
  5. 1
      2019/day2/main.txt
  6. 1
      2019/day2/test1.txt
  7. 1
      2019/day2/test2.txt
  8. 1
      2019/day2/test3.txt
  9. 1
      2019/day2/test4.txt
  10. 148
      2019/day3/AoC3-0.2.c
  11. 66
      2019/day3/AoC3.c
  12. BIN
      2019/day3/aoc
  13. 2
      2019/day3/directions0.txt
  14. 1
      2019/day3/directions1.txt
  15. 1
      2019/day3/directions2.txt
  16. 200
      2020/day1/input.asc
  17. 41
      2020/day1/p1.d
  18. 47
      2020/day1/p2.d
  19. 93
      2020/day10/input.asc
  20. 37
      2020/day10/p1.d
  21. 47
      2020/day10/p2.d
  22. 46
      2020/day10/p2Memoize.d
  23. 11
      2020/day10/test.asc
  24. 31
      2020/day10/test2.asc
  25. 94
      2020/day11/input.asc
  26. 100
      2020/day11/p1.d
  27. 141
      2020/day11/p2.d
  28. 10
      2020/day11/test.asc
  29. 774
      2020/day12/input.asc
  30. 98
      2020/day12/p1.d
  31. 104
      2020/day12/p2.d
  32. 5
      2020/day12/test.asc
  33. 2
      2020/day13/input.asc
  34. 44
      2020/day13/p1.d
  35. 54
      2020/day13/p2.d
  36. 35
      2020/day13/test.d
  37. 2
      2020/day13/test0.asc
  38. 2
      2020/day13/test1.asc
  39. 2
      2020/day13/test2.asc
  40. 2
      2020/day13/test3.asc
  41. 2
      2020/day13/test4.asc
  42. 2
      2020/day13/test5.asc
  43. BIN
      2020/day14/-
  44. 4
      2020/day14/ex.asc
  45. 4
      2020/day14/ex2.asc
  46. 569
      2020/day14/input.asc
  47. 75
      2020/day14/p1.d
  48. 210
      2020/day14/p2.d
  49. 0
      2020/day14/tags.TLsVgI
  50. 0
      2020/day14/tags.tJPrx5
  51. 0
      2020/day14/tags.txbRGH
  52. 13
      2020/day14/test.d
  53. 1
      2020/day15/ex.asc
  54. 1
      2020/day15/ex2.asc
  55. 1
      2020/day15/ex3.asc
  56. 1
      2020/day15/ex4.asc
  57. 1
      2020/day15/ex5.asc
  58. 1
      2020/day15/ex6.asc
  59. 1
      2020/day15/ex7.asc
  60. 1
      2020/day15/input.asc
  61. 47
      2020/day15/p1.d
  62. 55
      2020/day15/p2.d
  63. 12
      2020/day16/ex.asc
  64. 11
      2020/day16/ex2.asc
  65. 262
      2020/day16/input.asc
  66. 53
      2020/day16/p1.d
  67. 102
      2020/day16/p2.d
  68. 1000
      2020/day2/input.asc
  69. 39
      2020/day2/p1.d
  70. 49
      2020/day2/p2.d
  71. 11
      2020/day3/exinput.asc
  72. 323
      2020/day3/input.asc
  73. 36
      2020/day3/p1.d
  74. 50
      2020/day3/p2.d
  75. 1129
      2020/day4/input.asc
  76. 100
      2020/day4/p1.d
  77. 199
      2020/day4/p2.d
  78. 13
      2020/day4/test1.asc
  79. 26
      2020/day4/test2.asc
  80. 816
      2020/day5/input.asc
  81. 90
      2020/day5/p1.d
  82. 94
      2020/day5/p2.d
  83. 3
      2020/day5/test.asc
  84. 5
      2020/day5/test.d
  85. 2277
      2020/day6/input.asc
  86. 49
      2020/day6/p1.d
  87. 54
      2020/day6/p2.d
  88. 15
      2020/day6/test.asc
  89. 9
      2020/day6/test.d
  90. 594
      2020/day7/input.asc
  91. 69
      2020/day7/p1.d
  92. 9
      2020/day7/test.asc
  93. 10
      2020/day7/test.d
  94. 7
      2020/day7/test2.asc
  95. 591
      2020/day8/input.asc
  96. 54
      2020/day8/p1.d
  97. 66
      2020/day8/p2.d
  98. 9
      2020/day8/test.asc
  99. 13
      2020/day8/test.d
  100. 1000
      2020/day9/input.asc
  101. Some files were not shown because too many files have changed in this diff Show More

99
2019/IntComp.c

@ -0,0 +1,99 @@
/*
* IntComp.c - Advent of Code 2019 Day 2 Intcode Computer
*
* Copyright 2019 Jacob Sims <jtsims@pm.me>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
if (argc < 2)
{
fprintf(stderr, "Usage: ./IntComp filename [name verb]\n");
return 1;
}
char *filename = argv[1];
FILE *input = fopen(filename, "r");
if (input == NULL)
{
fprintf(stderr, "Could not open %s\n", filename);
return 2;
}
char *buf = NULL;
size_t n = 0;
int memory[500];
int address = 0;
// Read the program into memory
while (getdelim(&buf, &n, ',', input) != -1)
{
memory[address] = strtod(buf, NULL);
address++;
}
fclose(input);
// Add noun and verb values, if necessary
if (argc == 4 && !strcmp(filename, "main.txt"))
{
int noun = strtod(argv[2], NULL);
int verb = strtod(argv[3], NULL);
opcode[1] = noun;
opcode[2] = verb;
}
// Run the program
for (int i = 0; memory[i] != 99; i += 4)
{
int param1 = memory[memory[i + 1]];
int param2 = memory[memory[i + 2]];
int storeparam = memory[i + 3];
switch (memory[i])
{
case 1:
memory[storeparam] = param1 + param2;
break;
case 2:
memory[storeparam] = param1 * param2;
break;
default:
fprintf(stderr, "Unknown instruction\n");
return 3;
}
}
// Printf the output
printf("Address 0: %i\n", memory[0]);
return 0;
}

54
2019/day1/AoC1.c

@ -0,0 +1,54 @@
/*
* AoC1.c - Advent of Code 2019 Day 1
*
* Given a certain mass, the amount of fuel required is calculated by
* floor dividing (dividing and rounding down) the mass by 3 then
* subtracting two. The masses in question are in 'mass-input.txt' in
* newline-divided format. The result is all of these masses' fuel
* requirements added together.
*
* Provides the correct answer.
*
* Jacob Sims (jtsims@pm.me) 2019-12-03
*
*/
#include <stdio.h>
#include <stdlib.h>
int calculateFuel(int to_lift);
int main(int argc, char **argv)
{
FILE *input = fopen("mass-input.txt", "r");
if (input == NULL)
{
fprintf(stderr, "Could not open input file\n");
return 1;
}
char *buf = NULL;
size_t n = 0;
int reqfuel;
int mass;
while (getline(&buf, &n, input) != -1)
{
mass = strtod(buf, NULL);
reqfuel += calculateFuel(mass);
}
fclose(input);
free(buf);
printf("Total fuel: %i\n", reqfuel);
return 0;
}
int calculateFuel(int to_lift)
{
int fuel = (to_lift / 3) - 2;
return fuel <= 0 ? 0 : fuel + calculateFuel(fuel);
}

100
2019/day1/mass-input.txt

@ -0,0 +1,100 @@
83453
89672
81336
74923
71474
117060
55483
116329
123515
99383
80314
108221
128335
72860
139235
127843
140120
63561
68854
109062
146211
59096
123085
105763
127657
142212
111007
100166
63641
59010
108575
93619
144095
74561
95059
145318
81404
96567
91799
92987
107137
87678
126842
85594
116330
104714
128117
132641
75602
90747
69038
67322
146147
147535
83266
85908
124634
51681
104430
56202
68631
69970
116985
140878
125357
126229
66379
103213
108210
73855
130992
113363
82298
111468
110751
52272
103661
122262
114363
80881
65183
125291
100119
56995
101634
55467
136284
107433
95647
71462
133265
104554
62499
61347
68675
123501
113954
135798
80825
128235

114
2019/day2/AoC2.c

@ -0,0 +1,114 @@
/*
* AoC2.c - Advent of Code 2019 Day 2 Intcode Computer
*
* Copyright 2019 Jacob Sims <jtsims@pm.me>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int compute(int *program, int noun, int verb);
int main(int argc, char **argv)
{
if (argc < 2)
{
fprintf(stderr, "Usage: ./aoc filename\n");
exit(1);
}
char *filename = argv[1];
FILE *input = fopen(filename, "r");
if (input == NULL)
{
fprintf(stderr, "Could not open %s\n", filename);
exit(2);
}
char *buf;
size_t n;
int address;
int memory[500];
for (int verb = 0; verb < 100; verb++)
{
for (int noun = 0; noun < 100; noun++)
{
buf = NULL;
n = 0;
address = 0;
fseek(input, 0, SEEK_SET);
while (getdelim(&buf, &n, ',', input) != -1)
{
memory[address] = strtod(buf, NULL);
address++;
}
if (compute(memory, noun, verb) == 19690720)
{
printf("Noun: %i\nVerb: %i\n", noun, verb);
break;
}
}
}
fclose(input);
free(buf);
exit(0);
}
int compute(int *program, int noun, int verb)
{
program[1] = noun;
program[2] = verb;
// Run the program
for (int i = 0; program[i] != 99; i += 4)
{
int param1 = program[program[i + 1]];
int param2 = program[program[i + 2]];
int storeparam = program[i + 3];
switch (program[i])
{
case 1:
program[storeparam] = param1 + param2;
break;
case 2:
program[storeparam] = param1 * param2;
break;
default:
printf("Unknown instruction\n");
return 1;
}
}
return program[0];
}

1
2019/day2/main.txt

@ -0,0 +1 @@
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,13,1,19,1,6,19,23,2,23,6,27,1,5,27,31,1,10,31,35,2,6,35,39,1,39,13,43,1,43,9,47,2,47,10,51,1,5,51,55,1,55,10,59,2,59,6,63,2,6,63,67,1,5,67,71,2,9,71,75,1,75,6,79,1,6,79,83,2,83,9,87,2,87,13,91,1,10,91,95,1,95,13,99,2,13,99,103,1,103,10,107,2,107,10,111,1,111,9,115,1,115,2,119,1,9,119,0,99,2,0,14,0

1
2019/day2/test1.txt

@ -0,0 +1 @@
1,0,0,0,99

1
2019/day2/test2.txt

@ -0,0 +1 @@
2,3,0,3,99

1
2019/day2/test3.txt

@ -0,0 +1 @@
2,4,4,5,99,0

1
2019/day2/test4.txt

@ -0,0 +1 @@
1,1,1,4,99,5,6,0,99

148
2019/day3/AoC3-0.2.c

@ -0,0 +1,148 @@
/*
* AoC3.c - Advent of Code 2019 Day 3
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct step
{
char direction;
int distance;
};
struct coord
{
int x;
int y;
};
#define TOTAL_STEPS 301
#define TOTAL_COORDS 131072
void readIn(struct step *to_fill, FILE *input);
/* returns length of line */
int calculatePath(struct step *line, struct coord *coordlist, size_t size);
int main(int argc, char **argv)
{
/* open files, create arrays, and read in */
FILE *input1 = fopen("directions1.txt", "r");
if (input1 == NULL)
{
fprintf(stderr, "Cannot open first input file\n");
exit(1);
}
FILE *input2 = fopen("directions2.txt", "r");
if (input2 == NULL)
{
fprintf(stderr, "Cannot open second input file\n");
exit(1);
}
struct step line1[TOTAL_STEPS];
struct step line2[TOTAL_STEPS];
readIn(line1, input1);
readIn(line2, input2);
fclose(input1);
fclose(input2);
struct coord *coordlist1 = malloc(sizeof(struct coord) * TOTAL_COORDS);
struct coord *coordlist2 = malloc(sizeof(struct coord) * TOTAL_COORDS);
if (coordlist1 == NULL || coordlist2 == NULL)
{
fprintf(stderr, "Insufficient memory for allocation\n");
exit(2);
}
/*int length1 = */calculatePath(line1, coordlist1, TOTAL_COORDS);
/*int length2 = */calculatePath(line2, coordlist2, TOTAL_COORDS);
for (int i = 0; i < 27603; i++)
{
printf("List 1 step %d x: %d\n", i, coordlist1[i].x);
printf("List 1 step %d y: %d\n", i, coordlist1[i].y);
printf("List 2 step %d x: %d\n", i, coordlist2[i].x);
printf("List 2 step %d y: %d\n", i, coordlist2[i].y);
}
free(coordlist1);
free(coordlist2);
return 0;
}
/* reads in a coordinate CSV file */
void readIn(struct step *to_fill, FILE *input)
{
int index = 0;
size_t n = 0;
char *buf = NULL;
struct step *ptr = to_fill;
// iterate over CSV file
while (getdelim(&buf, &n, ',', input) != -1)
{
// interprete a value by dividing it into direction and distance
sscanf(buf, "%[DLRU]%d", &ptr[index].direction, &ptr[index].distance);
index++;
}
free(buf);
}
/* compute and store the entire path of one line */
int calculatePath(struct step *line, struct coord *coordlist, size_t size)
{
// tracker variables
int x = 0;
int y = 0;
int preceding_steps = 0;
int distance;
// iterate over each direction-distance pair
for (int i = 0; i < TOTAL_STEPS; i++)
{
distance = line[i].distance;
if ((distance + preceding_steps) >= size)
{
size *= 2;
coordlist = (struct coord *)realloc(coordlist, sizeof(struct coord) * size);
}
// store every coordinate following the path of this straight line
for (int k = 0; k < distance; k++)
{
coordlist[k + preceding_steps].x = x;
coordlist[k + preceding_steps].y = y;
// check direction and iterate appropriate value
switch (line[i].direction)
{
case 'R':
x++;
break;
case 'L':
x--;
break;
case 'U':
y++;
break;
case 'D':
y--;
break;
default:
fprintf(stderr, "Path computation error; no valid direction\n");
exit(2);
}
}
preceding_steps += distance;
}
return preceding_steps;
}

66
2019/day3/AoC3.c

@ -0,0 +1,66 @@
/*
* AoC3.c - Advent of Code 2019 Day 3
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct step
{
char direction;
int distance;
};
// total steps: 301 for each line
int main(int argc, char **argv)
{
FILE *input = fopen("directions.txt", "r");
if (input == NULL)
{
fprintf(stderr, "Cannot open input file\n");
exit(1);
}
size_t n = 0;
char *buf = NULL;
int total_steps = 500;
struct step line1[total_steps];
struct step line2[total_steps];
struct step *ptr = line1;
int index = 0;
while(getdelim(&buf, &n, ',', input) != -1)
{
sscanf(buf, "%[DLRU]%d", &ptr[index].direction, &ptr[index].distance);
for (int i = 0; i < n; i++)
{
if (buf[i] == '\n')
{
buf[i] = ' ';
ptr = line2;
index = 0;
sscanf(buf, "%[DLRU]%d", &ptr[index].direction, &ptr[index].distance);
break;
}
buf[i] = ' ';
}
index++;
}
fclose(input);
free(buf);
for (int i = 0; i < 301; i++)
{
printf("Line 1 direction: %c\n", (char) line1[i].direction);
printf("Line 1 distance: %i\n", line1[i].distance);
printf("Line 2 direction: %c\n", (char) line2[i].direction);
printf("Line 2 distance: %i\n", line2[i].distance);
}
return 0;
}

BIN
2019/day3/aoc

Binary file not shown.

2
2019/day3/directions0.txt

@ -0,0 +1,2 @@
R990,U475,L435,D978,L801,D835,L377,D836,L157,D84,R329,D342,R931,D522,L724,U891,L508,U274,L146,U844,R686,D441,R192,U992,L781,D119,R436,D286,R787,D85,L801,U417,R619,D710,R42,U261,R296,U697,L354,D843,R613,U880,R789,D134,R636,D738,L939,D459,L338,D905,R811,D950,L44,U992,R845,U771,L563,D76,L69,U839,L57,D311,L615,D931,L437,D201,L879,D1,R978,U415,R548,D398,L560,D112,L894,D668,L708,D104,R622,D768,R901,D746,L793,D26,R357,U216,L216,D33,L653,U782,R989,U678,L7,D649,R860,D281,L988,U362,L525,U652,R620,D376,L983,U759,R828,D669,L297,U207,R68,U77,R255,U269,L661,U310,L309,D490,L55,U471,R260,D912,R691,D62,L63,D581,L289,D366,L862,D360,L485,U946,R937,D470,L792,D614,R936,D963,R611,D151,R908,D195,R615,U768,L166,D314,R640,U47,L161,U872,R50,U694,L917,D149,L92,U244,L337,U479,R755,U746,L196,D759,L936,U61,L744,D774,R53,U439,L185,D504,R769,D696,L285,D396,R791,U21,L35,D877,L9,U398,R447,U101,R590,U862,L351,D210,L935,U938,R131,U758,R99,U192,L20,U142,L946,D981,R998,U214,R174,U710,L719,D879,L411,U839,L381,U924,L221,D397,R380,U715,R139,D367,R253,D973,L9,U624,L426,D885,R200,U940,R214,D75,R717,D2,R578,U161,R421,U326,L561,U311,L701,U259,R836,D920,R35,D432,R610,D63,R664,D39,L119,D47,L605,D228,L364,D14,L226,D365,R796,D233,R476,U145,L926,D907,R681,U267,R844,U735,L948,U344,L629,U31,L383,U694,L666,U158,R841,D27,L150,D950,L335,U275,L184,D157,R504,D602,R605,D185,L215,D420,R700,U809,L139,D937,L248,U693,L56,U92,L914,U743,R445,U417,L504,U23,R332,U865,R747,D553,R595,U845,R693,U915,R81
L1004,U406,L974,D745,R504,D705,R430,D726,R839,D550,L913,D584,R109,U148,L866,U664,R341,U449,L626,D492,R716,U596,L977,D987,L47,U612,L478,U928,L66,D752,R665,U415,R543,U887,R315,D866,R227,D615,R478,U180,R255,D316,L955,U657,R752,U561,R786,U7,R918,D755,R506,U131,L875,D849,R823,D755,L604,U944,R186,D326,L172,U993,L259,D765,R427,D193,R663,U470,L294,D437,R645,U10,L926,D814,L536,D598,R886,D290,L226,U156,R754,D105,L604,D136,L883,U87,R839,D807,R724,U184,L746,D79,R474,U186,R727,U9,L69,U565,R459,D852,R61,U370,L890,D439,L431,U846,R460,U358,R51,D407,R55,U179,L385,D652,R193,D52,L569,U980,L185,U813,R636,D275,L585,U590,R215,U947,R851,D127,L249,U954,L884,D235,R3,U735,R994,D883,L386,D506,L963,D751,L989,U733,L221,U890,L711,D32,L74,U437,L700,D977,L49,U478,R438,D27,R945,D670,L230,U863,L616,U461,R267,D25,L646,D681,R426,D918,L791,U712,L730,U715,L67,U359,R915,D524,L722,U374,L582,U529,L802,D865,L596,D5,R323,U235,R405,D62,R304,U996,L939,U420,L62,D299,R802,D803,L376,U430,L810,D334,L67,U395,L818,U953,L817,D411,L225,U383,R247,D234,L430,U315,L418,U254,L964,D372,R979,D301,R577,U440,R924,D220,L121,D785,L609,U20,R861,U288,R388,D410,L278,D748,L800,U755,L919,D985,L785,U676,R916,D528,L507,D469,L582,D8,L900,U512,L764,D124,L10,U567,L379,D231,R841,D244,R479,U145,L769,D845,R651,U712,L920,U791,R95,D958,L608,D755,R967,U855,R563,D921,L37,U699,L944,U718,R959,D195,L922,U726,R378,U258,R340,D62,L555,D135,L690,U269,L273,D851,L60,D851,R1,D315,R117,D855,L275,D288,R25,U503,R569,D596,L823,U687,L450

1
2019/day3/directions1.txt

@ -0,0 +1 @@
R990,U475,L435,D978,L801,D835,L377,D836,L157,D84,R329,D342,R931,D522,L724,U891,L508,U274,L146,U844,R686,D441,R192,U992,L781,D119,R436,D286,R787,D85,L801,U417,R619,D710,R42,U261,R296,U697,L354,D843,R613,U880,R789,D134,R636,D738,L939,D459,L338,D905,R811,D950,L44,U992,R845,U771,L563,D76,L69,U839,L57,D311,L615,D931,L437,D201,L879,D1,R978,U415,R548,D398,L560,D112,L894,D668,L708,D104,R622,D768,R901,D746,L793,D26,R357,U216,L216,D33,L653,U782,R989,U678,L7,D649,R860,D281,L988,U362,L525,U652,R620,D376,L983,U759,R828,D669,L297,U207,R68,U77,R255,U269,L661,U310,L309,D490,L55,U471,R260,D912,R691,D62,L63,D581,L289,D366,L862,D360,L485,U946,R937,D470,L792,D614,R936,D963,R611,D151,R908,D195,R615,U768,L166,D314,R640,U47,L161,U872,R50,U694,L917,D149,L92,U244,L337,U479,R755,U746,L196,D759,L936,U61,L744,D774,R53,U439,L185,D504,R769,D696,L285,D396,R791,U21,L35,D877,L9,U398,R447,U101,R590,U862,L351,D210,L935,U938,R131,U758,R99,U192,L20,U142,L946,D981,R998,U214,R174,U710,L719,D879,L411,U839,L381,U924,L221,D397,R380,U715,R139,D367,R253,D973,L9,U624,L426,D885,R200,U940,R214,D75,R717,D2,R578,U161,R421,U326,L561,U311,L701,U259,R836,D920,R35,D432,R610,D63,R664,D39,L119,D47,L605,D228,L364,D14,L226,D365,R796,D233,R476,U145,L926,D907,R681,U267,R844,U735,L948,U344,L629,U31,L383,U694,L666,U158,R841,D27,L150,D950,L335,U275,L184,D157,R504,D602,R605,D185,L215,D420,R700,U809,L139,D937,L248,U693,L56,U92,L914,U743,R445,U417,L504,U23,R332,U865,R747,D553,R595,U845,R693,U915,R81

1
2019/day3/directions2.txt

@ -0,0 +1 @@
L1004,U406,L974,D745,R504,D705,R430,D726,R839,D550,L913,D584,R109,U148,L866,U664,R341,U449,L626,D492,R716,U596,L977,D987,L47,U612,L478,U928,L66,D752,R665,U415,R543,U887,R315,D866,R227,D615,R478,U180,R255,D316,L955,U657,R752,U561,R786,U7,R918,D755,R506,U131,L875,D849,R823,D755,L604,U944,R186,D326,L172,U993,L259,D765,R427,D193,R663,U470,L294,D437,R645,U10,L926,D814,L536,D598,R886,D290,L226,U156,R754,D105,L604,D136,L883,U87,R839,D807,R724,U184,L746,D79,R474,U186,R727,U9,L69,U565,R459,D852,R61,U370,L890,D439,L431,U846,R460,U358,R51,D407,R55,U179,L385,D652,R193,D52,L569,U980,L185,U813,R636,D275,L585,U590,R215,U947,R851,D127,L249,U954,L884,D235,R3,U735,R994,D883,L386,D506,L963,D751,L989,U733,L221,U890,L711,D32,L74,U437,L700,D977,L49,U478,R438,D27,R945,D670,L230,U863,L616,U461,R267,D25,L646,D681,R426,D918,L791,U712,L730,U715,L67,U359,R915,D524,L722,U374,L582,U529,L802,D865,L596,D5,R323,U235,R405,D62,R304,U996,L939,U420,L62,D299,R802,D803,L376,U430,L810,D334,L67,U395,L818,U953,L817,D411,L225,U383,R247,D234,L430,U315,L418,U254,L964,D372,R979,D301,R577,U440,R924,D220,L121,D785,L609,U20,R861,U288,R388,D410,L278,D748,L800,U755,L919,D985,L785,U676,R916,D528,L507,D469,L582,D8,L900,U512,L764,D124,L10,U567,L379,D231,R841,D244,R479,U145,L769,D845,R651,U712,L920,U791,R95,D958,L608,D755,R967,U855,R563,D921,L37,U699,L944,U718,R959,D195,L922,U726,R378,U258,R340,D62,L555,D135,L690,U269,L273,D851,L60,D851,R1,D315,R117,D855,L275,D288,R25,U503,R569,D596,L823,U687,L450

200
2020/day1/input.asc

@ -0,0 +1,200 @@
261
1773
1839
1551
1781
1276
1372
1668
1823
1870
1672
1821
1327
1902
1949
1389
1720
1437
1716
1360
1893
1410
1881
1927
1639
1514
1753
1625
1249
1696
1698
1699
2004
1742
1903
473
1948
1830
1973
2005
1468
1824
1809
1493
2009
1848
1306
1519
1618
1905
1402
1705
1910
1609
1571
1557
1420
608
1471
1383
1442
1447
1985
1486
1629
1450
1767
1407
1626
1623
1467
1224
1269
1325
1674
1945
1733
1913
1451
1853
1875
405
1500
1634
1570
1868
1510
1069
1296
1852
1287
1274
832
1373
1142
1838
1854
1480
1628
1632
1597
1761
1717
1684
1956
1351
1622
1941
1704
1926
1873
1393
1850
1898
1960
1673
1736
1901
1806
1768
1670
1989
1214
1851
1715
1461
1277
951
1482
1464
1883
1976
1602
1606
1258
1801
1593
1332
1386
1309
1388
1762
1533
1805
1462
375
1555
1357
1578
1552
1473
1834
1262
1466
1925
1955
1575
1975
1964
1440
1667
1922
1454
1813
1968
1836
1982
1326
1811
900
1588
1529
1997
1345
1859
1458
1764
1509
1397
1237
1627
1564
1814
1842
1679
1289
1957
1819
801
1350
1841
1803
1718
1966
1272
1636
1352
1496
1455
1488

41
2020/day1/p1.d

@ -0,0 +1,41 @@
import std.conv;
import std.file;
import std.stdio;
import std.string;
int main(string[] args) {
if (args.length < 2) {
writeln("Usage: ./day1 input.file");
return 1;
}
string fileName = args[1];
if (!exists(fileName)) {
writeln(fileName, " does not exist.");
return 2;
}
string[] entries = readText(args[1]).splitLines();
int sum;
foreach (a; entries) {
int x = a.to!int;
foreach (b; entries) {
int y = b.to!int;
if (x == y)
continue;
else
sum = x + y;
if (sum == 2020) {
writeln("Solution: ", a, " * ", b, " = ", x * y);
return 0;
}
}
}
return 3;
}

47
2020/day1/p2.d

@ -0,0 +1,47 @@
import std.conv;
import std.file;
import std.stdio;
import std.string;
int main(string[] args) {
if (args.length < 2) {
writeln("Usage: ./day1 input.file");
return 1;
}
string fileName = args[1];
if (!exists(fileName)) {
writeln(fileName, " does not exist.");
return 2;
}
string[] entries = readText(args[1]).splitLines();
int sum;
foreach (a; entries) {
int x = a.to!int;
foreach (b; entries) {
int y = b.to!int;
if (x == y)
continue;
foreach (c; entries) {
int z = c.to!int;
if (y == z || x == z)
continue;
else
sum = x + y + z;
if (sum == 2020) {
writeln("Solution: ", a, " * ", b, " * ", c, " = ", x * y * z);
return 0;
}
}
}
}
return 3;
}

93
2020/day10/input.asc

@ -0,0 +1,93 @@
97
62
23
32
51
19
98
26
90
134
73
151
116
76
6
94
113
127
119
44
115
50
143
150
86
91
36
104
131
101
38
66
46
96
54
70
8
30
1
108
69
139
24
29
77
124
107
14
137
16
140
80
68
25
31
59
45
126
148
67
13
125
53
57
41
47
35
145
120
12
37
5
110
138
130
2
63
83
22
79
52
7
95
58
149
123
89
109
15
144
114
9
78

37
2020/day10/p1.d

@ -0,0 +1,37 @@
import std.algorithm.sorting : sort;
import std.array : split;
import std.conv : to;
import std.file : exists, readText;
import std.stdio : stderr, writeln;
import std.string : splitLines;
int main(string[] args) {
if (args.length != 2) {
stderr.writeln("Usage: ./p1 file.name");
return 1;
}
string fileName = args[1];
if (!exists(fileName)) {
stderr.writeln(fileName, " does not exist");
return 1;
}
string[] fileLines = fileName.readText.splitLines();
ulong[] nums;
foreach (line; fileLines)
nums ~= line.to!ulong;
nums.sort();
nums ~= nums[$-1] + 3;
ulong[ulong] numDifferences;
ulong last;
foreach (val; nums) {
numDifferences[val - last]++;
last = val;
}
writeln("Result: ", numDifferences[1]*numDifferences[3]);
return 0;
}

47
2020/day10/p2.d

@ -0,0 +1,47 @@
/* The runtime for this is impractical - use the memoized version */
import std.algorithm.searching : canFind;
import std.algorithm.sorting : sort;
import std.array : split;
import std.conv : to;
import std.file : exists, readText;
import std.stdio : stderr, writeln;
import std.string : splitLines;
int main(string[] args) {
if (args.length != 2) {
stderr.writeln("Usage: ./p2 file.name");
return 1;
}
string fileName = args[1];
if (!exists(fileName)) {
stderr.writeln(fileName, " does not exist");
return 1;
}
string[] fileLines = fileName.readText.splitLines();
ulong[] nums = [0];
foreach (line; fileLines)
nums ~= line.to!ulong;
nums.sort();
nums ~= nums[$-1] + 3;
ulong numPaths = createArrangements(nums, nums[0]);
writeln("Result: ", numPaths);
return 0;
}
ulong createArrangements(ulong[] nums, ulong val) {
ulong toRet;
foreach (mod; 1 .. 4) {
ulong toFind = val+mod;
if (toFind == nums[$-1])
return toRet + 1;
else if (nums.canFind(toFind))
toRet += createArrangements(nums, toFind);
}
return toRet;
}

46
2020/day10/p2Memoize.d

@ -0,0 +1,46 @@
import std.algorithm.searching : canFind;
import std.algorithm.sorting : sort;
import std.array : split;
import std.conv : to;
import std.file : exists, readText;
import std.functional : memoize;
import std.stdio : stderr, writeln;
import std.string : splitLines;
int main(string[] args) {
if (args.length != 2) {
stderr.writeln("Usage: ./p2 file.name");
return 1;
}
string fileName = args[1];
if (!exists(fileName)) {
stderr.writeln(fileName, " does not exist");
return 1;
}
string[] fileLines = fileName.readText.splitLines();
ulong[] nums = [0];
foreach (line; fileLines)
nums ~= line.to!ulong;
nums.sort();
nums ~= nums[$-1] + 3;
ulong numPaths = createArrangements(nums, nums[0]);
writeln("Result: ", numPaths);
return 0;
}
ulong createArrangements(ulong[] nums, ulong val) {
ulong toRet;
foreach (mod; 1 .. 4) {
ulong toFind = val+mod;
if (toFind == nums[$-1])
return toRet + 1;
else if (nums.canFind(toFind))
toRet += memoize!createArrangements(nums, toFind);
}
return toRet;
}

11
2020/day10/test.asc

@ -0,0 +1,11 @@
16
10
15
5
1
11
7
19
6
12
4

31
2020/day10/test2.asc

@ -0,0 +1,31 @@
28
33
18
42
31
14
46
20
48
47
24
23
49
45
19
38
39
11
1
32
25
35
8
17
7
9
4
2
34
10
3

94
2020/day11/input.asc

@ -0,0 +1,94 @@
LLLLLLLLLLL..LLLLLL.LLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLL.LLLLLL.LL.LLLLLLLL..LLLLL
LLLLL..L.LLL.L.LLLL.LLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLL.LLLLLL..LLLL
LLLLLL.LLLLL.LLLLLLLLLLLLLLLLLLLLLLLL.LLL.LLLLLLLLL.LLLLLL.LLLLLLL.LLLLLL.LLLLL.LLLLLL.LLLLL
LLLLLLLLLLLL.LLLLLL.LLLLL.LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLL
LLLLLL.LLLLLLLLLLLL.LLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLL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LLLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLL.LLLLL.LLLLLL.LLLLL
LLLLLLLLLLLL.LLLLLLLLLLLL.LLLLLLLL.LLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLLLLLLLL.LLLLL.LLLLLL.LLLLL
LLLLLLLLLLLLLLLLLLL.LLLLL.LLLLLLLL.LLLLLL.LLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLLLLLLL.LLLLLL.LLLLL
LLLLLL.LLLLL.LLLLLL.LLLLL.LLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLL...LLLLL.L.LLLL.LLLLLLLLLLLL..LLLL
LLLLLL.LL.LL.LLLLLLLLLLLLLLLLLL.LL.LLLLLL.LLLLLLLL..LLLLLL.LLLLLLL.LLLLLL.LLLLLLLLLLLL.LLLLL
LLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLL.LLLLLL.LLLLL
LLLLLL.LLLLL.LLLLLL.LLLLLLLLLLLLLL.LLLLLL.LL.LLLLLL.LLLLLLLLLL.LLL.LLLLLL..LLLL.LLLLLLLLLLLL

100
2020/day11/p1.d

@ -0,0 +1,100 @@
/*
* If a seat is empty (L) and there are no occupied seats adjacent to it, the seat becomes occupied.
* If a seat is occupied (#) and four or more seats adjacent to it are also occupied, the seat becomes empty.
* Otherwise, the seat's state does not change.
*/
import std.file : exists, readText;
import std.stdio: stderr, writeln;
import std.string : splitLines;
int main(string[] args) {
if (args.length != 2) {
stderr.writeln("Usage: ./p1 file.name");
return 1;
}
string fileName = args[1];
if (!exists(fileName)) {
stderr.writeln(fileName, " does not exist");
return 2;
}
string[] fileText = fileName.readText.splitLines();
char[][] map;
foreach (i, line; fileText) {
char[] toAdd;
foreach (j, c; line) {
toAdd ~= c;
}
map ~= toAdd;
}
while (iterGen(map)) {}
ulong counter;
foreach (line; map) {
foreach (c; line) {
if (c == '#')
counter++;
}
}
writeln("Occupied seats: ", counter);
return 0;
}
bool iterGen(ref char[][] baseMap) {
bool changed;
char[][] map;
foreach (i, row; baseMap) {
char[] toAdd;
foreach (j, c; row) {
if (c == '.') {
toAdd ~= '.';
continue;
}
ubyte neighbors;
if (i > 0) {
if (j > 0)
neighbors += baseMap[i-1][j-1] == '#' ? 1 : 0;
neighbors += baseMap[i-1][j] == '#' ? 1 : 0;
if (j < row.length-1)
neighbors += baseMap[i-1][j+1] == '#' ? 1 : 0;
}
if (i < baseMap.length-1) {
if (j > 0)
neighbors += baseMap[i+1][j-1] == '#' ? 1 : 0;
neighbors += baseMap[i+1][j] == '#' ? 1 : 0;
if (j < row.length-1)
neighbors += baseMap[i+1][j+1] == '#' ? 1 : 0;
}
if (j > 0)
neighbors += baseMap[i][j-1] == '#' ? 1 : 0;
if (j < row.length-1)
neighbors += baseMap[i][j+1] == '#' ? 1 : 0;
if (c == 'L' && neighbors == 0) {
toAdd ~= '#';
changed = true;
} else if (c == '#' && neighbors >= 4) {
toAdd ~= 'L';
changed = true;
} else {
toAdd ~= c;
}
}
map ~= toAdd;
}
baseMap = map;
return changed;
}

141
2020/day11/p2.d

@ -0,0 +1,141 @@
/*
* If a seat is empty (L) and there are no occupied seats adjacent to it, the seat becomes occupied.
* If a seat is occupied (#) and four or more seats adjacent to it are also occupied, the seat becomes empty.
* Otherwise, the seat's state does not change.
*/
import std.algorithm.comparison : min;
import std.file : exists, readText;
import std.stdio: stderr, writeln;
import std.string : splitLines;
int main(string[] args) {
if (args.length != 2) {
stderr.writeln("Usage: ./p2 file.name");
return 1;
}
string fileName = args[1];
if (!exists(fileName)) {
stderr.writeln(fileName, " does not exist");
return 2;
}
string[] fileText = fileName.readText.splitLines();
char[][] map;
foreach (i, line; fileText) {
char[] toAdd;
foreach (j, c; line) {
toAdd ~= c;
}
map ~= toAdd;
}
while (iterGen(map)) {}
ulong counter;
foreach (line; map) {
writeln(line);
foreach (c; line) {
if (c == '#')
counter++;
}
}
writeln("Occupied seats: ", counter);
return 0;
}
bool iterGen(ref char[][] baseMap) {
bool changed;
char[][] map;
foreach (i, row; baseMap) {
char[] toAdd;
foreach (j, c; row) {
if (c == '.') {