From 09cd18143e29bed44ff8533b2e12fef8e86898da Mon Sep 17 00:00:00 2001 From: Pascal Welsch Date: Sun, 2 Dec 2018 14:57:06 +0100 Subject: [PATCH] Use readLines --- 02/day2.dart | 64 ++++++++++++ 02/day2_test.dart | 38 +++++++ 02/input.txt | 250 ++++++++++++++++++++++++++++++++++++++++++++++ pubspec.yaml | 1 + 4 files changed, 353 insertions(+) create mode 100644 02/day2.dart create mode 100644 02/day2_test.dart create mode 100644 02/input.txt diff --git a/02/day2.dart b/02/day2.dart new file mode 100644 index 0000000..a6470de --- /dev/null +++ b/02/day2.dart @@ -0,0 +1,64 @@ +import 'dart:io'; + +import 'package:dart_kollection/dart_kollection.dart'; + +int checksum(KList listIds) { + final boxes = listIds.map((id) => Box(id)); + + var doubles = 0; + var triples = 0; + for (final box in boxes.iter) { + if (box.withDoubles) doubles++; + if (box.withTriples) triples++; + } + return doubles * triples; +} + +class Box { + final String id; + bool withDoubles; + bool withTriples; + + Box(this.id) { + final chars = listOf(id.codeUnits); + final groups = + chars.groupBy((it) => it).mapValues((entry) => entry.value.size); + withDoubles = groups.containsValue(2); + withTriples = groups.containsValue(3); + } +} + +KPair fabricPairs(KList boxIds) { + for (final id1 in boxIds.iter) { + for (final id2 in boxIds.iter) { + if (id1 == id2) continue; + var diff = 0; + for (var i = 0; i < id1.length; i++) { + if (diff > 1) break; + if (id1[i] != id2[i]) { + diff++; + } + } + if (diff == 1) { + return KPair(id1, id2); + } + } + } + throw "not found"; +} + +String commonLetters(KPair pair) { + var c1 = listOf(pair.first.runes); + var c2 = listOf(pair.second.runes); + return String.fromCharCodes(c1.filter((it) => c2.contains(it)).iter); +} + +main() { + KList input = listOf(File("02/input.txt").readAsLinesSync()); + + print("checksum: ${checksum(input)}"); + + final pair = fabricPairs(input); + print("boxes with fabric: $pair"); + print("common letters: ${commonLetters(pair)}"); +} diff --git a/02/day2_test.dart b/02/day2_test.dart new file mode 100644 index 0000000..942d685 --- /dev/null +++ b/02/day2_test.dart @@ -0,0 +1,38 @@ +import 'package:dart_kollection/dart_kollection.dart'; +import 'package:test/test.dart'; + +import 'day2.dart'; + +main() { + group("part1", () { + test('box has no doubles', () { + expect(Box("abcdef").withDoubles, false); + }); + test('box has no triples', () { + expect(Box("abcdef").withTriples, false); + }); + test('box has doubles', () { + expect(Box("bababc").withDoubles, true); + }); + test('box has triples', () { + expect(Box("bababc").withTriples, true); + }); + }); + group("part2", () { + test('checksum sample', () { + var ids = listOf([ + "abcdef", + "bababc", + "abbcde", + "abcccd", + "aabcdd", + "abcdee", + "ababab" + ]); + expect(checksum(ids), 12); + }); + test('common letters sample', () { + expect(commonLetters(KPair("fghij", "fguij")), "fgij"); + }); + }); +} diff --git a/02/input.txt b/02/input.txt new file mode 100644 index 0000000..df30d18 --- /dev/null +++ b/02/input.txt @@ -0,0 +1,250 @@ +auxwcbzrmdvpsjfgkrthnkioqm +auxwcbzrmdvpsjfgbltonyijqe +auxwcbzrmdfpsefgklthnoioqe +auxwcbzrmdvpsjfgkluhnjisqe +auxwcbzrmdvesjfgdzthnyioqe +auxwcbzrmdvhsjfgklthnmijqe +auxwcbzridvpsjfgkltxeyioqe +ayxwcbzrgdvpsjfgklthiyioqe +ajxwcbzrmdvpsjfgklkhnyiode +auxwcbcrmdvpsjfqelthnyioqe +auxwcbzrmsvpsjsgklthnyiope +auxwcbzrmqvpsjzgklghnyioqe +auxwcbzrmdvpsjtqklthxyioqe +auxwcbzrmdopsjfdklthncioqe +auxwcbzrmdvpsjfgkltmhyfoqe +aixwcbzrmdvpsjfgllthdyeoqe +vuxicbzrmdepsjfgklthnyioqe +auxwcbbxmdkpsjfgklthnyioqe +auxwcbzrgdvpsofaklthnyioqe +auxycbzrmdvpsjfgklthnyuose +aujwcbzrmdvprjfgkltcnyioqe +auxwgbzrmdvpsjfgyzthnyioqe +auxwcbzrmavpsjfgkltsnyiome +auxwcbgrmdvpsjfgkdthnrioqe +kuxwcbzrmdvpsyfgklthnyioue +auxwcbzomdvpjdfgklthnyioqe +auxwcbzrmdppsjfgklthvyifqe +aunwubzrmdvpsjrgklthnyioqe +auxwcbzrmoipsjfgklbhnyioqe +auxwdbzrmdvpsjfgmlthnyioce +auxwcbzjmsvpsjfiklthnyioqe +auxwcbzrmwcpsjfcklthnyioqe +auxwcbzfmdvprjfhklthnyioqe +auxdcbzrgdvpsjfgklthnyxoqe +wuxwbbzrmdvpsjfgklthnyiote +auowcbjrmdvpsjfgklthnyfoqe +auxwsbzrmdvpsjfglltcnyioqe +quxwcbzrmdvpkjfgklthnyioqt +vuxwcbzrudvpsjfgklthnyioqi +puxwcbzrmdvgsjfgklthncioqe +luxdcbzrmdvpsjfgkothnyioqe +auxwcbzrmdvpsjfyklthfhioqe +auxwcbqrmdvpsjfgkldhnyiote +quxwcbzrmlvpsjfgklthnyioqi +auxwcbzgmdvpsjfoklthnyiuqe +auxwcbzrmdvpsbfgkltdjyioqe +auxwcbzsmdrpsjfgklthpyioqe +auxwcbzrmfvpsjfwklthnyiote +auxbkpzrmdvpsjfgklthnyioqe +auxwcbzrddvpsjfsklthnyroqe +abxwcbzrmdvpsjfgkltdnyivqe +awxwcbzrmvvpsjfgklthngioqe +auxwcbzrmkvgsjfgkltcnyioqe +auxwcbammdvpsjfgklthpyioqe +auxwcbhrmdvpsjfgtlthnuioqe +auxwcpzrmdvpbjogklthnyioqe +auxwcbzrmdvpslfgklbhkyioqe +auxwcbsrmdvpjjfgkldhnyioqe +auxwcbzrmdqpsjfgauthnyioqe +ydxwcbxrmdvpsjfgklthnyioqe +auxwcbzrmdvpejfgklthnyyofe +auxwchzrmxvpsjfgklthnyioqh +auxwcbzrtdvpsjfgklxhnzioqe +auxwcbyrmdvpsnfgklnhnyioqe +auxwcbzrcdvpsjugklihnyioqe +auxwcbzrddvpsjfgklhhnyiaqe +aumwtbzrmdvpsjfgklthnyitqe +auxucbzrmdvpsjfgklthwfioqe +auxwcbzrmdvpzmfgkllhnyioqe +auxwcbzrmdvpsjhgklthntiome +buxwzbzrmdvpszfgklthnyioqe +ouxwcbzsgdvpsjfgklthnyioqe +auxwcbzrmdvpsjfskltgnyioqz +auxwcbbrmdvpsjftklthnyioqu +quxocbzrmdvpsjfgklthfyioqe +acxwcbzrmdvpsjfgklfhnrioqe +auxwcbzrmdnpsjfrkjthnyioqe +wuxwybzrmdwpsjfgklthnyioqe +auxwgbxrmdvpsjfghlthnyioqe +atxwcbzrmdvnsjfgklthnyjoqe +acxwcbzmmdvpsjfbklthnyioqe +auxhcbzrmdvbsjbgklthnyioqe +auxwlbzrfdvpsjfgxlthnyioqe +auxwmbzrmdfpsjqgklthnyioqe +auxwcbzrmdvpsgfgklahnyigqe +auxwgbzrmdvpsjfgzldhnyioqe +auxwcbzrmdvpydfgklthnyiohe +auxwxbzrmdvpsjfsklchnyioqe +auxqcbzrmdvpsjfgqlthnyiwqe +auxwcozrmdvssbfgklthnyioqe +auxvcczrmdvpsufgklthnyioqe +auxwcbzrudvpsjfgklyhnyioxe +aulwcbzrmdvpsjqgknthnyioqe +auukcbzrmdvpsjfgklthtyioqe +auxwcszimdvpsjfgklthnyigqe +juxwcbzrbdvpsjfgklthnyboqe +auxwcbzrmdvpjofgklthnyioqj +auxwcbzrmdvpsjfgplfhnyione +auxwcbzrmdhpsjfgkltknyeoqe +luxwcqzrmdvpsjfgklthnbioqe +uuxwcbzrmdvpsjfgkithnyiiqe +auxwcbzrmdvpdjfgkrthnyeoqe +auuwcbnrmdvpsjfgklthnjioqe +auxwcnzrmdvpsjvgklthnyooqe +auxwcbzcmdvpsjfcklthnyiose +auxwcbzrldfpsjfgklthjyioqe +auxwcizrmdvpsjfjklthnymoqe +auxwcbtrmdvpsjfgtlphnyioqe +amxwcbzrmdvksjfgklthnyiove +auxwcbzrmdvpszfgkpthnyiuqe +auxwcbzrmdvxdjfgkltqnyioqe +auxwcbzrudvpsjfgklthnymiqe +auxwcbirmdvfsjfgklmhnyioqe +auwwcbzrndvprjfgklthnyioqe +auxwcbormdgpsjfgklbhnyioqe +auxwabzrmdupsjfgklthnyioqt +auxvcbzrmdvpsjfgkltrmyioqe +auxwcbzrmddpsjfsklthnyizqe +auxwcczrmuvpyjfgklthnyioqe +auxwcczrmdvpsnfgkpthnyioqe +auxkcbzrmdvpsjfhklihnyioqe +auxwcbzrmdvpsjfgklthnkijje +auxwcbzcmdvpsjpgkldhnyioqe +auxwcnzrudvpstfgklthnyioqe +xuxwcbzrgdvusjfgklthnyioqe +aaxwcbzrmdvpsjvgklthnyidqe +auxwcbztmdvpsjfgklthnyhqqe +auxwcbzrmfvpsjfgklthnyilfe +auxwcbzrmdvksjfgklthjyioqq +auxwcbzrmdzksjfgktthnyioqe +auxwcbzrmfvpszfgklohnyioqe +auxwckzamdvpsjfgklthnyioqs +auxwcmzrhdvpsjfaklthnyioqe +fuxwcbzrmdapsjfgklrhnyioqe +avxwxbzrmdvpsjfgklthniioqe +auxwubzrmevpsjfgkltpnyioqe +fuxwcbzrgdvpsjfgklhhnyioqe +duxwwbdrmdvpsjfgklthnyioqe +audwcbzrmdvpnjcgklthnyioqe +auxtcbzrmdvpsjmgklthnyyoqe +aucwcbwrmdepsjfgklthnyioqe +auxwcbzrudvpsjfpklthnyiose +auxwcbzridvpsjfsklthxyioqe +auxtcbzrmdvpscfgklyhnyioqe +auxwcbzrmdvppjfgklthnyivee +auxwdbzrmuvpskfgklthnyioqe +auxwubzrmdvosjfgklthnyiope +auxwcbzrmhnpsjfgklthnyimqe +auxwcbzrmdqpwjfgkltpnyioqe +auxwcbormdvpsjljklthnyioqe +auxwcbzrmdjpsjfgkltjpyioqe +auxwcbzrmdvpszfgklthkyizqe +auxwcbzighvpsjfgklthnyioqe +auxwcbzrmdlpsjfgcythnyioqe +auxwcbzumdvpsjflklthnyimqe +pdxwcbzrmdvpsjfgklthnyihqe +auxwcbzrsdvpsjfgklhhvyioqe +auxwcfzamdvpsjfgkmthnyioqe +aexwcdzrmdvpsjogklthnyioqe +auxxcbkrmavpsjfgklthnyioqe +auxwcbzredvssjfgklthryioqe +aupwqbzrmdvpsjfgklthnyioqc +auxwcbzrmdvpkcagklthnyioqe +auxwcbzrmdvwsbfgklthnlioqe +aunwcbzxmdvhsjfgklthnyioqe +auxwcbzrhddpsjfgklthnnioqe +ouxwcbzrmdvtsifgklthnyioqe +auxwcbzrmdqpsjfgklthnyfoqp +auxwrbzrhdvpsjfgolthnyioqe +auxwcbcqmdvpsjugklthnyioqe +auxwcbzrqdvpsjhgklthnjioqe +auxmcbzrmdvpsjfgmlthnyjoqe +auxwcbzrmdvpsjfgzlthnycoqv +auswcbzrmdvpsffgslthnyioqe +auxwcbzrfdvpsjfrmlthnyioqe +auxwcbzrmdvpsjngzlthnxioqe +auxwcbzrmdvpsjfuqlthnyiyqe +auxwzbzrrdvosjfgklthnyioqe +auxwcbzdmdvpsjfikxthnyioqe +guxwcbzrmdvpsjfgmlthnytoqe +auxwcbzrmdvpspfgkytenyioqe +auxvcbzrldvpsjfgklthnyhoqe +auxwcbzrmavpckfgklthnyioqe +autwcbzrmdvpsafgklthnyirqe +auxwcbzrxuvpsjfgklthmyioqe +auxwcbarmdppsjfgklthnywoqe +anxvcbzrmdvpsjfgklthnyijqe +auxwcbwrmdapsjngklthnyioqe +abxwcbzrmdvpsjugkltgnyioqe +auxwcbtrmdvpsjfgkltunyioue +aujwcbzrmovpsjfgklthryioqe +auxwcbzrydvpsjfgklthndikqe +auxwcbzrmdvpsjfgklmrnyioqo +auxwcbzrddvpsjfggithnyioqe +auxwcbzrmdvpfjfaklthlyioqe +fuxtcbzrmdvpsjfgklwhnyioqe +tuxwcbzrjdvpsjfgjlthnyioqe +auxwcbzrmdppsofgklthnyfoqe +auxvclzamdvpsjfgklthnyioqe +auxwcbzrmdvpsjfdklhhnzioqe +auxwcbzrmsvpsvdgklthnyioqe +arxfcbzrmdvpsvfgklthnyioqe +auxzcbzrmdvpsjfgklthnhioqj +auxwcbzrrdvpsjfgpltunyioqe +auxuibzrmdvpwjfgklthnyioqe +auxwcbzrwdqpsjfgklthnyooqe +aujwcbzrmdvpsjvgklthxyioqe +abxwcbzrmfvpsjfgklthnyxoqe +aurwcbzrmdvpshfgklthnyhoqe +auxwcbzjmdvpsjfgknthnycoqe +auxwcbzrmdvpsjfgklmhxwioqe +auxwcbzrmfvpsjfgklthnyiorq +auxwcbormdvpsjfgklwhnlioqe +auxwctzrmdvpsjfgklcknyioqe +awxwcbzrmdvpsjfgvlthnyiome +auxwcbzrmdvpsjfjklthnyixje +auxwcsxrmdvpsjfgkltsnyioqe +auxbmbzrmdvpsjfgklthnyioce +auxwcbzrmdvpsjfukzthnytoqe +aixwcbzrmdvpsjfgllthdyioqe +auxwcbzrmdypsjfgklthnlioqy +auxccbzrmdvpsjfgkltrnnioqe +auxwcznrmdvpsjfgklthnykoqe +auxwmqzrmdvpsjfgilthnyioqe +auxwcbzrmdvpdyfgolthnyioqe +auxwcbzrmdvpsjfgkmohnqioqe +auxwcfzrmzvpsjfoklthnyioqe +auxwjyzrmdvpsjfgulthnyioqe +auxwcgzredvpsjfgkxthnyioqe +wuxwcbtrmdvpsjfgklthnyiofe +auxwcbzrmdopsgfgklthncioqe +auxmcbzjmdvpsjfgklbhnyioqe +auxwlbzrmdvpsjffklthgyioqe +auxwcbzrmrvpsjfgqlthtyioqe +kuxwhbzrmdvpsjfgklthgyioqe +auxwcozrmdgpsjfgklthnydoqe +auxwdbzrmdvpdjfgklthgyioqe +auxwqbzrmdapsvfgklthnyioqe +auqwcbzridvjsjfgklthnyioqe +auxwckzrmdvpsjfoklthnyuoqe +auxwcbzvmdvpsjfgklghnyiome +auxtcbzrmdvpsjqgktthnyioqe +auxwcbzrmdvesjfgkljhnnioqe +auxwcbzrmpvpsqfgklthnqioqe +auxwcbzrmdcpsqfgklthnzioqe +yuxwcbzrmdvpsjggklthnlioqe +auxwcbzradvpsjftklthoyioqe +auxwcbzrmdvjujfgklmhnyioqe +auxwcbzrmdvpsrfgklpinyioqe +auxwobzrvqvpsjfgklthnyioqe \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index fc5b5d6..599d7ff 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,5 +9,6 @@ environment: dependencies: test: ^1.0.0 + dart_kollection: ^0.1.0 dev_dependencies: