Skip to content

Commit

Permalink
Day01: Solved
Browse files Browse the repository at this point in the history
  • Loading branch information
julemand101 committed Dec 1, 2024
1 parent 7a7aa99 commit e6783b3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 74 deletions.
90 changes: 36 additions & 54 deletions lib/day01.dart
Original file line number Diff line number Diff line change
@@ -1,57 +1,39 @@
// --- Day 1: Trebuchet?! ---
// https://adventofcode.com/2023/day/1
// --- Day 1: Historian Hysteria ---
// https://adventofcode.com/2024/day/1

import 'package:collection/collection.dart';

int solveA(Iterable<String> input) =>
input.map((line) => line.split('')).map((charList) {
final firstDigit = charList.map(int.tryParse).nonNulls.first;
final lastDigit = charList.reversed.map(int.tryParse).nonNulls.first;

return firstDigit * 10 + lastDigit;
}).sum;

const List<String> numberStrings = [
'zero', // Added so index position in list matches the number as String
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
];

int solveB(Iterable<String> input) => input.map((line) {
int? firstDigit;
int? secondDigit;

for (var i = 0; i < line.length; i++) {
int? parsed = int.tryParse(line[i]);

if (parsed != null) {
if (firstDigit == null) {
firstDigit = parsed;
} else {
secondDigit = parsed;
}
} else {
for (final (int value, String string) in numberStrings.indexed) {
if (i + string.length - 1 < line.length &&
line.substring(i, i + string.length) == string) {
if (firstDigit == null) {
firstDigit = value;
} else {
secondDigit = value;
}
}
}
}
}

secondDigit ??= firstDigit;

return int.parse('$firstDigit$secondDigit');
}).sum;
int solveA(Iterable<String> input) {
final listA = <int>[];
final listB = <int>[];

for (final line in input) {
final [valueA, valueB] = line.split(' ');
listA.add(int.parse(valueA));
listB.add(int.parse(valueB));
}

listA.sort();
listB.sort();

var distanceSum = 0;

for (final (index, valueA) in listA.indexed) {
distanceSum += (valueA - listB[index]).abs();
}

return distanceSum;
}

int solveB(Iterable<String> input) {
final listA = <int>[];
final countMapB = <int, int>{};

for (final line in input) {
final [valueA, valueB] = line.split(' ');
listA.add(int.parse(valueA));
countMapB.update(int.parse(valueB), (i) => i + 1, ifAbsent: () => 1);
}

return listA.map((valueA) => valueA * (countMapB[valueA] ?? 0)).sum;
}
2 changes: 1 addition & 1 deletion test/data
Submodule data updated from 03d16c to c7941e
39 changes: 20 additions & 19 deletions test/day01_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// --- Day 1: Trebuchet?! ---
// https://adventofcode.com/2023/day/1
// --- Day 1: Historian Hysteria ---
// https://adventofcode.com/2024/day/1

import 'dart:io';
import 'package:advent_of_code_2024/day01.dart';
Expand All @@ -12,36 +12,37 @@ void main() {
group('Part One', () {
test('Example 1', () {
expect(
solveA('''
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
solveA(r'''
3 4
4 3
2 5
1 3
3 9
3 3
'''
.asLines),
equals(142));
equals(11));
});
test('Solution', () {
expect(solveA(input), equals(53974));
expect(solveA(input), equals(1341714));
});
});
group('Part Two', () {
test('Example 1', () {
expect(
solveB('''
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
solveB(r'''
3 4
4 3
2 5
1 3
3 9
3 3
'''
.asLines),
equals(281));
equals(31));
});
test('Solution', () {
expect(solveB(input), equals(52840));
expect(solveB(input), equals(27384707));
});
});
}

0 comments on commit e6783b3

Please sign in to comment.