Skip to content

Commit

Permalink
Add AoC 2024 day 01 solver
Browse files Browse the repository at this point in the history
  • Loading branch information
h3x4d3c1m4l committed Dec 1, 2024
1 parent 94feba0 commit 0ab8495
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ When a commit has been pushed to the `main` branch, it gets deployed here: <http
- [Advent of Code 2021](https://adventofcode.com/2021)
- [Advent of Code 2022](https://adventofcode.com/2022)
- [Advent of Code 2023](https://adventofcode.com/2023)
- [Advent of Code 2024](https://adventofcode.com/2024)

## Features

Expand Down
6 changes: 6 additions & 0 deletions assets/advent_of_code/2024_01_sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3
1 change: 1 addition & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:go_router/go_router.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/advent_of_code/2024/_all_solvers.dart' as aoc2024;
import 'package:h3x_devtools/solvers/solver.dart';
import 'package:h3x_devtools/storage.dart';
import 'package:h3x_devtools/views/challenge_solver_view.dart';
Expand Down
13 changes: 13 additions & 0 deletions lib/main.panel_items.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ final List<NavigationPaneItem> _rootPaneItems = [
_adventOfCode2021PaneItem,
_adventOfCode2022PaneItem,
_adventOfCode2023PaneItem,
_adventOfCode2024PaneItem,
_overlayEffectsItem,
];

Expand Down Expand Up @@ -96,6 +97,18 @@ _RoutingPaneItemExpander get _adventOfCode2023PaneItem {
);
}

_RoutingPaneItemExpander get _adventOfCode2024PaneItem {
return _RoutingPaneItemExpander(
routePath: '/aoc2024',
icon: const Icon(FluentIcons.code),
title: const Text('Advent of Code 2024'),
routeBodyBuilder: (context, state) => const Center(child: Text("Choose a day from the sub menu")),
items: [
_getAdventOfCodePaneItem(2024, 01, aoc2024.Day01Solver()),
],
);
}

_RoutingPaneItem _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/2024/_all_solvers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'day_01_solver.dart';
12 changes: 12 additions & 0 deletions lib/solvers/advent_of_code/2024/aoc_2024_solver.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:h3x_devtools/solvers/advent_of_code/aoc_solver.dart';

export 'dart:math';

export '../../_solver_exports.dart';

abstract class AdventOfCode2024Solver extends AdventOfCodeSolver {

@override
final int yearNumber = 2024;

}
27 changes: 27 additions & 0 deletions lib/solvers/advent_of_code/2024/day_01_solver.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:h3x_devtools/solvers/advent_of_code/2024/aoc_2024_solver.dart';

class Day01Solver extends AdventOfCode2024Solver {

@override
final int dayNumber = 1;

@override
String getSolution(String input) {
List<List<int>> locationIdGroups = input.splitLines().map((line) => line.split(' ').parsedAsInts.toList()).toList();

// Part 1
List<int> list1Sorted = locationIdGroups.map((group) => group[0]).toList()..sort();
List<int> list2Sorted = locationIdGroups.map((group) => group[1]).toList()..sort();

int totalDistance = list1Sorted.foldIndexed(0, (index, previous, element) => previous + (element - list2Sorted[index]).abs());

// Part 2
List<int> list1 = locationIdGroups.map((group) => group[0]).toList();
List<int> list2 = locationIdGroups.map((group) => group[1]).toList();

double totalSimilarity = list1.foldIndexed(0, (index, previous, element) => previous + element * list2.countValueOccurances(element));

return 'Total distance: $totalDistance, total similarity: $totalSimilarity';
}

}
2 changes: 2 additions & 0 deletions lib/solvers/helpers/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ extension IterableHelpers<T> on Iterable<T> {

int gcdBy(int Function(T element) valueSelector) => map(valueSelector).gcd;

int countOccurances(bool Function(T element) valueSelector) => where(valueSelector).length;
int countValueOccurances(T value) => countOccurances((iterableValue) => iterableValue == value);
TNum sumBy<TNum extends num>(TNum Function(T element) valueSelector) => map(valueSelector).sum as TNum;
TNum minBy<TNum extends num>(TNum Function(T element) valueSelector) => map(valueSelector).min as TNum;
TNum maxBy<TNum extends num>(TNum Function(T element) valueSelector) => map(valueSelector).max as TNum;
Expand Down

0 comments on commit 0ab8495

Please sign in to comment.