Skip to content

Instantly share code, notes, and snippets.

@deanbot
Last active December 8, 2020 15:43
Show Gist options
  • Save deanbot/99b52ed24fe8153bbfe79baeb88a0627 to your computer and use it in GitHub Desktop.
Save deanbot/99b52ed24fe8153bbfe79baeb88a0627 to your computer and use it in GitHub Desktop.
Advent Of Code 2020 Day 7 in dart
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/7/input
const inputsDay7a =
"""light red bags contain 1 bright white bag, 2 muted yellow bags.
dark orange bags contain 3 bright white bags, 4 muted yellow bags.
bright white bags contain 1 shiny gold bag.
muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.
shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.
dark olive bags contain 3 faded blue bags, 4 dotted black bags.
vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.
faded blue bags contain no other bags.
dotted black bags contain no other bags.""";
const inputsDay7b = """shiny gold bags contain 2 dark red bags.
dark red bags contain 2 dark orange bags.
dark orange bags contain 2 dark yellow bags.
dark yellow bags contain 2 dark green bags.
dark green bags contain 2 dark blue bags.
dark blue bags contain 2 dark violet bags.
dark violet bags contain no other bags.""";
List<BagRule> _parseInputs(String inputs) => inputs
.split('\n')
.where((element) => element.isNotEmpty)
.map<BagRule>((e) => BagRule.fromString(e))
.toList();
var _inputs = _parseInputs(inputsDay7b);
class BagContents {
final String color;
final int quantity;
BagContents({this.color, this.quantity});
@override
String toString() {
return "$quantity $color bag(s)";
}
}
class BagRule {
final String color;
final List<BagContents> contents;
// i.e.: bright magenta bags contain 1 clear blue bag, 3 wavy indigo bags.
BagRule.fromString(String input)
: color = input.substring(0, input.indexOf(' bags')),
contents = input
.replaceAll('.', '')
.split('contain ')[1]
.split(', ')
.map<dynamic>((e) {
if (e == 'no other bags') {
return null;
}
var pieces =
e.replaceAll(' bags', '').replaceAll(' bag', '').split(' ');
return BagContents(
quantity: int.parse(pieces[0]),
color: pieces.sublist(1, pieces.length).join(' '));
})
.whereType<BagContents>()
.toList();
@override
String toString() {
return "$color bag contain: $contents";
}
}
// recursively populate list of all rules including searchFor colors in their contents
List<BagRule> getContainers(List<String> searchFor, List<BagRule> containers) {
// get list of rules that contain search color in their contents
var newContainers = <BagRule>[];
searchFor.forEach((search) {
newContainers.addAll(_inputs
.where((e) => e.contents.where((e) => e.color == search).length > 0)
.toList());
});
// remove duplicates
newContainers = newContainers.toSet().toList();
// add new containers to list
containers.addAll(newContainers);
// do this search again for new list of containers otherwise return completed list
if (newContainers.length > 0) {
return getContainers(
newContainers.map((e) => e.color).toList(), containers);
} else {
return containers;
}
}
int getChildCount(String searchFor) => _inputs
.firstWhere((e) => e.color == searchFor)
.contents
.fold(0, (p, e) => p + e.quantity + e.quantity * getChildCount(e.color));
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 _Day7Solution extends AdventSolution {
_Day7Solution(name) : super(7, name);
}
class SolutionA extends _Day7Solution {
SolutionA() : super('A');
String getSolution() {
return getContainers(["shiny gold"], []).length.toString();
}
}
class SolutionB extends _Day7Solution {
SolutionB() : super('B');
String getSolution() {
return getChildCount("shiny gold").toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment