mirror of
https://github.com/HenkKalkwater/aoc-2020
synced 2024-11-22 02:55:18 +00:00
Day 2, puzzle 1
This commit is contained in:
parent
f6a49daaf9
commit
a29b28b303
|
@ -6,12 +6,14 @@ import std.stdio;
|
|||
import std.getopt;
|
||||
|
||||
import day1;
|
||||
import day2;
|
||||
import dayutil;
|
||||
|
||||
immutable string progName = "aoc-2020";
|
||||
|
||||
void function(string[])[] programs = [
|
||||
&day1.run
|
||||
&day1.run,
|
||||
&day2.run
|
||||
];
|
||||
|
||||
void printUsage(string name) {
|
||||
|
|
|
@ -15,7 +15,7 @@ void run(string[] args) {
|
|||
|
||||
/* For each line on stdin, copy it, map it to an integer and sort it.
|
||||
Sorting a range makes it a SortedRange and functions like contains(range, elem)
|
||||
will make use of optimised implementations, in the case of contains(range, elem)
|
||||
will make use of optimised implementations, in the case of contains(range, elem)
|
||||
it will use a binary search instead of a linear search */
|
||||
auto numbers = stdin.byLineCopy.map!(a => to!int(a)).array.sort;
|
||||
|
||||
|
|
27
source/day2.d
Normal file
27
source/day2.d
Normal file
|
@ -0,0 +1,27 @@
|
|||
import std.algorithm;
|
||||
import std.format;
|
||||
import std.stdio;
|
||||
|
||||
|
||||
import dayutil;
|
||||
|
||||
bool isPasswordValid(T)(T line) {
|
||||
int min, max;
|
||||
char c;
|
||||
string password;
|
||||
line.formattedRead("%d-%d %c: %s", min, max, c, password);
|
||||
ulong charCount = password.count(c);
|
||||
return min <= charCount && charCount <= max;
|
||||
}
|
||||
|
||||
void run(string[] args) {
|
||||
ulong count = stdin.byLine
|
||||
.count!(l => isPasswordValid(l));
|
||||
writeln(count);
|
||||
}
|
||||
|
||||
unittest {
|
||||
assert(isPasswordValid("1-3 a: abcde") == true);
|
||||
assert(isPasswordValid("1-3 b: cdefg") == false);
|
||||
assert(isPasswordValid("2-9 c: ccccccccc") == true);
|
||||
}
|
Loading…
Reference in a new issue