🎄 Advent of Code 2025 – Day 10 Complete Wrapped up Day 10 of Advent of Code. This one was a fascinating journey from brute force to constraint solving, teaching an important lesson about recognizing problem patterns. 𝗧𝗵𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 The input describes machines with buttons that toggle lights and reduce joltage values. 𝗣𝗮𝗿𝘁 1: Find the minimum button presses to toggle specific lights from off to a target pattern using XOR operations. 𝗣𝗮𝗿𝘁 2: Find the minimum button presses to reduce all joltage values from their initial state to zero, where each button press decrements specific positions. 𝗪𝗵𝗮𝘁 𝗠𝗮𝗱𝗲 𝗜𝘁 𝗜𝗻𝘁𝗲𝗿𝗲𝘀𝘁𝗶𝗻𝗴 Part 1 is a bitwise XOR problem solvable through combinatorial brute force. Each button toggles specific bit positions, and we need to find which combination produces the target state. Part 2 initially looked like a search problem, but attempting BFS/DFS leads to exponential state explosion and impossibly long runtimes. The key insight: Part 2 is actually a linear programming problem! Each button press creates a linear equation, and we need to minimize the sum of button presses while satisfying all constraints. This transforms an intractable search problem into one solvable instantly using the z3 constraint solver. This problem perfectly demonstrates how the same surface-level problem can require completely different algorithmic approaches. Recognizing whether you're dealing with search, optimization, or constraint satisfaction can make the difference between a solution that runs in milliseconds versus one that never finishes. 𝗞𝗲𝘆 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆𝘀: Not every optimization problem requires exhaustive search Linear constraints → constraint solver (z3, OR-tools) Problem recognition is as important as implementation Sometimes the right tool makes the impossible trivial 🔗 Code Repository: https://lnkd.in/eT6542Ts #AdventOfCode #ProblemSolving #Algorithms #ConstraintSolving #Python #ComputationalThinking #SoftwareEngineering #Z3Solver
Advent of Code 2025 Day 10: Solving with Constraint Programming
More Relevant Posts
-
🎄 Advent of Code 2025 – Day 11 Complete Wrapped up Day 11 of Advent of Code. This one was an elegant demonstration of how memoization transforms exponential problems into tractable ones. 𝗧𝗵𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 The input describes a directed graph where each node points to its children, ultimately leading to an 'out' node. 𝗣𝗮𝗿𝘁 1: Count all possible paths from a starting node ('you') to the destination ('out'). 𝗣𝗮𝗿𝘁 2: Count only the paths that pass through specific required nodes ('dac' AND 'fft') before reaching the destination. 𝗪𝗵𝗮𝘁 𝗠𝗮𝗱𝗲 𝗜𝘁 𝗜𝗻𝘁𝗲𝗿𝗲𝘀𝘁𝗶𝗻𝗴 Part 1 is a classic recursive path counting problem. The naive approach recalculates the same subproblems repeatedly, leading to exponential time complexity. Adding @cache (memoization) transforms it into a dynamic programming solution that computes each node's result exactly once. Part 2 adds a twist: constraint tracking. We need to count only paths that satisfy certain conditions. The solution tracks which required nodes have been visited using a frozenset as part of the state space. This allows us to maintain memoization while ensuring we only count valid paths. The key insight: state space design matters. By using (node, visited_required_nodes) as our state, we keep the cache size manageable at O(nodes × 2^k) where k is the number of required nodes. 𝗞𝗲𝘆 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆𝘀: • Memoization: exponential → polynomial time complexity • State space design is crucial for efficient DP • Python's @cache decorator makes memoization trivial • Adding constraints requires careful state tracking • Frozensets enable hashable collections for caching 𝗣𝗲𝗿𝗳𝗼𝗿𝗺𝗮𝗻𝗰𝗲 𝗜𝗺𝗽𝗮𝗰𝘁: Without memoization: Would timeout on large graphs With memoization: Instant results ⚡ This problem perfectly illustrates why dynamic programming is one of the most powerful optimization techniques in computer science. 🔗 Code Repository: https://lnkd.in/eYRvn-mk #AdventOfCode #DynamicProgramming #Algorithms #GraphTheory #Python #Memoization #ComputationalThinking #SoftwareEngineering #ProblemSolving
To view or add a comment, sign in
-
-
🚀 LeetCode + DSA — Day 18 Today I solved the classic array problem “Remove Element”, which focuses on in-place array modification and pointer-based traversal. 🔹 Problem Overview Given an array of integers and a value val, the task is to remove all occurrences of val in-place and return the count of remaining elements. The relative order of elements can be changed, and no extra space should be used. 🔹 Approach Used ✔ Used a pointer to track the position for valid elements ✔ Traversed the array once ✔ Copied non-val elements forward ✔ Returned the final count of valid elements 🔹 Why This Works In-place modification avoids extra memory usage Single-pass traversal ensures optimal performance Pointer technique keeps the logic clean and efficient 📊 Performance ✅ All test cases passed (116/116) ⚡ Runtime: 0 ms (Beats 100%) 💾 Memory: Beats ~94% 💡 Key Takeaway In-place algorithms are essential for writing memory-efficient code. Simple pointer logic can often replace complex data structures when constraints are clear. Consistency over intensity — sharpening fundamentals one problem at a time 💪 #LeetCode #DSA #Python #Arrays #InPlaceAlgorithm #TwoPointers #CodingPractice #ProblemSolving #DailyCoding #TechGrowth #LearningJourney
To view or add a comment, sign in
-
-
🌟 Day 40 of my 100 Days LeetCode Challenge 📘 Problem: LeetCode 1143 – Longest Common Subsequence 🧩 Difficulty: Medium 📚 Concepts: Dynamic Programming, String DP, Optimization 💡 Problem Summary You’re given two strings text1 and text2. A subsequence is formed by deleting some characters (or none) without changing the order of the remaining characters. 👉 The task is to find the length of the longest subsequence common to both strings. If no common subsequence exists, return 0. ✅ Approach & Thought Process This is a classic Dynamic Programming problem. Key ideas: Compare characters from both strings one by one. If characters match, they can extend a common subsequence. If they don’t match, skip one character from either string and take the best possible result. To optimize: Instead of using a full 2D table, we can keep only two rows at a time. This reduces space usage while preserving correctness. The solution builds answers incrementally, reusing results from smaller subproblems. ⏱ Complexity Time: O(n × m), where n and m are the lengths of the two strings Space: O(min(n, m)) using rolling DP optimization Efficient enough for large inputs. ✨ Key Takeaways LCS is a foundational DP problem—mastering it helps with many string problems. Subsequence ≠ Substring (order matters, continuity doesn’t). Space optimization is often possible once the DP transition is clear. Appears frequently in interviews, competitive programming, and bioinformatics. Onward to Day 41 🚀 #Python #LeetCode #100DaysOfCode #DynamicProgramming #Strings #ProblemSolving
To view or add a comment, sign in
-
-
🐍 Why did my loop miss half the errors? It is the most frustrating kind of bug: The code doesn't crash. It doesn't throw an error. It just... walks right past the data you told it to delete. The Problem: In the latest episode of The Secret Life of Python, Timothy tries to "clean" a list of sensor data by removing negative numbers inside a for loop. He writes: if temp < 0: temperatures.remove(temp) He runs it. It deletes the first error... but mysteriously skips the second one. 👻 The Lesson: Margaret calls this "The Loophole." When you remove an item from a list, the entire floor shifts to the left. But the loop's internal pointer keeps marching forward. You are effectively pulling the floorboards out from under your own feet. We cover: ✅ The visual mechanics of index shifting (The "Disappearing Step"). ✅ The "Snapshot" fix using [:]. ✅ The "New World" approach using List Comprehensions. If you have ever tried to modify a list while iterating over it, you need to read this. 👉 Read the full story here: https://lnkd.in/gdbNgfGm #Python #Coding #Programming #SoftwareDevelopment
To view or add a comment, sign in
-
-
𝗘𝘃𝗲𝗿 𝗻𝗼𝘁𝗶𝗰𝗲𝗱 𝘀𝗼𝗺𝗲 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻𝘀 𝗼𝗻 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝘁𝗵𝗮𝘁 “𝗯𝗲𝗮𝘁 𝟭𝟬𝟬%”? __import__("atexit").register( lambda: open("display_runtime.txt", "w").write("0") ) Looks like a hacker trick. Feels smart. 𝗪𝗵𝗮𝘁 𝘁𝗵𝗶𝘀 𝗰𝗼𝗱𝗲 𝗮𝗰𝘁𝘂𝗮𝗹𝗹𝘆 𝗱𝗼𝗲𝘀 (𝗹𝗶𝗻𝗲 𝗯𝘆 𝗹𝗶𝗻𝗲): • 𝗮𝘁𝗲𝘅𝗶𝘁 → runs code when the program is about to exit • 𝗿𝗲𝗴𝗶𝘀𝘁𝗲𝗿(...) → hooks a function to run at the very end • 𝗼𝗽𝗲𝗻(...).𝘄𝗿𝗶𝘁𝗲("𝟬") → overwrites the file LeetCode reads to show runtime So when execution finishes, the runtime file gets replaced with 0. The algorithm didn’t get faster. Only the displayed number did. 𝗟𝗲𝘀𝘀𝗼𝗻: If you don’t understand how a metric is calculated, you’ll optimize the metric — not the system. #moxcode #Python #LeetCode #Engineering #SystemsThinking #LearningInPublic
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge – Day 3 Problem #1411: Number of Ways to Paint N × 3 Grid (Hard) Today’s problem was a good example of how dynamic programming + pattern recognition can turn a complex-looking problem into an efficient solution. 💡 Approach & Thought Process: Instead of thinking about the entire grid at once, I focused on how one row affects the next row. For a single row of 3 cells: There are two valid coloring patterns: Type A: All three colors are different Type B: The first and third cells have the same color By counting how many ways each pattern can transition to the next row: We can track just two values instead of the full grid Each new row updates these values using fixed transition formulas This avoids brute force and keeps the solution efficient even for large n This reduces the problem to a simple iterative DP update. ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) This problem reinforced an important lesson: 👉 When constraints are large, look for repeating patterns instead of brute force. 📌 Stay tuned for more daily problem-solving insights and DSA practice updates! #LeetCode #DailyCoding #DataStructures #Algorithms #DynamicProgramming #ProblemSolving #Python #DSA #LearningInPublic #SoftwareEngineering
To view or add a comment, sign in
-
-
🗓 Day 60 / 100 – #100DaysOfLeetCode 📌 Problem 1411: Number of Ways to Paint N × 3 Grid Today’s problem was a really good exercise in dynamic programming and pattern observation. The goal was to count the number of valid ways to paint an n × 3 grid using three colors, such that no two adjacent cells (horizontal or vertical) share the same color. 🧠 My Approach: Instead of tracking every possible coloring explicitly, I categorized each row into two patterns: 1️⃣ same – Rows where the 1st and 3rd cells have the same color (e.g., A B A) 2️⃣ diff – Rows where all three cells have different colors (e.g., A B C) For the first row: same = 6 diff = 6 Then for each subsequent row: A same pattern can be formed from: previous same rows in 3 ways previous diff rows in 2 ways A diff pattern can be formed from: previous same rows in 2 ways previous diff rows in 2 ways Using these transitions, I iterated row by row and applied modulo 10^9 + 7 to handle large values efficiently. 💡 Key Learning: This problem reinforced: ✔ breaking complex constraints into manageable states ✔ recognizing repeating patterns to optimize DP solutions ✔ reducing a large state space into just a few variables ✔ how mathematical transitions can drastically simplify implementation A perfect example of how thinking in patterns turns a hard-looking problem into a clean and elegant solution 🚀 #100DaysOfLeetCode #LeetCodeChallenge #Python #ProblemSolving #DynamicProgramming #DP #Combinatorics #Algorithms #DSA #CompetitiveProgramming #SoftwareEngineering #CodingJourney #DeveloperJourney #LearningInPublic #TechStudent #CareerGrowth #Programming #KeepLearning
To view or add a comment, sign in
-
-
🚀 Day 53 of #100DaysOfCode — Making Array Sum Divisible by K Hey everyone! 👋 Today’s challenge focused on a smart math-based optimization problem where the goal was to make the sum of an array divisible by a given number k using the minimum number of operations. 👨💻 What I practiced today: ✅ Understanding modulo (%) operations deeply ✅ Translating a problem into a simple mathematical observation ✅ Optimizing brute-force thinking into an O(n) solution ✅ Writing clean and minimal Python code 📌 Today’s Task: ✔ Given an integer array nums and an integer k ✔ In one operation, decrement any element by 1 ✔ Find the minimum operations required so that sum(nums) is divisible by k 🧠 Key Insight: If the sum of the array is S, then the minimum number of operations required is simply: S % k Because each operation reduces the sum by 1. 💡 Example: Input: nums = [3, 9, 7], k = 5 Sum = 19 → 19 % 5 = 4 Output: 4 ✨ Key Takeaway: Sometimes the best solution isn’t complex logic or loops — it’s recognizing a simple mathematical pattern. Mastering such observations can drastically reduce code complexity and runtime. #100DaysOfCode #Day53 #Python #LeetCode #ProblemSolving #MathInProgramming #CodingJourney #DSA #CleanCode
To view or add a comment, sign in
-
-
🗓 Day 59 / 100 – #100DaysOfLeetCode 📌 Problem 961: N-Repeated Element in Size 2N Array Today’s problem focused on identifying a repeating pattern in an array with well-defined constraints. The task was to find the element that appears n times in an array of size 2n, where all other elements are unique. 🧠 My Approach: I used a set-based approach to track elements as I traversed the array: Iterated through each element in the array. Stored visited elements in a set. If an element was already present in the set, that meant it was the repeated element → returned it immediately. This works efficiently because the problem guarantees exactly one element is repeated n times. 💡 Why this works well: Sets provide O(1) average time complexity for lookup. The repeated element is guaranteed to appear early due to frequency, so we can exit early. Simple logic with clean and readable code. 💡 Key Learning: This problem reinforced: ✔ how problem constraints can simplify the solution ✔ effective use of hash-based data structures ✔ recognizing patterns instead of overcomplicating logic ✔ writing efficient solutions even for “easy” problems A great example of how understanding constraints leads to elegant solutions 🚀 #100DaysOfLeetCode #LeetCodeChallenge #Python #ProblemSolving #Arrays #HashSet #Simulation #Algorithms #DSA #CompetitiveProgramming #SoftwareEngineering #CodingJourney #DeveloperJourney #LearningInPublic #TechStudent #CareerGrowth #Programming #KeepLearning
To view or add a comment, sign in
-
More from this author
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development