Skip to content

Commit

Permalink
Day13a: Solved
Browse files Browse the repository at this point in the history
  • Loading branch information
julemand101 committed Dec 25, 2024
1 parent 9357695 commit fe7ce1e
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 1 deletion.
53 changes: 53 additions & 0 deletions lib/day13.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// --- Day 13: Claw Contraption ---
// https://adventofcode.com/2024/day/13

import 'dart:math';

import 'package:collection/collection.dart';

final buttonRegExp = RegExp(r'X\+(\d+), Y\+(\d+)');
final prizeRegExp = RegExp(r'X=(\d+), Y=(\d+)');

const costAButton = 3;
const costBButton = 1;

int solveA(Iterable<String> input) {
var sum = 0;

for (final chunk in input
.where((line) => line.isNotEmpty)
.splitBefore((line) => line.startsWith('Button A:'))) {
final [ax, ay] = [
...buttonRegExp
.firstMatch(chunk[0])!
.groups(const [1, 2]).map((s) => int.parse(s!))
];
final [bx, by] = [
...buttonRegExp
.firstMatch(chunk[1])!
.groups(const [1, 2]).map((s) => int.parse(s!))
];
final [px, py] = [
...prizeRegExp
.firstMatch(chunk[2])!
.groups(const [1, 2]).map((s) => int.parse(s!))
];

var cost = 0;
for (var a = 1; a <= 100; a++) {
for (var b = 1; b <= 100; b++) {
if ((a * ax) + (b * bx) == px && (a * ay) + (b * by) == py) {
if (cost == 0) {
cost = (a * costAButton) + (b * costBButton);
} else {
cost = min(cost, (a * costAButton) + (b * costBButton));
}
}
}
}

sum += cost;
}

return sum;
}
2 changes: 1 addition & 1 deletion test/data
Submodule data updated from e590e5 to 045569
79 changes: 79 additions & 0 deletions test/day13_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// --- Day 13: Claw Contraption ---
// https://adventofcode.com/2024/day/13

import 'dart:io';
import 'package:advent_of_code_2024/day13.dart';
import 'package:advent_of_code_2024/util.dart';
import 'package:test/test.dart';

final input = File('test/data/day13.txt').readAsLinesSync();

void main() {
group('Part One', () {
test('Example 1', () {
expect(
solveA(r'''
Button A: X+94, Y+34
Button B: X+22, Y+67
Prize: X=8400, Y=5400
'''
.asLines),
equals(280));
});
test('Example 2', () {
expect(
solveA(r'''
Button A: X+26, Y+66
Button B: X+67, Y+21
Prize: X=12748, Y=12176
'''
.asLines),
equals(0));
});
test('Example 3', () {
expect(
solveA(r'''
Button A: X+17, Y+86
Button B: X+84, Y+37
Prize: X=7870, Y=6450
'''
.asLines),
equals(200));
});
test('Example 4', () {
expect(
solveA(r'''
Button A: X+69, Y+23
Button B: X+27, Y+71
Prize: X=18641, Y=10279
'''
.asLines),
equals(0));
});
test('Example 5', () {
expect(
solveA(r'''
Button A: X+94, Y+34
Button B: X+22, Y+67
Prize: X=8400, Y=5400
Button A: X+26, Y+66
Button B: X+67, Y+21
Prize: X=12748, Y=12176
Button A: X+17, Y+86
Button B: X+84, Y+37
Prize: X=7870, Y=6450
Button A: X+69, Y+23
Button B: X+27, Y+71
Prize: X=18641, Y=10279
'''
.asLines),
equals(480));
});
test('Solution', () {
expect(solveA(input), equals(36571));
});
});
}

0 comments on commit fe7ce1e

Please sign in to comment.