-
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
9357695
commit fe7ce1e
Showing
3 changed files
with
133 additions
and
1 deletion.
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,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; | ||
} |
Submodule data
updated
from e590e5 to 045569
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,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)); | ||
}); | ||
}); | ||
} |