-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.dart
97 lines (78 loc) · 3.08 KB
/
day4.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import 'dart:io';
import 'dart:math';
class Day4 {
/// It looks like each card has two lists of numbers separated by a vertical bar (|): a list of winning numbers
/// and then a list of numbers you have. You organize the information into a table (your puzzle input).
final File _input = File('./input/day4.txt');
Future<List<String>> get _inputLines => _input.readAsLines();
Future<List<Card>> parseCards() async {
final List<String> inputLines = await _inputLines;
return inputLines.map((e) => Card.fromString(e)).toList();
}
/// Figure out which of the numbers you have appear in the list of winning numbers. The first match makes
/// the card worth one point and each match after the first doubles the point value of that card.
Future<int> part1() async {
List<Card> cards = await parseCards();
int totalScore = cards.fold(0, (previousValue, element) => previousValue + element.computeScore());
return totalScore;
}
/// Instead, scratchcards only cause you to win more scratchcards equal to the number of winning numbers you have.
/// Process all of the original and copied scratchcards until no more scratchcards are won.
/// Including the original set of scratchcards, how many total scratchcards do you end up with?
Future<int> part2() async {
List<Card> cards = await parseCards();
for (int i = 0; i < cards.length; i++) {
int cardCopies = cards[i].copies;
int matchingNumbers = cards[i].matchingNumbers();
if (matchingNumbers == 0) continue;
int original = i + 1;
for (int j = original + 1; j <= original + matchingNumbers; j++) {
if (j - 1 >= cards.length) break;
cards[j - 1].copies += 1 + cardCopies;
}
}
int totalCard = cards.fold(0, (previousValue, element) => previousValue + element.totalCard());
return totalCard;
}
}
class Card {
final String name;
late final int cardNumber;
final Set<int> winningNumbers;
final Set<int> numbers;
int copies = 0;
Card({
required this.name,
required this.winningNumbers,
required this.numbers,
}) {
cardNumber = int.parse(name.split(RegExp('\\s+')).last);
}
factory Card.fromString(String input) {
List<String> nameNumbers = input.split(':');
String name = nameNumbers.first.trim();
List<String> numbers = nameNumbers.last.split('|');
Set<int> extractNumbers(String numberInput) {
List<String> numbersStr = numberInput.trim().split(RegExp('\\s+'));
return numbersStr.map((e) => int.parse(e.trim())).toSet();
}
Set<int> winningNumbers = extractNumbers(numbers.first);
Set<int> userNumbers = extractNumbers(numbers.last);
return Card(
name: name,
winningNumbers: winningNumbers,
numbers: userNumbers,
);
}
int computeScore() {
Set<int> matchingNumbers = numbers.intersection(winningNumbers);
if (matchingNumbers.isEmpty) {
return 0;
}
return pow(2, matchingNumbers.length - 1).toInt();
}
//region used in part2
int matchingNumbers() => numbers.intersection(winningNumbers).length;
int totalCard() => 1 + copies;
//endregion
}