1
0
Fork 0
mirror of https://github.com/HenkKalkwater/aoc-2020 synced 2024-11-22 11:05:18 +00:00

Added day 1

This commit is contained in:
Chris Josten 2020-12-01 12:28:18 +01:00
parent 51afb9d03e
commit c6f7df0bf3
Signed by: chris
GPG key ID: 1795A594046530AB
4 changed files with 308 additions and 4 deletions

15
.gitignore vendored Normal file
View file

@ -0,0 +1,15 @@
.dub
docs.json
__dummy.html
docs/
/aoc-2020
aoc-2020.so
aoc-2020.dylib
aoc-2020.dll
aoc-2020.a
aoc-2020.lib
aoc-2020-test-*
*.exe
*.o
*.obj
*.lst

200
in/1.txt Normal file
View file

@ -0,0 +1,200 @@
1036
1897
1256
1080
1909
1817
1759
1883
1088
1841
1780
1907
1874
1831
1932
1999
1989
1840
1973
1102
1906
1277
1089
1275
1228
1917
1075
1060
1964
1942
2001
1950
1181
1121
1854
1083
1772
1481
1976
1805
1594
1889
1726
1866
798
1739
1709
1946
1948
1808
1836
1849
1465
1066
1943
664
1894
1993
1061
1225
1589
1916
1885
1998
1470
1668
1666
1499
1437
1986
1127
1875
1132
1888
1877
1046
1982
1265
1757
1848
1786
1638
1958
1015
1013
1552
1742
1850
1016
1839
558
1826
1261
1988
1545
1078
1963
1967
1951
1086
1947
1880
1903
1994
1167
1736
1041
1652
1040
1033
1179
1844
1861
1488
1962
1135
1347
1187
1777
1598
1803
1147
1760
1926
1898
1923
1865
1313
1924
1023
1576
1715
1391
1346
1882
2000
1024
1143
1065
1560
1029
1119
1966
1022
1931
1512
1049
1929
1312
1069
1159
1053
1249
1074
1983
1761
1868
195
24
1331
1636
1020
1034
1671
708
1699
1900
1927
1829
301
1832
1042
1896
1928
1032
1992
2005
1955
1047
1068
1001
1052
1744
1845
1208
1018
1859
1342
1823
1758
2007
1241
1893
1876
1984
1655
1534
1150
1789
1870

View file

@ -1,6 +1,37 @@
import std.stdio; import core.stdc.stdlib;
import std.conv;
import std.format;
import std.stdio;
import std.getopt;
import day1;
immutable string progName = "aoc-2020";
void function(string[])[] programs = [
&day1.run
];
void main(string[] args) {
int day;
if (args.length < 2) {
stderr.writeln("USAGE: %s [day]".format(args[0]));
exit(-1);
}
try {
day = to!int(args[1]);
} catch (ConvException e) {
stderr.writeln("[day] is not an integer");
exit(-1);
}
if (day <= 0 || day > programs.length) {
stderr.writeln("Day must be between 1 and %d".format(programs.length - 1));
exit(-1);
}
programs[day - 1](args[2..$]);
void main()
{
writeln("Edit source/app.d to start your project.");
} }

58
source/day1.d Normal file
View file

@ -0,0 +1,58 @@
import std.algorithm;
import std.array;
import std.conv;
import std.exception;
import std.format;
import std.range;
import std.stdio;
immutable string progName = "aoc-2020";
void run(string[] args) {
enforce(args.length == 1, "Please provide a part to run %s 1 [part]".format(progName));
int part = to!int(args[0]);
enforce(part > 0 && part <= 2, "Parts %d to %d supported".format(1, 2));
auto numbers = stdin.byLineCopy.map!(a => to!int(a)).array.sort;
auto fun = part == 1 ? &part1 : &part2;
writeln(fun(numbers));
}
int part1(SortedRange!(int[]) numbers) {
int result = -1;
foreach (ref int a; numbers) {
int b = 2020 - a;
if (numbers.contains(b)) {
result = b * a;
break;
}
}
return result;
}
unittest {
auto numbers = [1721, 979, 366, 299, 675, 1456].sort;
assert(part1(numbers) == 514579);
}
int part2(SortedRange!(int[]) numbers) {
int result = -1;
foreach (ref int a; numbers) {
foreach (ref int b; numbers) {
int c = 2020 - b - a;
if (numbers.contains(c)) {
result = c * b * a;
goto exit;
}
}
}
exit:
return result;
}
unittest {
auto numbers = [1721, 979, 366, 299, 675, 1456].sort;
assert(part2(numbers) == 241861950);
}