Last active
December 2, 2020 19:28
Advent Of Code 2020 Day 2 in dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
int i = 0; | |
final List<Object> solutions = [ | |
SolutionA(), | |
SolutionB() | |
]; | |
solutions.forEach((s) => print(s.toString())); | |
} | |
// Full inputs at https://adventofcode.com/2020/day/2/input | |
const inputAdvent2 = """1-3 a: abcde | |
1-3 b: cdefg | |
2-9 c: ccccccccc"""; | |
abstract class AdventSolution { | |
final int day; | |
final String name; | |
AdventSolution(this.day, this.name); | |
String get solution => getSolution(); | |
// implement in subclass | |
String getSolution(); | |
@override | |
String toString() { | |
return "Advent Of Code, " | |
"Day $day " | |
"${name.isNotEmpty ? name : ''} " | |
"solution: $solution"; | |
} | |
} | |
abstract class _Day2Solution extends AdventSolution { | |
_Day2Solution(name) : super(2, name); | |
} | |
class SolutionA extends _Day2Solution { | |
SolutionA() : super("A"); | |
String getSolution() { | |
var validCount = | |
_passwords.where((element) => element.isValidLegacy).length; | |
return validCount.toString(); | |
} | |
} | |
class SolutionB extends _Day2Solution { | |
SolutionB() : super("B"); | |
String getSolution() { | |
var validCount = _passwords.where((element) => element.isValid).length; | |
return validCount.toString(); | |
} | |
} | |
// parse input lines i.e. of: "1-3 a: abcde" to get password record items | |
List<PasswordRecord> _parseInputs(String inputs) => inputs | |
.split("\n") | |
.where((element) => element.isNotEmpty) | |
.map<PasswordRecord>((e) { | |
var pieces = e.split(": "), | |
policyPieces = pieces[0].split(' '), | |
range = policyPieces[0].split('-'); | |
return PasswordRecord( | |
password: pieces[1], | |
searchCharacter: policyPieces[1], | |
indexLower: int.parse(range[0]), | |
indexUpper: int.parse(range[1]), | |
); | |
}).toList(); | |
List<PasswordRecord> _passwords = _parseInputs(inputAdvent2); | |
class PasswordRecord { | |
final String _password; | |
final String _searchCharacter; | |
final int _indexLower; | |
final int _indexUpper; | |
PasswordRecord( | |
{String password, String searchCharacter, int indexLower, int indexUpper}) | |
: _password = password, | |
_searchCharacter = searchCharacter, | |
_indexLower = indexLower, | |
_indexUpper = indexUpper; | |
// total search occurances fall within min and max as defined by lower and upper index | |
bool get isValidLegacy { | |
final match = RegExp(_searchCharacter), | |
searchCount = match.allMatches(_password).length; | |
return searchCount >= _indexLower && searchCount <= _indexUpper; | |
} | |
// search should occur in either index (1-based) but not both | |
bool get isValid { | |
var t1Valid = _password[_indexLower - 1] == _searchCharacter, | |
t2Valid = _password[_indexUpper - 1] == _searchCharacter; | |
return (t1Valid || t2Valid) && !(t1Valid && t2Valid); | |
} | |
String get password => this._password; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment