Took some time to clean this up a bit before posting it, though it could probably still stand to be made more succinct. I used the waypoint characters directly when solving this initially, but that made part 2 especially kind of ugly. Instead, I now convert the waypoints to integers (from 0 to 5), which as a bonus allows part 1 to be done more efficiently. Part 2 is handled by keeping track of how many of each waypoint remain, and repeatedly selecting the most optimal waypoint that's still available.
Solution (Lily)
var program: List[Integer] = []
var waypoints: Hash[Integer, Integer] = [
'r' => 0, 'o' => 1, 'y' => 2, 'g' => 3, 'b' => 4, 'v' => 5
]
for char in File.read_to_string("inputs/day_02.txt"): {
# The `+ 0` coerces `char` to an `Integer` (`Byte`s can't be `Hash` keys).
program.push(waypoints[char + 0])
}
var total_distance = 0
var current_waypoint = 0
var counts = [0, 0, 0, 0, 0, 0]
for waypoint in program: {
total_distance += (
waypoint <= current_waypoint ? waypoint + 6 : waypoint
) - current_waypoint
current_waypoint = waypoint
counts[waypoint] += 1
}
print("Part 1: {}".format(total_distance))
total_distance = 0
current_waypoint = 0
for _ in 1...program.size(): {
for distance in 6...1 by -1: {
var waypoint = current_waypoint - distance
if waypoint < 0: {
waypoint += 6
}
if counts[waypoint] > 0: {
total_distance += distance
current_waypoint = waypoint
counts[waypoint] -= 1
break
}
}
}
print("Part 2: {}".format(total_distance))
This is kind of brute-force and ugly, especially the Part 2 solution. I believe that it could be made a decent bit shorter, but oh well. Namely, I think there's a static ordering that is always optimal.
For part 2, you're correct that there's a static order. When the colour is the same, the step size is always 6, so you only need to maximize the step size when the colour changes. So if you can find the optimal ordering of 'roygbv', you've found the optimal ordering of the sorted list.
EDIT: I looked at your Reddit post and it seems like you've already figured this out.
When I started doing Part 2 for a moment I thought I was getting to reuse Day 01's code, but the ordering of instructions always been the same saved me from abusing BTreeMap a second time.
For part 2, I spotted that you just need the length of the string and which colours occur in order to calculate the furthest distance, but I couldn't figure out how to cleanly count the jumps for the occurring colours in one line without using my previous algorithm. I didn't consider that I could just assume that all the colours occur, due to the length of the string
I'm still working on part 2 but figured I'd share my part 1:
Both parts in Gleam!
Part 1
It doesn't feel super idiomatic maybe? I dunno, functional programming is fun but I think I don't do it enough so I always make unreadable code lol.
const ring_size = 6
/// Given a color in ROYGBV, return its position in the waypoitn loop.
fn waypoint(color: String) -> Result(Int, Nil) {
case color {
"r" -> Ok(0)
"o" -> Ok(1)
"y" -> Ok(2)
"g" -> Ok(3)
"b" -> Ok(4)
"v" -> Ok(5)
// Shouldn't really hit this
_ -> Error(Nil)
}
}
type Status {
Status(current_position: Int, total_distance_moved: Int)
}
fn input_to_program(input: String) -> Result(List(Int), Nil) {
string.split(input, on: "")
|> list.filter(fn(wp) {
wp |> string.trim() |> string.is_empty() |> bool.negate()
})
|> list.try_map(waypoint)
}
/// Part 1?
pub fn part1(input: String) -> Int {
// Convert the input into a program of waypoints
// Now that we have the program, calculate our steps through
let assert Ok(program) =
input
|> input_to_program()
program
|> list.fold(Status(0, 0), fn(acc, next_wp) {
// We just fold with an accumulator keeping our current_pos and total_pos as we go through
Status(
current_position: next_wp,
total_distance_moved: acc.total_distance_moved
+ case int.compare(next_wp, acc.current_position) {
order.Lt | order.Eq -> ring_size - acc.current_position + next_wp
order.Gt -> next_wp - acc.current_position
},
)
})
|> fn(status) { status.total_distance_moved }
}
Part 2
consume_sorted() is actually a noop right now because I got kinda stuck with looping in gleam :P butttt I realized if I just get the counts of the waypoints it's trivial to do the rest.
e.g. [#(0, 2), #(2, 2), #(4, 2)] from one of the examples (where 0 is "r", 1 is "o", etc. and the second value is the count of that waypoint in the program), we can do 26 because we start at r so that's the longest, then the next longest is to 4 so 26+4 and we've got [#(2, 2), #(4, 1] left, so 26+4+16 then repeat for the 2 26+4+16+4+1*6. Works the same for the large input so I just did that in a calculator and got the answer. I still can't wrap my head around how to implement the algorithm without loops! Not used to this stuff.
pub fn part2(input: String) -> Int {
let assert Ok(program) =
input
|> input_to_program()
// We want a count of each letter in the program
// sorted
let counts =
program
|> list.fold(dict.new(), fn(acc, next_wp) {
let count = case dict.get(acc, next_wp) {
Ok(n) -> n + 1
_ -> 1
}
dict.insert(acc, next_wp, count)
})
|> dict.to_list()
|> list.sort(fn(a, b) { int.compare(a.0, b.0) })
|> echo
counts |> consume_sorted(Status(0, 0))
}
I actually didn't even finish my program for part 2 because I'm so bad at gleam but I figured out it's pretty easy to get the answer (more in the details).
Not a terribly "clean" solution; just a do-what-the-problem-says approach. It does turn out you can greedily optimize part 2 and it works just fine, to reduce 991 loops down to 5, but I'm not sure if that would hold generally. (I think I saw someone say that would be NP-hard, so it probably wouldn't be appropriate for day 2.)
lily | 17 hours ago
Took some time to clean this up a bit before posting it, though it could probably still stand to be made more succinct. I used the waypoint characters directly when solving this initially, but that made part 2 especially kind of ugly. Instead, I now convert the waypoints to integers (from 0 to 5), which as a bonus allows part 1 to be done more efficiently. Part 2 is handled by keeping track of how many of each waypoint remain, and repeatedly selecting the most optimal waypoint that's still available.
Solution (Lily)
Boojum | 13 hours ago
This is kind of brute-force and ugly, especially the Part 2 solution. I believe that it could be made a decent bit shorter, but oh well. Namely, I think there's a static ordering that is always optimal.
Part 1
Part 2
Visualization
thecakeisalime | 7 hours ago
For part 2, you're correct that there's a static order. When the colour is the same, the step size is always 6, so you only need to maximize the step size when the colour changes. So if you can find the optimal ordering of 'roygbv', you've found the optimal ordering of the sorted list.
EDIT: I looked at your Reddit post and it seems like you've already figured this out.
tomf | 13 hours ago
This could be way cleaner... the second part was a waste of time. Not that these should be way harder, but three of four have been trivial.
Google Sheets
vsczpv | 8 hours ago
When I started doing Part 2 for a moment I thought I was getting to reuse Day 01's code, but the ordering of instructions always been the same saved me from abusing BTreeMap a second time.
Both Parts (Rust)
FlappyFish | 7 hours ago
For part 2, I spotted that you just need the length of the string and which colours occur in order to calculate the furthest distance, but I couldn't figure out how to cleanly count the jumps for the occurring colours in one line without using my previous algorithm. I didn't consider that I could just assume that all the colours occur, due to the length of the string
Part 1
Part 2
kari | 6 hours ago
I'm still working on part 2 but figured I'd share my part 1:Both parts in Gleam!
Part 1
It doesn't feel super idiomatic maybe? I dunno, functional programming is fun but I think I don't do it enough so I always make unreadable code lol.
Part 2
consume_sorted()is actually a noop right now because I got kinda stuck with looping in gleam :P butttt I realized if I just get the counts of the waypoints it's trivial to do the rest.e.g.
[#(0, 2), #(2, 2), #(4, 2)]from one of the examples (where 0 is "r", 1 is "o", etc. and the second value is the count of that waypoint in the program), we can do 26 because we start at r so that's the longest, then the next longest is to 4 so 26+4 and we've got[#(2, 2), #(4, 1]left, so 26+4+16 then repeat for the 2 26+4+16+4+1*6. Works the same for the large input so I just did that in a calculator and got the answer. I still can't wrap my head around how to implement the algorithm without loops! Not used to this stuff.I actually didn't even finish my program for part 2 because I'm so bad at gleam but I figured out it's pretty easy to get the answer (more in the details).
fidwell | 6 hours ago
Not a terribly "clean" solution; just a do-what-the-problem-says approach. It does turn out you can greedily optimize part 2 and it works just fine, to reduce 991 loops down to 5, but I'm not sure if that would hold generally. (I think I saw someone say that would be NP-hard, so it probably wouldn't be appropriate for day 2.)
Part 1
Part 2
Kremor | 5 hours ago
Here's my solution in Guile
Day 2: Fulminating Farmland
Banazir | 3 hours ago
Solutions for both parts in Lua:
Both parts