-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from h3x4d3c1m4l/feature/aoc23-01
Add AoC 2023 day 01 solver
- Loading branch information
Showing
6 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export 'day_01_solver.dart'; |
8 changes: 8 additions & 0 deletions
8
lib/solvers/advent_of_code/2023/advent_of_code_2023_solver.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import 'package:h3x_devtools/solvers/advent_of_code/advent_of_code_solver.dart'; | ||
|
||
abstract class AdventOfCode2023Solver extends AdventOfCodeSolver { | ||
|
||
@override | ||
final int yearNumber = 2023; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import 'package:collection/collection.dart'; | ||
import 'package:h3x_devtools/solvers/advent_of_code/2023/advent_of_code_2023_solver.dart'; | ||
|
||
class Day01Solver extends AdventOfCode2023Solver { | ||
|
||
@override | ||
final int dayNumber = 1; | ||
|
||
static const Map<String, int> _digitNames = { | ||
'one': 1, | ||
'two': 2, | ||
'three': 3, | ||
'four': 4, | ||
'five': 5, | ||
'six': 6, | ||
'seven': 7, | ||
'eight': 8, | ||
'nine': 9, | ||
}; | ||
|
||
@override | ||
String getSolution(String input) { | ||
List<String> rawCalibrationLines = input | ||
.split('\n') | ||
.where((rawCalibrationLine) => rawCalibrationLine.isNotEmpty) | ||
.toList(); | ||
|
||
// Part 1 | ||
int totalCalibrationValuePart1 = 0; | ||
for (String line in rawCalibrationLines) { | ||
List<int> lineCodeUnits = line.codeUnits; | ||
|
||
int? firstCodeUnit = lineCodeUnits.firstWhereOrNull((element) => element >= 48 && element <= 57); | ||
int? lastCodeUnit = lineCodeUnits.lastWhereOrNull((element) => element >= 48 && element <= 57); | ||
if (firstCodeUnit != null && lastCodeUnit != null) { | ||
totalCalibrationValuePart1 += (firstCodeUnit - 48) * 10 + (lastCodeUnit - 48); | ||
} | ||
} | ||
|
||
// Part 2 | ||
int totalCalibrationValuePart2 = 0; | ||
for (String line in rawCalibrationLines) { | ||
int? firstDigit, lastDigit; | ||
|
||
for (int i = 0; i < line.length; i++) { | ||
// First check if there's a digit here | ||
int codeUnit = line.codeUnits[i]; | ||
if (codeUnit >= 48 && codeUnit <= 57) { | ||
firstDigit ??= codeUnit - 48; | ||
lastDigit = codeUnit - 48; | ||
continue; | ||
} | ||
|
||
// If not check if there's a text digit here | ||
for (String digitName in _digitNames.keys) { | ||
if (line.substring(i).startsWith(digitName)) { | ||
firstDigit ??= _digitNames[line.substring(i, i + digitName.length)]; | ||
lastDigit = _digitNames[line.substring(i, i + digitName.length)]; | ||
} | ||
} | ||
} | ||
|
||
totalCalibrationValuePart2 += firstDigit! * 10 + lastDigit!; | ||
} | ||
|
||
return 'Total calibration value: $totalCalibrationValuePart1\nTotal correct calibration value: $totalCalibrationValuePart2'; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters