The first part for this problem was pretty straight forward, simply calculate the erosion level for each region and transform that into region types before summing the ones in the desired region. Since you’re multiplying big integers, at some point there be overflow, but since you’re also doing modulo operations, you can work with modulo arithmetic from the start and only conserve the remainder when computing the erosion level of a region.

The second part was a little more complicated and took me quite some time more. I ended up with a distance matrix calculation where I would take the values around it depending on the tool and the region types. My original attempt got me numbers too low because I was switching tools regardless of the type of the previous region. My second attempt was too high because I didn’t extend my matrix enough. I ended up having to multiply the size of my map by 3 to get the correct solution.