Day 3 python part 1

This commit is contained in:
rain
2023-12-06 12:34:03 -06:00
parent c9d543c416
commit c458de3ada
4 changed files with 183 additions and 0 deletions
+46
View File
@@ -1,3 +1,7 @@
Part 1: `543867`
Part 2: ``
[--- Day 3: Gear Ratios --- [--- Day 3: Gear Ratios ---
-------------------------- --------------------------
@@ -43,4 +47,46 @@ In this schematic, two numbers are *not* part numbers because they are not adjac
Of course, the actual engine schematic is much larger. *What is the sum of all of the part numbers in the engine schematic?* Of course, the actual engine schematic is much larger. *What is the sum of all of the part numbers in the engine schematic?*
, --- Part Two ---
----------------
The engineer finds the missing part and installs it in the engine! As the engine springs to life, you jump in the closest gondola, finally ready to ascend to the water source.
You don't seem to be going very fast, though. Maybe something is still wrong? Fortunately, the gondola has a phone labeled "help", so you pick it up and the engineer answers.
Before you can explain the situation, she suggests that you look out the window. There stands the engineer, holding a phone in one hand and waving with the other. You're going so slowly that you haven't even left the station. You exit the gondola.
The missing part wasn't the only issue - one of the gears in the engine is wrong. A *gear* is any `*` symbol that is adjacent to *exactly two part numbers*. Its *gear ratio* is the result of multiplying those two numbers together.
This time, you need to find the gear ratio of every gear and add them all up so that the engineer can figure out which gear needs to be replaced.
Consider the same engine schematic again:
```
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
```
In this schematic, there are *two* gears. The first is in the top left; it has part numbers `467` and `35`, so its gear ratio is `16345`. The second gear is in the lower right; its gear ratio is `451490`. (The `*` adjacent to `617` is *not* a gear because it is only adjacent to one part number.) Adding up all of the gear ratios produces `*467835*`.
*What is the sum of all of the gear ratios in your engine schematic?*
] ]
+67
View File
@@ -0,0 +1,67 @@
with open("../input.txt", "r") as fp:
inputs = [line.rstrip() for line in fp]
def getReplaceNumber(y,x):
if not inputs[y][x].isdigit() or inputs[y][x] == ".":
return 0
num = [inputs[y][x]]
xSave = x
# Left
x-=1
while x >= 0 and inputs[y][x].isdigit():
num.insert(0,inputs[y][x])
tmp = list(inputs[y])
tmp[x] = '.'
inputs[y] = ''.join(tmp)
x-=1
# Right
x = xSave + 1
while x < len(inputs[y]) and inputs[y][x].isdigit():
num += inputs[y][x]
tmp = list(inputs[y])
tmp[x] = '.'
inputs[y] = ''.join(tmp)
x+=1
return int(''.join(num))
def findNumbers(y, x):
if inputs[y][x].isdigit() or inputs[y][x] == ".":
return 0
sum = 0
# N
if y-1>=0:
sum += getReplaceNumber(y-1, x)
# NW
if x-1>=0:
sum += getReplaceNumber(y-1, x-1)
# NE
if x+1<len(inputs[y]):
sum += getReplaceNumber(y-1, x+1)
# S
if y+1<len(inputs):
sum += getReplaceNumber(y+1, x)
# SW
if x-1>=0:
sum += getReplaceNumber(y+1, x-1)
# SE
if x+1<len(inputs[y]):
sum += getReplaceNumber(y+1, x+1)
# W
if x-1>=0:
sum += getReplaceNumber(y, x-1)
# E
if x+1<len(inputs[y]):
sum += getReplaceNumber(y, x+1)
return sum
finalSum = 0
for y in range(0,len(inputs)):
for x in range(0,len(inputs[y])):
finalSum += findNumbers(y,x)
print(finalSum)
+68
View File
@@ -0,0 +1,68 @@
[--- Day 6: Wait For It ---
--------------------------
The ferry quickly brings you across Island Island. After asking around, you discover that there is indeed normally a large pile of sand somewhere near here, but you don't see anything besides lots of water and the small island where the ferry has docked.
As you try to figure out what to do next, you notice a poster on a wall near the ferry dock. "Boat races! Open to the public! Grand prize is an all-expenses-paid trip to *Desert Island*!" That must be where the sand comes from! Best of all, the boat races are starting in just a few minutes.
You manage to sign up as a competitor in the boat races just in time. The organizer explains that it's not really a traditional race - instead, you will get a fixed amount of time during which your boat has to travel as far as it can, and you win if your boat goes the farthest.
As part of signing up, you get a sheet of paper (your puzzle input) that lists the *time* allowed for each race and also the best *distance* ever recorded in that race. To guarantee you win the grand prize, you need to make sure you *go farther in each race* than the current record holder.
The organizer brings you over to the area where the boat races are held. The boats are much smaller than you expected - they're actually *toy boats*, each with a big button on top. Holding down the button *charges the boat*, and releasing the button *allows the boat to move*. Boats move faster if their button was held longer, but time spent holding the button counts against the total race time. You can only hold the button at the start of the race, and boats don't move until the button is released.
For example:
```
Time: 7 15 30
Distance: 9 40 200
```
This document describes three races:
* The first race lasts 7 milliseconds. The record distance in this race is 9 millimeters.
* The second race lasts 15 milliseconds. The record distance in this race is 40 millimeters.
* The third race lasts 30 milliseconds. The record distance in this race is 200 millimeters.
Your toy boat has a starting speed of *zero millimeters per millisecond*. For each whole millisecond you spend at the beginning of the race holding down the button, the boat's speed increases by *one millimeter per millisecond*.
So, because the first race lasts 7 milliseconds, you only have a few options:
* Don't hold the button at all (that is, hold it for *`0` milliseconds*) at the start of the race. The boat won't move; it will have traveled *`0` millimeters* by the end of the race.
* Hold the button for *`1` millisecond* at the start of the race. Then, the boat will travel at a speed of `1` millimeter per millisecond for 6 milliseconds, reaching a total distance traveled of *`6` millimeters*.
* Hold the button for *`2` milliseconds*, giving the boat a speed of `2` millimeters per millisecond. It will then get 5 milliseconds to move, reaching a total distance of *`10` millimeters*.
* Hold the button for *`3` milliseconds*. After its remaining 4 milliseconds of travel time, the boat will have gone *`12` millimeters*.
* Hold the button for *`4` milliseconds*. After its remaining 3 milliseconds of travel time, the boat will have gone *`12` millimeters*.
* Hold the button for *`5` milliseconds*, causing the boat to travel a total of *`10` millimeters*.
* Hold the button for *`6` milliseconds*, causing the boat to travel a total of *`6` millimeters*.
* Hold the button for *`7` milliseconds*. That's the entire duration of the race. You never let go of the button. The boat can't move until you let go of the button. Please make sure you let go of the button so the boat gets to move. *`0` millimeters*.
Since the current record for this race is `9` millimeters, there are actually `*4*` different ways you could win: you could hold the button for `2`, `3`, `4`, or `5` milliseconds at the start of the race.
In the second race, you could hold the button for at least `4` milliseconds and at most `11` milliseconds and beat the record, a total of `*8*` different ways to win.
In the third race, you could hold the button for at least `11` milliseconds and no more than `19` milliseconds and still beat the record, a total of `*9*` ways you could win.
To see how much margin of error you have, determine the *number of ways you can beat the record* in each race; in this example, if you multiply these values together, you get `*288*` (`4` \* `8` \* `9`).
Determine the number of ways you could beat the record in each race. *What do you get if you multiply these numbers together?*
]
+2
View File
@@ -0,0 +1,2 @@
Time: 38 94 79 70
Distance: 241 1549 1074 1091