You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.3 KiB
Dart
43 lines
1.3 KiB
Dart
import 'package:aoc2020/aocbase.dart';
|
|
import 'package:aoc2020/model/point.dart';
|
|
import 'package:aoc2020/model/readdata.dart';
|
|
|
|
class AOC20201203 extends AOCBase {
|
|
static const tree = '#';
|
|
|
|
int indexWrap({int index, int width}) => index % width;
|
|
|
|
int treeTest({int index, String line}) {
|
|
var testChar = line[indexWrap(index: index, width: line.length)];
|
|
return testChar == tree ? 1 : 0;
|
|
}
|
|
|
|
int tobogganRide({List<String> hill, Point start, Point slope}) {
|
|
var numberOfTrees = 0;
|
|
var pos = start;
|
|
for (var lineNum = 0; lineNum < hill.length; lineNum += slope.item2) {
|
|
numberOfTrees += treeTest(index: pos.item1, line: hill[lineNum]);
|
|
pos = pos + slope;
|
|
}
|
|
return numberOfTrees;
|
|
}
|
|
|
|
@override
|
|
Future<void> a({bool test}) async {
|
|
var mylist = await ReadData.readFile<String>(classString, test: test);
|
|
answerA = tobogganRide(hill: mylist, start: Point(0, 0), slope: Point(3, 1));
|
|
}
|
|
|
|
@override
|
|
Future<void> b({bool test}) async {
|
|
var mylist = await ReadData.readFile<String>(classString, test: test);
|
|
|
|
var slopes = <Point>[Point(1, 1), Point(3, 1), Point(5, 1), Point(7, 1), Point(1, 2)];
|
|
answerB = 0;
|
|
for (var slope in slopes) {
|
|
var trees = tobogganRide(hill: mylist, start: Point(0, 0), slope: slope);
|
|
answerB = (answerB == 0) ? trees : answerB * trees;
|
|
}
|
|
}
|
|
}
|