diff options
-rw-r--r-- | 2023/05/solve.py | 86 |
1 files changed, 49 insertions, 37 deletions
diff --git a/2023/05/solve.py b/2023/05/solve.py index 965a81e..9872417 100644 --- a/2023/05/solve.py +++ b/2023/05/solve.py @@ -1,39 +1,51 @@ -L = open("input").read().strip().split("\n\n") +# I also wrote a solution in Haskell but it was much uglier than this.. +# (and didn't fully work) +L = open("input").read().strip().split("\n\n") -def part1(): - seeds = [] - mp = [] - - allI = [] - - for b in L: - ls = b.split("\n") - if ls[0].startswith("seeds:"): - seeds = [int(n) for n in ls[0].split(" ")[1:]] - continue - - mp.append({}) - curr = mp[len(mp) - 1] - for m in ls[1:]: - ms = [int(n) for n in m.split(" ")] - for off in range(ms[2]): - # curr[ms[1] + off] = ms[0] + off - curr[ms[0] + off] = ms[1] + off - allI += list(range(ms[0], ms[0] + ms[2])) - - # for i in range(len(mp) - 1, -1, -1): - mn = float("infinity") - res = 0 - for loc in allI: - curr = mp[-1].get(loc, loc) - for i in range(len(mp) - 2, -1, -1): - curr = mp[i].get(curr, curr) - print(loc, curr) - if curr in seeds and loc < mn: - mn = curr - res = loc - print(res) - - -part1() +p1 = float("infinity") +p2 = float("infinity") + +seeds = [] +mp = [] + +for b in L: + ls = b.split("\n") + if ls[0].startswith("seeds:"): + seeds = [int(n) for n in ls[0].split(" ")[1:]] + continue + + mp.append([]) + for m in ls[1:]: + mp[len(mp) - 1].append([int(n) for n in m.split(" ")]) + +# part 1 +for seed in seeds: + curr = seed + for m in mp: + for dest, source, inc in m: + if source <= curr < source + inc: + curr += dest - source + break + p1 = min(curr, p1) + +# part 2 +for i in range(0, len(seeds), 2): + start, length = seeds[i], seeds[i + 1] + seed = start + off = length + while seed <= start + length: + curr = seed + for m in mp: + for dest, source, inc in m: + if source <= curr < source + inc: + off = max(min(off, source + inc - curr), 1) + curr += dest - source + break + p2 = min(curr, p2) + seed += off + off = length - seed + start + + +print(p1) +print(p2) |