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 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 a({bool test}) async { var mylist = await ReadData.readFile(classString, test: test); answerA = tobogganRide(hill: mylist, start: Point(0, 0), slope: Point(3, 1)); } @override Future b({bool test}) async { var mylist = await ReadData.readFile(classString, test: test); var slopes = [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; } } }