solve Day09 Part1 in python
This commit is contained in:
parent
9c784cc6b7
commit
01927dee76
92
Day09/README.md
Normal file
92
Day09/README.md
Normal file
@ -0,0 +1,92 @@
|
||||
# Day 9: Mirage Maintenance ---
|
||||
## Part One
|
||||
You ride the camel through the sandstorm and stop where the ghost's maps told you to stop. The sandstorm subsequently subsides, somehow seeing you standing at an oasis!
|
||||
|
||||
The camel goes to get some water and you stretch your neck. As you look up, you discover what must be yet another giant floating island, this one made of metal! That must be where the parts to fix the sand machines come from.
|
||||
|
||||
There's even a hang glider partially buried in the sand here; once the sun rises and heats up the sand, you might be able to use the glider and the hot air to get all the way up to the metal island!
|
||||
|
||||
While you wait for the sun to rise, you admire the oasis hidden here in the middle of Desert Island. It must have a delicate ecosystem; you might as well take some ecological readings while you wait. Maybe you can report any environmental instabilities you find to someone so the oasis can be around for the next sandstorm-worn traveler.
|
||||
|
||||
You pull out your handy Oasis And Sand Instability Sensor and analyze your surroundings. The OASIS produces a report of many values and how they are changing over time (your puzzle input). Each line in the report contains the history of a single value. For example:
|
||||
```
|
||||
0 3 6 9 12 15
|
||||
1 3 6 10 15 21
|
||||
10 13 16 21 30 45
|
||||
```
|
||||
To best protect the oasis, your environmental report should include a prediction of the next value in each history. To do this, start by making a new sequence from the difference at each step of your history. If that sequence is not all zeroes, repeat this process, using the sequence you just generated as the input sequence. Once all of the values in your latest sequence are zeroes, you can extrapolate what the next value of the original history should be.
|
||||
|
||||
In the above dataset, the first history is 0 3 6 9 12 15. Because the values increase by 3 each step, the first sequence of differences that you generate will be 3 3 3 3 3. Note that this sequence has one fewer value than the input sequence because at each step it considers two numbers from the input. Since these values aren't all zero, repeat the process: the values differ by 0 at each step, so the next sequence is 0 0 0 0. This means you have enough information to extrapolate the history! Visually, these sequences can be arranged like this:
|
||||
```
|
||||
0 3 6 9 12 15
|
||||
3 3 3 3 3
|
||||
0 0 0 0
|
||||
```
|
||||
To extrapolate, start by adding a new zero to the end of your list of zeroes; because the zeroes represent differences between the two values above them, this also means there is now a placeholder in every sequence above it:
|
||||
```
|
||||
0 3 6 9 12 15 B
|
||||
3 3 3 3 3 A
|
||||
0 0 0 0 0
|
||||
```
|
||||
You can then start filling in placeholders from the bottom up. A needs to be the result of increasing 3 (the value to its left) by 0 (the value below it); this means A must be 3:
|
||||
```
|
||||
0 3 6 9 12 15 B
|
||||
3 3 3 3 3 3
|
||||
0 0 0 0 0
|
||||
```
|
||||
Finally, you can fill in B, which needs to be the result of increasing 15 (the value to its left) by 3 (the value below it), or 18:
|
||||
```
|
||||
0 3 6 9 12 15 18
|
||||
3 3 3 3 3 3
|
||||
0 0 0 0 0
|
||||
```
|
||||
So, the next value of the first history is 18.
|
||||
|
||||
Finding all-zero differences for the second history requires an additional sequence:
|
||||
```
|
||||
1 3 6 10 15 21
|
||||
2 3 4 5 6
|
||||
1 1 1 1
|
||||
0 0 0
|
||||
```
|
||||
Then, following the same process as before, work out the next value in each sequence from the bottom up:
|
||||
```
|
||||
1 3 6 10 15 21 28
|
||||
2 3 4 5 6 7
|
||||
1 1 1 1 1
|
||||
0 0 0 0
|
||||
```
|
||||
So, the next value of the second history is 28.
|
||||
|
||||
The third history requires even more sequences, but its next value can be found the same way:
|
||||
```
|
||||
10 13 16 21 30 45 68
|
||||
3 3 5 9 15 23
|
||||
0 2 4 6 8
|
||||
2 2 2 2
|
||||
0 0 0
|
||||
```
|
||||
So, the next value of the third history is 68.
|
||||
|
||||
If you find the next value for each history in this example and add them together, you get 114.
|
||||
|
||||
Analyze your OASIS report and extrapolate the next value for each history. What is the sum of these extrapolated values?
|
||||
|
||||
## Part Two
|
||||
Of course, it would be nice to have even more history included in your report. Surely it's safe to just extrapolate backwards as well, right?
|
||||
|
||||
For each history, repeat the process of finding differences until the sequence of differences is entirely zero. Then, rather than adding a zero to the end and filling in the next values of each previous sequence, you should instead add a zero to the beginning of your sequence of zeroes, then fill in new first values for each previous sequence.
|
||||
|
||||
In particular, here is what the third example history looks like when extrapolating back in time:
|
||||
```
|
||||
5 10 13 16 21 30 45
|
||||
5 3 3 5 9 15
|
||||
-2 0 2 4 6
|
||||
2 2 2 2
|
||||
0 0 0
|
||||
```
|
||||
Adding the new values on the left side of each sequence from bottom to top eventually reveals the new left-most history value: 5.
|
||||
|
||||
Doing this for the remaining example data above results in previous values of -3 for the first history and 0 for the second history. Adding all three new values together produces 2.
|
||||
|
||||
Analyze your OASIS report again, this time extrapolating the previous value for each history. What is the sum of these extrapolated values?
|
68
Day09/python/solution1.py
Normal file
68
Day09/python/solution1.py
Normal file
@ -0,0 +1,68 @@
|
||||
def generate_difference_table(history):
|
||||
"""Generates a difference table for a given history."""
|
||||
table = [history]
|
||||
while not all(value == 0 for value in table[-1]):
|
||||
try:
|
||||
next_row = [table[-1][i+1] - table[-1][i] for i in range(len(table[-1]) - 1)]
|
||||
except IndexError as e:
|
||||
print(f"IndexError in generate_difference_table: {e}")
|
||||
print(f"Current table: {table}")
|
||||
raise
|
||||
table.append(next_row)
|
||||
return table
|
||||
|
||||
def extrapolate_next_value(table):
|
||||
"""Extrapolates the next value from the difference table."""
|
||||
try:
|
||||
for i in range(len(table) - 2, -1, -1):
|
||||
table[i].append(table[i][-1] + table[i+1][-1])
|
||||
except IndexError as e:
|
||||
print(f"IndexError in extrapolate_next_value: {e}")
|
||||
print(f"Current table: {table}")
|
||||
raise
|
||||
return table[0][-1]
|
||||
|
||||
def solve_puzzle(filename):
|
||||
"""Solves the puzzle by reading histories from the file and summing their extrapolated next values."""
|
||||
total = 0
|
||||
try:
|
||||
with open(filename, 'r') as file:
|
||||
for line in file:
|
||||
try:
|
||||
history = [int(x) for x in line.split()]
|
||||
diff_table = generate_difference_table(history)
|
||||
next_value = extrapolate_next_value(diff_table)
|
||||
total += next_value
|
||||
except ValueError as e:
|
||||
print(f"ValueError processing line '{line}': {e}")
|
||||
except IndexError as e:
|
||||
print(f"IndexError processing line '{line}': {e}")
|
||||
except FileNotFoundError as e:
|
||||
print(f"FileNotFoundError: The file {filename} was not found.")
|
||||
raise
|
||||
except Exception as e:
|
||||
print(f"Unexpected error processing file {filename}: {e}")
|
||||
raise
|
||||
return total
|
||||
|
||||
def test():
|
||||
"""Runs the test using the test.txt file and asserts the expected outcome."""
|
||||
expected = 114 # Expected result from the test data
|
||||
try:
|
||||
result = solve_puzzle("../test.txt")
|
||||
assert result == expected, f"Test failed: Expected {expected}, got {result}"
|
||||
print("Test passed successfully.")
|
||||
except AssertionError as e:
|
||||
print(e)
|
||||
raise
|
||||
|
||||
def main():
|
||||
"""Main function to run the test and then solve the puzzle."""
|
||||
print("Running test...")
|
||||
test()
|
||||
print("Test completed. Solving the puzzle...")
|
||||
result = solve_puzzle("../input.txt")
|
||||
print(f"Puzzle result: {result}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
3
Day09/test.txt
Normal file
3
Day09/test.txt
Normal file
@ -0,0 +1,3 @@
|
||||
0 3 6 9 12 15
|
||||
1 3 6 10 15 21
|
||||
10 13 16 21 30 45
|
@ -40,7 +40,7 @@ I aim to complete each day's puzzle on the same day, but as with any challenge,
|
||||
| 06 | ✅ | ✅ | [Day06 README](/Day06/README.md) |
|
||||
| 07 | ✅ | ✅ | [Day07 README](/Day07/README.md) |
|
||||
| 08 | ✅ | ✅ | [Day08 README](/Day08/README.md) |
|
||||
| 09 | ❓ | ❓ | [Day09 README](/Day09/README.md) |
|
||||
| 09 | ✅ | ❓ | [Day09 README](/Day09/README.md) |
|
||||
| 10 | ❓ | ❓ | [Day10 README](/Day10/README.md) |
|
||||
| 11 | ❓ | ❓ | [Day11 README](/Day11/README.md) |
|
||||
| 12 | ❓ | ❓ | [Day12 README](/Day12/README.md) |
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2023",
|
||||
"target": "ES2022",
|
||||
"module": "commonjs",
|
||||
"strict": true,
|
||||
"esModuleInterop": true
|
||||
|
Loading…
Reference in New Issue
Block a user