-
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
Showing
12 changed files
with
1,417 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
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,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.
Empty file.
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 @@ | ||
Button A: X+94, Y+34 | ||
Button B: X+22, Y+67 | ||
Prize: X=8400, Y=5400 |
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 @@ | ||
Button A: X+26, Y+66 | ||
Button B: X+67, Y+21 | ||
Prize: X=12748, Y=12176 |
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 @@ | ||
Button A: X+17, Y+86 | ||
Button B: X+84, Y+37 | ||
Prize: X=7870, Y=6450 |
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 @@ | ||
Button A: X+69, Y+23 | ||
Button B: X+27, Y+71 | ||
Prize: X=18641, Y=10279 |
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,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 |
Oops, something went wrong.