diff --git a/aoc2020/lib/aoc2020.dart b/aoc2020/lib/aoc2020.dart index 42e34f1..0fd3d53 100644 --- a/aoc2020/lib/aoc2020.dart +++ b/aoc2020/lib/aoc2020.dart @@ -7,6 +7,7 @@ import 'package:aoc2020/aoc20201206.dart'; import 'package:aoc2020/aoc20201207.dart'; import 'package:aoc2020/aoc20201208.dart'; import 'package:aoc2020/aoc20201209.dart'; +import 'package:aoc2020/aoc20201210.dart'; import 'package:aoc2020/aocbase.dart'; import 'dart:mirrors'; @@ -26,6 +27,7 @@ class aoc2020 { AOC20201207(), AOC20201208(), AOC20201209(), + AOC20201210(), ]; static void list() async { diff --git a/aoc2020/lib/aoc20201210.dart b/aoc2020/lib/aoc20201210.dart index 54b532c..8ccec1b 100644 --- a/aoc2020/lib/aoc20201210.dart +++ b/aoc2020/lib/aoc20201210.dart @@ -1,16 +1,68 @@ +import 'dart:math'; import 'package:aoc2020/aocbase.dart'; import 'package:aoc2020/model/readdata.dart'; class AOC20201210 extends AOCBase { + var mylist = []; + var dest = []; + + void init(bool test) async { + mylist = await ReadData.readFile(classString, test: test); + mylist.sort(); + mylist.insert(0, 0); + mylist.add(mylist.last + 3); + dest = []; + for (var index = 1; index < mylist.length; index++) { + dest.add(mylist[index] - mylist[index - 1]); + } + } + @override Future a({bool test}) async { - var filename = (test ?? false) ? classStringTest : classString; - var mylist = await ReadData.readFileString(filename); + await init(test); + + var hist = {}; + for (var num in dest) { + hist[num] = hist[num] == null ? 1 : hist[num] + 1; + } + // print(hist); + answerA = hist[1] * hist[3]; } @override Future b({bool test}) async { - var filename = (test ?? false) ? classStringTest : classString; - var mylist = await ReadData.readFileString(filename); + var groups = {}; + + await init(test); + + // Count groups + var groupStart = false; + var groupCount = 0; + for (var index = 0; index < dest.length; index++) { + if (!groupStart && dest[index] == 1) { + groupStart = true; + groupCount = 0; + } + if (groupStart) { + if (dest[index] != 1) { + groupStart = false; + groups[groupCount] = groups[groupCount] == null ? 1 : groups[groupCount] + 1; + } else { + groupCount++; + } + } + } + var multiplier = [1, 2, 4]; + for (var index = 3; index < 30; index++) { + multiplier.add(multiplier[index-1] + multiplier[index-2] + multiplier[index-3]); + } + // print(groups); + answerB = 1; + for (var group in groups.keys) { + if (group > 1) { + // print('for group $group ${multiplier[group]} ^ ${groups[group]}'); + answerB = answerB * pow(multiplier[group-1], groups[group]); + } + } } } diff --git a/aoc2020/lib/data/aoc20201210.data b/aoc2020/lib/data/aoc20201210.data new file mode 100644 index 0000000..ec74d95 --- /dev/null +++ b/aoc2020/lib/data/aoc20201210.data @@ -0,0 +1,99 @@ +18 +47 +144 +147 +124 +45 +81 +56 +16 +59 +97 +83 +75 +150 +33 +165 +30 +159 +84 +141 +104 +25 +164 +90 +92 +88 +2 +8 +51 +24 +153 +63 +27 +123 +127 +58 +108 +52 +38 +15 +149 +66 +72 +21 +46 +89 +135 +55 +34 +37 +78 +65 +134 +148 +76 +138 +103 +162 +114 +109 +42 +77 +102 +163 +7 +105 +69 +39 +91 +111 +131 +130 +6 +137 +96 +82 +64 +3 +95 +136 +85 +9 +116 +17 +99 +12 +117 +62 +50 +110 +26 +115 +71 +57 +156 +120 +98 +1 +70 diff --git a/aoc2020/lib/data/aoc20201210test.data b/aoc2020/lib/data/aoc20201210test.data new file mode 100644 index 0000000..e6376dc --- /dev/null +++ b/aoc2020/lib/data/aoc20201210test.data @@ -0,0 +1,31 @@ +28 +33 +18 +42 +31 +14 +46 +20 +48 +47 +24 +23 +49 +45 +19 +38 +39 +11 +1 +32 +25 +35 +8 +17 +7 +9 +4 +2 +34 +10 +3