-
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.
- Loading branch information
1 parent
7a7aa99
commit e6783b3
Showing
3 changed files
with
57 additions
and
74 deletions.
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
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; | ||
} |
Submodule data
updated
from 03d16c to c7941e
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