Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

I'm trying to solve, Advent of Code Day 17 Part 2. I get the correct answer to Part 1 but when i modified to solve P2 I get the wrong answer for input

$
0
0

I'm a bit annoyed as to why my answer is wrong because, it passes the outputs for the test cases they have provided. Here is the link to the problem https://adventofcode.com/2023/day/17

Can anyone try to suggest me what I'm doing wrong here. I really can't figure out what I'm doing wrong since everything seems to be correct.

from copy import deepcopyfrom heapq import heappush, heappoppInput = [    [int(char) for char in list(line.replace("\n", ""))]    for line in open("input.txt", "r")]width, height = len(pInput[0]), len(pInput)def maxStepsReached(steps):    return steps > 10def isOOB(y, x):    return not (0 <= y < len(pInput) and 0 <= x < len(pInput[0]))def expand_helper(node, dy, dx, dir, res):    new_y = node[1] + dy    new_x = node[2] + dx    if isOOB(new_y, new_x) or dir == node[3] and maxStepsReached(node[4] + 1):        return res    if dir == node[3]:        res.append((node[0] + pInput[new_y][new_x], new_y, new_x, dir, node[4] + 1))    elif node[4] >= 4:        res.append((node[0] + pInput[new_y][new_x], new_y, new_x, dir, 1))    return resdef expand(node):    res = []    if node[3] == ">":        for dy, dx, dir in [[0, 1, ">"], [-1, 0, "A"], [1, 0, "V"]]:            res = expand_helper(node, dy, dx, dir, res)    elif node[3] == "<":        for dy, dx, dir in [[0, -1, "<"], [-1, 0, "A"], [1, 0, "V"]]:            res = expand_helper(node, dy, dx, dir, res)    elif node[3] == "A":        for dy, dx, dir in [[-1, 0, "A"], [0, 1, ">"], [0, -1, "<"]]:            res = expand_helper(node, dy, dx, dir, res)    elif node[3] == "V":        for dy, dx, dir in [[1, 0, "V"], [0, 1, ">"], [0, -1, "<"]]:            res = expand_helper(node, dy, dx, dir, res)    return resdef ucs():    closed = set()    open = []    heappush(open, (0, 0, 0, ">", 1))    while open:        node = heappop(open)        if node[1] == height - 1 and node[2] == width - 1 and node[4] >= 4:            return node[0]        if (node[1], node[2], node[3], node[4]) in closed:            continue        closed.add((node[1], node[2], node[3], node[4]))        successors = expand(node)        for s in successors:            heappush(open, s)    return -1print(ucs())

Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>