Uiua
I spent a while thinking about how to best do a flood fill in Uiua when I saw that β
(partition) works beautifully with multidimensional markers: "Groups are formed from markers that are adjacent along any axis.", meaning I just had to convert all letters into numbers and I'd get all indices belonging to a field into an array.
For part 2, I cheated a bit by coming here and reading that you only need to count the edges. To my surprise, the second part is actually a bit faster than part 1. Takes less than 0.2 seconds each though :D
Run with example input here
$ RRRRIICCFF
$ RRRRIICCCF
$ VVRRRCCFFF
$ VVRCCCJFFF
$ VVVVCJJCFE
$ VVIVCCJJEE
$ VVIIICJJEE
$ MIIIIIJJEE
$ MIIISIJEEE
$ MMMISSJEEE
.
N β +[0_Β―1 0_1 Β―1_0 1_0]
Areas β ββ‘:β‘β³.+1βββ
Peri β -/+β‘(/+βNΒ€)βΒ€β(Γ4⧻)
Sides β (
β(-Β€)β―:β½β0ΓΒ°β.+2β΅βΈ-+1ββ£β’βΈβββ‘β
⧻ββΈβ1_3β§(/+/+)2_2.ββ‘=β+1:
+β(Γ2/+/+β§(β[[1_0 0_1][0_1 1_0]])2_2β)
)
Cost! β /+β‘β(Γ^0β⧻)
PartOne β (
# &rs β &fo "input-12.txt"
βββ @\n.
Cost!Peri Areas
)
PartTwo β (
# &rs β &fo "input-12.txt"
βββ @\n.
Cost!Sides Areas
)
&p "Day 12:"
&pf "Part 1: "
&p PartOne
&pf "Part 2: "
&p PartTwo