Skip to content

Commit

Permalink
Merge pull request #40 from h3x4d3c1m4l/feature/aoc23-01
Browse files Browse the repository at this point in the history
Add AoC 2023 day 01 solver
  • Loading branch information
h3x4d3c1m4l authored Dec 1, 2023
2 parents 8e89e30 + efbbe3f commit 2b127e7
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -70,6 +71,7 @@ class _MyAppState extends State<MyApp> {
),
_adventOfCode2021PaneItem,
_adventOfCode2022PaneItem,
_adventOfCode2023PaneItem,
_overlayEffectsItem,
];
}
Expand Down Expand Up @@ -108,6 +110,17 @@ class _MyAppState extends State<MyApp> {
);
}

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);

Expand Down
1 change: 1 addition & 0 deletions lib/solvers/advent_of_code/2023/_all_solvers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'day_01_solver.dart';
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;

}
69 changes: 69 additions & 0 deletions lib/solvers/advent_of_code/2023/day_01_solver.dart
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';
}

}
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ packages:
source: hosted
version: "4.8.0"
collection:
dependency: transitive
dependency: "direct main"
description:
name: collection
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 2b127e7

Please sign in to comment.