Skip to content

Commit

Permalink
AoC 2024 day 13 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
loociano committed Dec 13, 2024
1 parent 1437bfb commit 007d33f
Show file tree
Hide file tree
Showing 12 changed files with 1,417 additions and 0 deletions.
Empty file added aoc2024/src/day13/__init__.py
Empty file.
Empty file.
66 changes: 66 additions & 0 deletions aoc2024/src/day13/python/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
import math
from typing import Sequence

type Vector = tuple[int, int]
type Machine = tuple[Vector, ...]
_BUTTON_A_COST = 3 # Tokens
_BUTTON_B_COST = 1 # Token
# Buttons will not be pressed more than 100 times.
_MAX_BUTTON_PRESSES = 100


def _parse(input: Sequence[str]) -> tuple[Machine, ...]:
part = 0
machines = []
machine = []
for i in range(len(input)):
if part == 0 and input[i] != '':
a_matches = re.match(r'Button A: X\+(\d+), Y\+(\d+)', input[i])
machine.append((int(a_matches[1]), int(a_matches[2])))
part = 1
elif part == 1:
b_matches = re.match(r'Button B: X\+(\d+), Y\+(\d+)', input[i])
machine.append((int(b_matches[1]), int(b_matches[2])))
part = 2
elif part == 2:
price_matches = re.match(r'Prize: X=(\d+), Y=(\d+)', input[i])
machine.append((int(price_matches[1]), int(price_matches[2])))
machines.append(tuple(machine))
machine = []
part = 0
return tuple(machines)


def _calc_tokens_brute_force(vector_0: Vector, vector_1: Vector, vector_2: Vector) -> int | None:
min_tokens = math.inf
for a in range(1, _MAX_BUTTON_PRESSES):
for b in range(1, _MAX_BUTTON_PRESSES):
if a * vector_0[0] + b * vector_1[0] == vector_2[0] and a * vector_0[1] + b * vector_1[1] == vector_2[1]:
tokens = a * _BUTTON_A_COST + b * _BUTTON_B_COST
if tokens < min_tokens:
min_tokens = tokens
return min_tokens if min_tokens < math.inf else None


def min_tokens_to_win(input: Sequence[str]) -> int | None:
claw_machines = _parse(input)
num_tokens = 0
for machine in claw_machines:
tokens = _calc_tokens_brute_force(machine[0], machine[1], machine[2])
if tokens is not None:
num_tokens += tokens
return num_tokens if num_tokens > 0 else None
Empty file added aoc2024/test/day13/__init__.py
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions aoc2024/test/day13/python/example1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Button A: X+94, Y+34
Button B: X+22, Y+67
Prize: X=8400, Y=5400
3 changes: 3 additions & 0 deletions aoc2024/test/day13/python/example2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Button A: X+26, Y+66
Button B: X+67, Y+21
Prize: X=12748, Y=12176
3 changes: 3 additions & 0 deletions aoc2024/test/day13/python/example3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Button A: X+17, Y+86
Button B: X+84, Y+37
Prize: X=7870, Y=6450
3 changes: 3 additions & 0 deletions aoc2024/test/day13/python/example4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Button A: X+69, Y+23
Button B: X+27, Y+71
Prize: X=18641, Y=10279
15 changes: 15 additions & 0 deletions aoc2024/test/day13/python/example5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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
Loading

0 comments on commit 007d33f

Please sign in to comment.