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

Day 10 part 1

This commit is contained in:
Chris Josten 2020-12-10 09:58:18 +01:00
parent 4902daebaf
commit f728b02ad6
Signed by: chris
GPG key ID: 1795A594046530AB
3 changed files with 135 additions and 0 deletions

95
in/10.txt Normal file
View file

@ -0,0 +1,95 @@
76
12
97
28
132
107
145
121
84
34
115
127
22
23
11
135
113
82
140
119
69
77
83
36
13
37
92
133
122
99
147
112
42
62
65
40
123
139
33
129
149
68
41
16
48
109
5
27
142
81
90
9
78
103
26
100
141
59
55
120
126
1
35
2
20
114
58
54
10
51
116
93
6
134
108
47
70
91
138
63
19
64
148
106
21
98
43
30
146
46
128
73
94
87
29

View file

@ -17,6 +17,7 @@ import day6;
import day7;
import day8;
import day9;
import day10;
import dayutil;
immutable string progName = "aoc-2020";
@ -35,6 +36,7 @@ Variant function(int, File, bool, string[])[] programs = [
&day7.run,
&day8.run,
&day9.run,
&day10.run,
];
void printUsage(string message = null) {

38
source/day10.d Normal file
View file

@ -0,0 +1,38 @@
import std.array;
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.variant;
import dayutil;
Variant run(int part, File input, bool bigboy, string[] args) {
uint[] numbers = input.byLineCopy.map!(to!uint).array ~ [0u];
auto sortedNumbers = numbers.sort;
Variant result = parts!size_t(part,
() => part1(sortedNumbers));
return result;
}
size_t part1(T)(SortedRange!(T[]) numbers) {
size_t gap1 = 0, gap3 = 0;
foreach(window; numbers.slide!(No.withPartial)(2)) {
size_t diff = window[1] - window[0];
if (diff == 3) gap3++;
if (diff == 1) gap1++;
}
// Device adapter is always rated 3 volts higher
gap3++;
writeln(gap1, ", ", gap3);
return gap1 * gap3;
}
unittest {
uint[] numbers = [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];
numbers ~= [0u];
assert(part1(numbers.sort) == 220);
}