From 966de05ae8dda3ca7456657e797587019f1fc687 Mon Sep 17 00:00:00 2001 From: Peter Resch Date: Sat, 5 Dec 2020 11:36:53 -0600 Subject: [PATCH] Day 1 --- aoc2020/bin/main.dart | 3 ++- aoc2020/lib/aoc2020.dart | 27 +++++++++++++++++++++++---- aoc2020/lib/aoc20201201.dart | 24 ++++++++++++++++++++---- aoc2020/lib/model/readdata.dart | 4 ++-- 4 files changed, 47 insertions(+), 11 deletions(-) diff --git a/aoc2020/bin/main.dart b/aoc2020/bin/main.dart index 7e0223e..689fa21 100644 --- a/aoc2020/bin/main.dart +++ b/aoc2020/bin/main.dart @@ -8,7 +8,8 @@ ArgResults argResults; void main(List arguments) { final parser = ArgParser() ..addOption('day', abbr: 'd', help: 'Select the day') //, callback: (day) => print('the day is aoc202012$day')) - ..addFlag('list', abbr: 'l', help: 'List available days', callback: (_) => aoc2020.list()); + ..addFlag('list', abbr: 'l', help: 'List available days', callback: (_) => aoc2020.list()) + ..addFlag('runAll', abbr: 'r', help: 'Run all available days', callback: (_) => aoc2020.runAll()); argResults = parser.parse(arguments); // final paths = argResults.rest; // print('paths: ${paths}!'); diff --git a/aoc2020/lib/aoc2020.dart b/aoc2020/lib/aoc2020.dart index 7aa7e18..b176e8d 100644 --- a/aoc2020/lib/aoc2020.dart +++ b/aoc2020/lib/aoc2020.dart @@ -14,6 +14,25 @@ class aoc2020 { ]; static void list() async { + print('Days available:'); + for (var index = 0; index < aocDays.length; index++) { + var aocReflect = reflect(aocDays[index]); + var classString = aocReflect.toString().split("'")[1]; + + print('${index + 1} : ${classString ?? 'null'}'); + // var myclass = aocDays[index]; + // await myclass.a(test: true); + // await myclass.b(test: true); + // print('$index : ${myclass.answerA ?? 'null'}'); + // print('$index : ${myclass.answerB ?? 'null'}'); + // await myclass.a(); + // await myclass.b(); + // print('$index : ${myclass.answerA ?? 'null'}'); + // print('$index : ${myclass.answerB ?? 'null'}'); + } + } + + static void runAll() async { print('Days available:'); for (var index = 0; index < aocDays.length; index++) { var aocReflect = reflect(aocDays[index]); @@ -23,12 +42,12 @@ class aoc2020 { var myclass = aocDays[index]; await myclass.a(test: true); await myclass.b(test: true); - print('$index : ${myclass.answerA ?? 'null'}'); - print('$index : ${myclass.answerB ?? 'null'}'); + print('$classString A Test : ${myclass.answerA ?? 'null'}'); + print('$classString B Test : ${myclass.answerB ?? 'null'}'); await myclass.a(); await myclass.b(); - print('$index : ${myclass.answerA ?? 'null'}'); - print('$index : ${myclass.answerB ?? 'null'}'); + print('$classString A : ${myclass.answerA ?? 'null'}'); + print('$classString B : ${myclass.answerB ?? 'null'}'); } } } diff --git a/aoc2020/lib/aoc20201201.dart b/aoc2020/lib/aoc20201201.dart index 3385d89..1036a76 100644 --- a/aoc2020/lib/aoc20201201.dart +++ b/aoc2020/lib/aoc20201201.dart @@ -9,13 +9,29 @@ class AOC20201201 extends AOCBase { print("Running 'a'" + ((test ?? false) ? 'with test' : '')); print('$classStringTest'); var mylist = await ReadData.readFileNum(filename); - print(mylist); - answerA = 0; + for (var index1 = 0; index1 < mylist.length - 1; index1++) { + for (var index2 = index1 + 1; index2 < mylist.length; index2++) { + if (mylist[index1] + mylist[index2] == 2020) { + answerA = mylist[index1] * mylist[index2]; + } + } + } } @override Future b({bool test}) async { - print("Running 'b'"); - answerB = 1; + var filename = (test ?? false) ? classStringTest : classString; + print("Running 'b'" + ((test ?? false) ? 'with test' : '')); + print('$classStringTest'); + var mylist = await ReadData.readFileNum(filename); + for (var index1 = 0; index1 < mylist.length - 1; index1++) { + for (var index2 = index1 + 1; index2 < mylist.length; index2++) { + for (var index3 = index2 + 1; index3 < mylist.length; index3++) { + if (mylist[index1] + mylist[index2] + mylist[index3] == 2020) { + answerB = mylist[index1] * mylist[index2] * mylist[index3]; + } + } + } + } } } diff --git a/aoc2020/lib/model/readdata.dart b/aoc2020/lib/model/readdata.dart index e9e73ea..1ea9649 100644 --- a/aoc2020/lib/model/readdata.dart +++ b/aoc2020/lib/model/readdata.dart @@ -6,7 +6,7 @@ class ReadData { var file = File(fileName); var contents = []; - print('open file "$fileName"'); + // print('open file "$fileName"'); if (await file.exists()) { contents = await File(fileName).readAsLines(); } else { @@ -20,7 +20,7 @@ class ReadData { var file = File(fileName); var contents = []; - print('open file "$fileName"'); + // print('open file "$fileName"'); if (await file.exists()) { var contentStrings = await File(fileName).readAsLines(); contents = contentStrings.map(num.parse).toList();