diff --git a/lib/main.dart b/lib/main.dart index 13780a1..9e02698 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -3,6 +3,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:h3x_devtools/solvers/advent_of_code/2021/_all_solvers.dart' as aoc2021; import 'package:h3x_devtools/solvers/advent_of_code/2022/_all_solvers.dart' as aoc2022; +import 'package:h3x_devtools/solvers/advent_of_code/2023/_all_solvers.dart' as aoc2023; import 'package:h3x_devtools/solvers/solver.dart'; import 'package:h3x_devtools/views/overlay_effects.dart'; import 'package:h3x_devtools/views/problem_solver_view.dart'; @@ -70,6 +71,7 @@ class _MyAppState extends State { ), _adventOfCode2021PaneItem, _adventOfCode2022PaneItem, + _adventOfCode2023PaneItem, _overlayEffectsItem, ]; } @@ -108,6 +110,17 @@ class _MyAppState extends State { ); } + PaneItemExpander get _adventOfCode2023PaneItem { + return PaneItemExpander( + icon: const Icon(FluentIcons.code), + title: const Text('Advent of Code 2023'), + body: const Center(child: Text("Choose a day from the sub menu")), + items: [ + _getAdventOfCodePaneItem(2021, 01, aoc2023.Day01Solver()), + ], + ); + } + PaneItem _getAdventOfCodePaneItem(int year, int day, Solver solver) { String dayString = _dayNumberFormat.format(day); diff --git a/lib/solvers/advent_of_code/2023/_all_solvers.dart b/lib/solvers/advent_of_code/2023/_all_solvers.dart new file mode 100644 index 0000000..0732502 --- /dev/null +++ b/lib/solvers/advent_of_code/2023/_all_solvers.dart @@ -0,0 +1 @@ +export 'day_01_solver.dart'; diff --git a/lib/solvers/advent_of_code/2023/advent_of_code_2023_solver.dart b/lib/solvers/advent_of_code/2023/advent_of_code_2023_solver.dart new file mode 100644 index 0000000..81ec187 --- /dev/null +++ b/lib/solvers/advent_of_code/2023/advent_of_code_2023_solver.dart @@ -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; + +} diff --git a/lib/solvers/advent_of_code/2023/day_01_solver.dart b/lib/solvers/advent_of_code/2023/day_01_solver.dart new file mode 100644 index 0000000..84422c2 --- /dev/null +++ b/lib/solvers/advent_of_code/2023/day_01_solver.dart @@ -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 _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 rawCalibrationLines = input + .split('\n') + .where((rawCalibrationLine) => rawCalibrationLine.isNotEmpty) + .toList(); + + // Part 1 + int totalCalibrationValuePart1 = 0; + for (String line in rawCalibrationLines) { + List 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'; + } + +} diff --git a/pubspec.lock b/pubspec.lock index 282245d..e39a3ed 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -138,7 +138,7 @@ packages: source: hosted version: "4.8.0" collection: - dependency: transitive + dependency: "direct main" description: name: collection sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a diff --git a/pubspec.yaml b/pubspec.yaml index 7661fa2..2cf1a40 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -35,6 +35,7 @@ dependencies: sealed_unions: 3.0.3+1 # Collections and querying + collection: 1.18.0 fast_immutable_collections: 9.2.0 darq: 2.0.0