day9: solve part 1 & 2

This commit is contained in:
Johannes Loher 2020-12-09 14:38:46 +01:00
parent 531b0d6632
commit 48c04f4fa0
10 changed files with 2157 additions and 0 deletions

15
day9/part1/.gitignore vendored Normal file
View file

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

12
day9/part1/dub.json Normal file
View file

@ -0,0 +1,12 @@
{
"authors": [
"Johannes Loher"
],
"copyright": "Copyright © 2020, Johannes Loher",
"dependencies": {
"mir-algorithm": "~>3.10.12"
},
"description": "A minimal D application.",
"license": "MIT",
"name": "day9-part1"
}

View file

@ -0,0 +1,7 @@
{
"fileVersion": 1,
"versions": {
"mir-algorithm": "3.10.12",
"mir-core": "1.1.53"
}
}

1000
day9/part1/input Normal file

File diff suppressed because it is too large Load diff

28
day9/part1/source/app.d Normal file
View file

@ -0,0 +1,28 @@
import std;
import mir.combinatorics;
void main()
{
File("input", "r").byLine.map!(to!long)
.array.findFirstNumberNotSumOf2PreviousNumbers(25).writeln;
}
auto findFirstNumberNotSumOf2PreviousNumbers(long[] numbers, size_t windowSize)
in(numbers.length > windowSize)
{
return numbers.slide(windowSize + 1)
.map!(window => tuple(window[0 .. $ - 1].combinations(2).map!sum, window[windowSize]))
.find!(pair => !pair[0].canFind(pair[1]))
.front[1];
}
unittest
{
long[] input = [
35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299,
277, 309, 576
];
assert(input.findFirstNumberNotSumOf2PreviousNumbers(5) == 127);
}

15
day9/part2/.gitignore vendored Normal file
View file

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

11
day9/part2/dub.json Normal file
View file

@ -0,0 +1,11 @@
{
"authors": [
"Johannes Loher"
],
"copyright": "Copyright © 2020, Johannes Loher",
"dependencies": {
"mir-algorithm": "~>3.10.12"
},
"license": "MIT",
"name": "day9-part2"
}

View file

@ -0,0 +1,7 @@
{
"fileVersion": 1,
"versions": {
"mir-algorithm": "3.10.12",
"mir-core": "1.1.53"
}
}

1000
day9/part2/input Normal file

File diff suppressed because it is too large Load diff

62
day9/part2/source/app.d Normal file
View file

@ -0,0 +1,62 @@
import std;
import mir.combinatorics;
void main()
{
File("input", "r").byLine.map!(to!long).array.breakXMASEncryption(25).writeln;
}
auto findFirstNumberNotSumOf2PreviousNumbers(long[] numbers, size_t windowSize)
in(numbers.length > windowSize)
{
return numbers.slide(windowSize + 1)
.map!(window => tuple(window[0 .. $ - 1].combinations(2).map!sum, window[windowSize]))
.find!(pair => !pair[0].canFind(pair[1]))
.front[1];
}
unittest
{
long[] input = [
35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299,
277, 309, 576
];
assert(input.findFirstNumberNotSumOf2PreviousNumbers(5) == 127);
}
auto findContiguousSubrangeWhichSumsTo(long[] input, long number)
{
return iota(2, input.length + 1).map!(i => input.slide(i))
.joiner
.find!(it => it.sum == number)
.front;
}
unittest
{
long[] input = [
35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299,
277, 309, 576
];
assert(input.findContiguousSubrangeWhichSumsTo(127) == [15, 25, 47, 40]);
}
auto breakXMASEncryption(long[] input, size_t windowSize)
{
auto invalidNumber = input.findFirstNumberNotSumOf2PreviousNumbers(windowSize);
auto contiguousSubrange = input.findContiguousSubrangeWhichSumsTo(invalidNumber);
return contiguousSubrange.minElement + contiguousSubrange.maxElement;
}
unittest
{
long[] input = [
35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299,
277, 309, 576
];
assert(input.breakXMASEncryption(5) == 62);
}