-
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
3636b2a
commit 1e30c59
Showing
10 changed files
with
210 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Files and directories created by pub | ||
.packages | ||
.pub/ | ||
build/ | ||
.dart_tool | ||
|
||
# Remove the following pattern if you wish to check in your lock file | ||
pubspec.lock | ||
|
||
# IntelliJ IDEA | ||
.idea/ | ||
*.iml |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "test/data"] | ||
path = test/data | ||
url = https://github.com/julemand101/AdventOfCode2024_data.git |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
include: package:lints/recommended.yaml | ||
|
||
analyzer: | ||
language: | ||
strict-casts: true | ||
strict-inference: true | ||
strict-raw-types: true |
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// --- Day 1: Trebuchet?! --- | ||
// https://adventofcode.com/2023/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; |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import 'dart:convert'; | ||
|
||
extension StringAsLinesExtension on String { | ||
Iterable<String> get asLines => LineSplitter.split(this); | ||
List<String> toLinesList() => asLines.toList(growable: false); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
name: advent_of_code_2023 | ||
description: Solutions for Advent of Code 2023. | ||
version: 0.0.1 | ||
|
||
environment: | ||
sdk: "^3.5.0" | ||
|
||
dependencies: | ||
collection: ^1.19.0 | ||
|
||
dev_dependencies: | ||
lints: ^5.0.0 | ||
test: ^1.25.0 |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// --- Day 1: Trebuchet?! --- | ||
// https://adventofcode.com/2023/day/1 | ||
|
||
import 'dart:io'; | ||
import 'package:advent_of_code_2023/day01.dart'; | ||
import 'package:advent_of_code_2023/util.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
final input = File('test/data/day01.txt').readAsLinesSync(); | ||
|
||
void main() { | ||
group('Part One', () { | ||
test('Example 1', () { | ||
expect( | ||
solveA(''' | ||
1abc2 | ||
pqr3stu8vwx | ||
a1b2c3d4e5f | ||
treb7uchet | ||
''' | ||
.asLines), | ||
equals(142)); | ||
}); | ||
test('Solution', () { | ||
expect(solveA(input), equals(53974)); | ||
}); | ||
}); | ||
group('Part Two', () { | ||
test('Example 1', () { | ||
expect( | ||
solveB(''' | ||
two1nine | ||
eightwothree | ||
abcone2threexyz | ||
xtwone3four | ||
4nineeightseven2 | ||
zoneight234 | ||
7pqrstsixteen | ||
''' | ||
.asLines), | ||
equals(281)); | ||
}); | ||
test('Solution', () { | ||
expect(solveB(input), equals(52840)); | ||
}); | ||
}); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import 'dart:io'; | ||
|
||
const year = '2024'; | ||
|
||
void main(List<String> args) { | ||
if (args.length != 2) { | ||
print('Please call with: <dayNumber> <dayTitle>'); | ||
return; | ||
} | ||
|
||
final [dayNumber, dayTitle] = args; | ||
final paddedDayNumber = dayNumber.padLeft(2, '0'); | ||
|
||
// Create lib file | ||
final dayFileName = 'day$paddedDayNumber.dart'; | ||
|
||
File('lib/$dayFileName').writeAsString( | ||
''' | ||
// --- Day $dayNumber: $dayTitle --- | ||
// https://adventofcode.com/$year/day/$dayNumber | ||
int solveA(Iterable<String> input) { | ||
return 0; | ||
} | ||
''', | ||
); | ||
|
||
// Create empty test data file | ||
final dataPath = 'test/data/day$paddedDayNumber.txt'; | ||
File(dataPath).create(); | ||
|
||
// Create unit tests | ||
File( | ||
'test/day${paddedDayNumber}_test.dart', | ||
).writeAsString( | ||
''' | ||
// --- Day $dayNumber: $dayTitle --- | ||
// https://adventofcode.com/$year/day/$dayNumber | ||
import 'dart:io'; | ||
import 'package:advent_of_code_$year/$dayFileName'; | ||
import 'package:advent_of_code_$year/util.dart'; | ||
import 'package:test/test.dart'; | ||
final input = File('$dataPath').readAsLinesSync(); | ||
void main() { | ||
group('Part One', () { | ||
test('Example 1', () { | ||
expect( | ||
solveA(r\'\'\' | ||
<SomeLines> | ||
\'\'\' | ||
.asLines), | ||
equals(-1)); | ||
}); | ||
test('Solution', () { | ||
expect(solveA(input), equals(-1)); | ||
}); | ||
}); | ||
} | ||
''', | ||
); | ||
} |