Skip to content

Commit

Permalink
Use readLines
Browse files Browse the repository at this point in the history
  • Loading branch information
passsy committed Dec 2, 2018
1 parent 02f986f commit 09cd181
Show file tree
Hide file tree
Showing 4 changed files with 353 additions and 0 deletions.
64 changes: 64 additions & 0 deletions 02/day2.dart
@@ -0,0 +1,64 @@
import 'dart:io';

import 'package:dart_kollection/dart_kollection.dart';

int checksum(KList<String> 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<String, String> fabricPairs(KList<String> 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<String, String> 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<String> 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)}");
}
38 changes: 38 additions & 0 deletions 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");
});
});
}
250 changes: 250 additions & 0 deletions 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
1 change: 1 addition & 0 deletions pubspec.yaml
Expand Up @@ -9,5 +9,6 @@ environment:

dependencies:
test: ^1.0.0
dart_kollection: ^0.1.0

dev_dependencies:

0 comments on commit 09cd181

Please sign in to comment.