Skip to content

Commit

Permalink
Day04a: Solved
Browse files Browse the repository at this point in the history
  • Loading branch information
julemand101 committed Dec 9, 2023
1 parent 69e817c commit 97c5eef
Show file tree
Hide file tree
Showing 3 changed files with 291 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/day04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// --- Day 4: Scratchcards ---
// https://adventofcode.com/2023/day/4

import 'package:collection/collection.dart';

int solveA(Iterable<String> input) => input
.map(parse)
.map((card) =>
1 << card.winningNumbers.intersection(card.numbersYouHave).length >> 1)
.sum;

int solveB(Iterable<String> input) {
return 0;
}

RegExp regExp = RegExp(r'Card +(\d+): (.*) \| (.*)');

({
int cardId,
Set<int> winningNumbers,
Set<int> numbersYouHave,
}) parse(String line) {
final match = regExp.firstMatch(line)!;

return (
cardId: int.parse(match[1]!),
winningNumbers: match[2]!
.split(' ')
.map((e) => e.trim())
.where((e) => e.isNotEmpty)
.map(int.parse)
.toSet(),
numbersYouHave: match[3]!
.split(' ')
.map((e) => e.trim())
.where((e) => e.isNotEmpty)
.map(int.parse)
.toSet(),
);
}
Loading

0 comments on commit 97c5eef

Please sign in to comment.