-
Notifications
You must be signed in to change notification settings - Fork 0
/
day06.dart
69 lines (51 loc) · 1.87 KB
/
day06.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
import 'package:adventofcode2023/day.dart';
import 'package:adventofcode2023/helper/string_extensions.dart';
import 'package:collection/collection.dart';
class Day6 implements Day {
@override
int get dayNumber => 6;
@override
String solveFirstPuzzle(String puzzleInput) {
var timesAndDistances = puzzleInput.splitPerLine().map((e) => e.split(' '));
var times = timesAndDistances.first
.where((element) => element.isNotEmpty && element.isNumeric())
.map((e) => int.parse(e))
.toList();
var distances = timesAndDistances.last
.where((element) => element.isNotEmpty && element.isNumeric())
.map((e) => int.parse(e))
.toList();
var races = IterableZip([times, distances]).map((e) => Race(e.first, e.last));
List<int> allNumbersOfWaysWinningRaces = [];
for (var race in races) {
allNumbersOfWaysWinningRaces.add(race.numberOfWaysWinningTheRace());
}
var result = allNumbersOfWaysWinningRaces.reduce((value, element) => value * element);
return result.toString();
}
@override
String solveSecondPuzzle(String puzzleInput) {
var timesAndDistances = puzzleInput.splitPerLine().map((e) => e.split(':'));
var time = int.parse(timesAndDistances.first.last.replaceAll(' ', ''));
var distance = int.parse(timesAndDistances.last.last.replaceAll(' ', ''));
var race = Race(time, distance);
var result = race.numberOfWaysWinningTheRace();
return result.toString();
}
}
class Race {
final int _time;
final int _distance;
Race(this._time, this._distance);
int numberOfWaysWinningTheRace() {
var numberOfWaysWinningTheRace = 0;
for (var i = 0; i < _time; i++) {
var speed = i;
var currentDistance = (_time - i) * speed;
if (currentDistance > _distance) {
numberOfWaysWinningTheRace++;
}
}
return numberOfWaysWinningTheRace;
}
}