Skip to content

Commit

Permalink
Initialize repo with test from 2023
Browse files Browse the repository at this point in the history
  • Loading branch information
julemand101 committed Nov 28, 2024
1 parent 3636b2a commit 1e30c59
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 2 deletions.
12 changes: 12 additions & 0 deletions .gitignore
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
3 changes: 3 additions & 0 deletions .gitmodules
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
2 changes: 0 additions & 2 deletions README.md

This file was deleted.

7 changes: 7 additions & 0 deletions analysis_options.yaml
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
57 changes: 57 additions & 0 deletions lib/day01.dart
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;
6 changes: 6 additions & 0 deletions lib/util.dart
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);
}
13 changes: 13 additions & 0 deletions pubspec.yaml
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
1 change: 1 addition & 0 deletions test/data
Submodule data added at 03d16c
47 changes: 47 additions & 0 deletions test/day01_test.dart
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));
});
});
}
64 changes: 64 additions & 0 deletions util/prepare_day.dart
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));
});
});
}
''',
);
}

0 comments on commit 1e30c59

Please sign in to comment.