Day 6: Wait for It
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
Today was easy enough that I felt confident enough to hammer out a solution in Uiua, so read and enjoy (or try it out live):
{"Time: 7 15 30"
"Distance: 9 40 200"}
StoInt β /(+ Γ10) β½Γβ(β₯0)(β€9). -@0
Count β (
ββ’β-Γ4:Γ.ββ. # Determinant, and time
+1-β(+1β₯0βΓ·2-)(-1βΓ·2+) # Diff of sanitised roots
)
β‘(β1ββββ @\s.)
β(/Γβ‘Countββ΅StoInt)(Countββ‘(StoIntβ/β))
Rust
I went with solving the quadratic equation, so part 2 was just a trivial change in parsing. It was a bit janky to find the integer that is strictly larger than a floating point number, but it all worked out.
Nim
Hey, waitaminute, this isnβt a programming puzzle. This is algebra homework!
Part 2 only required a trivial change to the parsing, the rest of the code still worked. I kept the data as singleton arrays to keep it compatible.
Hi there! Looks like you linked to a Lemmy community using a URL instead of its name, which doesnβt work well for people on different instances. Try fixing it like this: !nim@programming.dev
Dart Solution
I decided to use the quadratic formula to solve part 1 which slowed me down while I struggled to remember how it went, but meant that part 2 was a one line change.
This year really is a roller coasterβ¦
int countGoodDistances(int time, int targetDistance) {
var det = sqrt(time * time - 4 * targetDistance);
return (((time + det) / 2).ceil() - 1) -
(max(((time - det) / 2).floor(), 0) + 1) +
1;
}
solve(List> data, [param]) {
var distances = data.first
.indices()
.map((ix) => countGoodDistances(data[0][ix], data[1][ix]));
return distances.reduce((s, t) => s * t);
}
getNums(l) => l.split(RegExp(r'\s+')).skip(1);
part1(List lines) =>
solve([for (var l in lines) getNums(l).map(int.parse).toList()]);
part2(List lines) => solve([
for (var l in lines) [int.parse(getNums(l).join(''))]
]);
Haskell
This problem has a nice closed form solution, but brute force also works.
(My keyboard broke during part two. Yet another day off the bottom of the leaderboardβ¦)
import Control.Monad
import Data.Bifunctor
import Data.List
readInput :: String -> [(Int, Int)]
readInput = map (\[t, d] -> (read t, read d)) . tail . transpose . map words . lines
-- Quadratic formula
wins :: (Int, Int) -> Int
wins (t, d) =
let c = fromIntegral t / 2 :: Double
h = sqrt (fromIntegral $ t * t - 4 * d) / 2
in ceiling (c + h) - floor (c - h) - 1
main = do
input <- readInput <$> readFile "input06"
print $ product . map wins $ input
print $ wins . join bimap (read . concatMap show) . unzip $ input