How I microwave food

September 20, 2024 (3mo ago)

When I microwave, I don’t press 1, 0, and 0 to set a one-minute timer. Instead, I press 6 and 0. This way, I can save a third of the energy used every time I microwave food for some time of less than or equal to 1 minute and 39 seconds. But the calculation doesn’t end here. There must be a way to determine the best sequence for entering a time on the microwave keypad.

Now, to determine the best sequence we have to calculate the distance between each button. First, we can convert the microwave keypad into a coordinate system:

The button 1 is positioned at (-1, 3).

The button 2 is positioned at (0, 3).

The button 3 is positioned at (1, 3).

The button 4 is positioned at (-1, 2).

The button 5 is positioned at (0, 2).

The button 6 is positioned at (1, 2).

The button 7 is positioned at (-1, 1).

The button 8 is positioned at (0, 1).

The button 9 is positioned at (1, 1).

The button 0 is positioned at (0, 0).

Next, to calculate the minimum distance, we can use the Euclidean distance formula:

import math
 
def dist(x: int, y: int):
    return math.sqrt(x**2 + y**2)

The total distance for a sequence would be calculated as the sum of individual differences. Next, when determining the optimized sequence for entering a specific time, there might be multiple ways to input the sequence. If the seconds component is less or equal to 39, there are potentially two options to represent the time:

Entering the time as MMSS, where M represents the minutes and S represents the seconds. Adjusting the sequence so that the total seconds fit within a single minute, which can be written as (MM-1)(SS+60).

For example, when computing 2 minutes and 39 seconds, the valid sequences are “239” and “199”.

If we calculate the distance travelled, “239” would yield a result of 3.0 units whereas “199” would yield a result of approximately 2.8284 units. This effectively saves about 5.7% of the original energy required.

Another example would include computing 5 minutes and 31 seconds, which has valid sequences “531” and “491”.

The sequence “531” has a distance of around 3.41 while the sequence “491” has a distance of around 5.06. That’s about 32.6% of the energy saved!

The algorithm has spoken!

# some python code I wrote in <5 minutes
# single digit inputs do not work, because why would you compute them anyways?
 
import math
 
keypad_coords = {
    '1': (-1, 3), '2': (0, 3), '3': (1, 3),
    '4': (-1, 2), '5': (0, 2), '6': (1, 2),
    '7': (-1, 1), '8': (0, 1), '9': (1, 1),
    '0': (0, 0)
}
 
def calculate_distance(b1, b2):
    x1, y1 = keypad_coords[b1]
    x2, y2 = keypad_coords[b2]
    return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
 
def total_distance(sequence):
    distance = 0
    for i in range(len(sequence) - 1):
        distance += calculate_distance(sequence[i], sequence[i + 1])
    return distance
    
def optimized_sequence(minutes, seconds):
    if seconds > 39:
        seq1 = f'{"" if minutes <= 0 else minutes}{seconds:02}'
        seq2 = None
    else:
        seq1 = f'{"" if minutes <= 0 else minutes}{seconds:02}'
        seq2 = None
        if minutes > 0:
            seq2 = f'{"" if minutes-1 <= 0 else minutes-1}{seconds + 60:02}'
 
    print("seq", seq1, seq2)
    dist1 = total_distance(seq1)
    dist2 = total_distance(seq2) if seq2 else float('inf')
 
    if dist1 <= dist2:
        return seq1, dist1, seq2, dist2
    else:
        return seq2, dist2, seq1, dist1
 
time_minutes = 1
time_seconds = 39
 
best_sequence, min_distance, worst_sequence, max_distance = optimized_sequence(time_minutes, time_seconds)
 
print(f"Best sequence for {time_minutes} minutes and {time_seconds} seconds: {best_sequence}")
print(f"Minimum distance: {min_distance}")
print("Worst sequence", worst_sequence)
print(f"Maximum distance: {max_distance}")
print("percent saved", (1-min_distance/max_distance) * 100)