I've completed "Guard Gallivant" - Day 6 - Advent of Code 2024 #AdventOfCode https://adventofcode.com/2024/day/6
[#]Swift
https://github.com/Felyashono/AdventOfCode24/blob/main/Day6.swift
=> More informations about this toot | More toots from felyashono@disabled.social
I did Part 1 running the simulation step by step. I leveraged one key insight for Part 2: any new obstacle must be placed somewhere on the original path from Part 1. This reduces the search space from 19K to 5K (approx) tiles, and I brute forced it from there.
And the trick for detecting a loop was being on a previously traversed tile and facing the same direction.
Runtime was around 25 minutes single-threaded on an Apple M1 Pro.
[#]AdventOfCode #Swift
=> More informations about this toot | More toots from felyashono@disabled.social
And I got the runtime down to 3.5 minutes by adding concurrency, running on the same 10-core M1 Pro.
[#]AdventOfCode #Swift
=> More informations about this toot | More toots from felyashono@disabled.social
@felyashono You should get much better speeds; my solution is running in 1.8s.
=> More informations about this toot | More toots from teotwaki@mastodon.online
@teotwaki
Are you saying my code should be getting better speeds than it is, or that I should have optimized it more/better?
=> More informations about this toot | More toots from felyashono@disabled.social
@felyashono @teotwaki My solution is in Typescript/Javascript, does a bunch of unneeded object allocations and still brute-forces in half a second on a base M1.
I'd say something odd is going on to hit the minutes in a compiled language...
=> More informations about this toot | More toots from nordern@chaos.social
@nordern @teotwaki
I assume by "brute-force” we're all talking about trying to place an obstacle on each of the 5,000+ points on the original path.
Are you also re-running the entire patrol route in each case afterwards to check for loops? I re-run it from the start, breaking it if the guard leaves the map or encounters the same tile a second time while facing the same direction.
=> More informations about this toot | More toots from felyashono@disabled.social
@felyashono @teotwaki Yes, you walk through the map once to find all relevant points, iterate through all the points, put an obstacle there on a copy of the original map, let the guard run on that until you hit a loop or the bounds.
=> More informations about this toot | More toots from nordern@chaos.social
@nordern @felyashono I guess one way to optimise it further would be to memoise the path simulation. You only need to check the guard’s decisions when its path is aligned on either x or y axis with the new obstacle. Haven’t figured out how to implement that, though.
=> More informations about this toot | More toots from teotwaki@mastodon.online
text/gemini
This content has been proxied by September (ba2dc).