mirror of
https://github.com/thib8956/advent-of-code.git
synced 2026-05-25 19:53:26 +00:00
2025 day 2 and 3
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env python3
|
||||
import fileinput
|
||||
|
||||
|
||||
def part1(inp):
|
||||
"Simple brute force solution"
|
||||
total = 0
|
||||
for line in inp:
|
||||
max_jolt = 0
|
||||
for i, a in enumerate(line):
|
||||
for b in line[i + 1 :]:
|
||||
max_jolt = max(max_jolt, int(a + b))
|
||||
total += max_jolt
|
||||
print("Part 1:", total)
|
||||
|
||||
|
||||
def find_max_joltage(inp):
|
||||
"""
|
||||
Greedy algorithm: pick 12 digits from the input to form the largest
|
||||
possible number, keeping the original order.
|
||||
"""
|
||||
stack = []
|
||||
for i, current in enumerate(inp):
|
||||
while stack != [] and current > stack[-1]:
|
||||
remaining_len = len(inp) - i
|
||||
if len(stack) + remaining_len <= 12:
|
||||
break
|
||||
stack.pop()
|
||||
stack.append(current)
|
||||
return "".join(stack[:12])
|
||||
|
||||
|
||||
def part2(inp):
|
||||
total = 0
|
||||
for line in inp:
|
||||
res = find_max_joltage(line)
|
||||
total += int(res)
|
||||
print("Part 2:", total)
|
||||
|
||||
|
||||
def main(inp):
|
||||
part1(inp)
|
||||
part2(inp)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
lines = [x.rstrip() for x in fileinput.input()]
|
||||
main(lines)
|
||||
Reference in New Issue
Block a user