Advent of Code 2023 - Day 6

  Рет қаралды 468

Daniel Persson

Daniel Persson

Күн бұрын

Пікірлер: 10
@aaronperl
@aaronperl Жыл бұрын
My solution was similar to yours, except I was able to get away with just using longs. Maybe I got lucky with my test input 😁 I was fully expecting Part 2 to ask us to take acceleration into account so that it wasn't just a simple linear function, or that the number of iterations would be huge, so I was getting ready to search my brain for whatever it retained from the linear optimization course I took 25 years ago (spoiler: I barely passed it back then, and I remember almost nothing from it now). Fortunately it turned out to be very quick to brute-force. The one optimization I made was that I break out of the loop as soon as I start losing races again, since anything after that point will have too short distances. Turns out it doesn't really save any measurable time since the total runtime is so fast, though...
@WayneBagguley
@WayneBagguley Жыл бұрын
Part 1 can be done in 1 line of Kotlin: fun calc(input: List): Int = input[0].drop(5).trim().split(Regex("\\s+")).map { it.toInt() }.zip(input[1].drop(9).trim().split(Regex("\\s+")).map { it.toInt() }).map{r -> (1 until r.first).map { (r.first - it)*it }.count{ it > r.second }}.reduce(Int::times) So can Part 2: fun calc(input: List): Int = (input[0].drop(5).trim().replace(Regex("\\s+"), "").toLong() to input[1].drop(9).trim().replace(Regex("\\s+"), "").toLong()).let{r -> (1 until r.first).map { (r.first - it)*it }.count{ it > r.second }} Ok, so it's not readable like this but you can split it over 3 lines for that. I plan to try Part 2 using quadratic roots at some point.
@DanielPersson
@DanielPersson Жыл бұрын
Hi Wayne. Well that is certainly a feat. I one commented on an early challenge another year that they solved day 1 in Brainfuck and that is definitely impressive. But as you say with a couple of line breaks this code becomes readable :) Great work. Thank you for watching my videos. Best regards Daniel
@Needforspeedfreak2nd
@Needforspeedfreak2nd Жыл бұрын
I am kinda proud of my solution here. For the calculating part of it i just handled it like a graph and calculated its zeroes. It basically boiled down to; Record =< (TotalTime - timeHeld) * timeHeld which can be written as 0 =< timeHeld² - totalTime*timeHeld - Record so think in terms of a graph: everything above 0 is a solution. so i calculated its zeroes, do some clever rounding and subtract the higher coord from the lowerr coord to get its range (and add one for the good ol index 0) class Race { long TotalRaceTime; long Record; public Race(long time, long record) { this.TotalRaceTime = time; this.Record = record; } public long getTimesToBeatRecord() { var t1 = ( 0.5 * (this.TotalRaceTime - Math.Sqrt(Math.Pow(this.TotalRaceTime, 2) - 4 * this.Record))); var t2 = ( 0.5 * (this.TotalRaceTime + Math.Sqrt(Math.Pow(this.TotalRaceTime, 2) - 4 * this.Record))); t1 = Math.Floor(t1 + 1); t2 = Math.Ceiling(t2 - 1); var ret = (t2 - t1) + 1; return (long)ret; } } My numbers were a good bit shorter, so they fit in a long, but this would be able to handle numbers a LOT bigger Also this is C# so syntax is a little diffrent
@aaronperl
@aaronperl Жыл бұрын
This was my first thought for a solution, but I only have a limited amount of time to work on the puzzles (at least for now, until my vacation starts), so trying to remember how to calculate intercepts at 5am was too taxing 🤣 so brute-force it was. Good solution, though 👍
@TheTrienco
@TheTrienco Жыл бұрын
@@aaronperl I still have to look up the quadratic equation every time. My math teachers would be very disappointed. In my head it's just "plus minus sqrt .. something something 4x something something". I fully expected part two to be intentionally too large for brute force.
@aaronperl
@aaronperl Жыл бұрын
@@TheTrienco My math teacher drilled it into us, "The only thing I will remember from grade 11 math is that the roots of the quadratic equation ax2 + bx + c= 0 are b plus or minus sqrt of..." and I've forgotten the rest, I always have to look it up ... but he was right, it is the only thing I remember from grade 11 math 🤣
@TheTrienco
@TheTrienco Жыл бұрын
@@aaronperl All I remember is (translated) "It's called midnight formula, because you have to know it, even if I wake you in the middle of the night". I got a feeling the wrong thing stuck from those lessons.
@DanielPersson
@DanielPersson Жыл бұрын
Hi NeedForSpeedFreak Lovely solution. Clear math and clear code. Great work! I wish I remembered these kind of simplifications from my studies :) Best regards Daniel
Advent of Code 2023 - Day 7
35:11
Daniel Persson
Рет қаралды 424
Summing numbers with Iterators in Rust | Advent of Code 2022 Day 1
16:46
Support each other🤝
00:31
ISSEI / いっせい
Рет қаралды 81 МЛН
Une nouvelle voiture pour Noël 🥹
00:28
Nicocapone
Рет қаралды 9 МЛН
The evil clown plays a prank on the angel
00:39
超人夫妇
Рет қаралды 53 МЛН
Quando A Diferença De Altura É Muito Grande 😲😂
00:12
Mari Maria
Рет қаралды 45 МЛН
Advent of Code 2023 Day 6: Wait For It in C# ✅✅
17:16
Martin Zikmund
Рет қаралды 419
Coding in a Random Programming Language Everyday (Huge Mistake)
17:17
Advent Of Code Day 1 challenge walkthrough solution
15:54
Felix Cenusa
Рет қаралды 831
Day 03 - Advent of Code
30:22
chris biscardi
Рет қаралды 7 М.
The Dome Paradox: A Loophole in Newton's Laws
22:59
Up and Atom
Рет қаралды 1,2 МЛН
Advent of Code 2023 Day 1: Trebuchet?! in C# ✅✅
19:58
Martin Zikmund
Рет қаралды 3,9 М.
ASMR Programming - Spinning Cube - No Talking
20:45
Servet Gulnaroglu
Рет қаралды 4,3 МЛН
Advent of Code 2024 - Day 6 - Guard Gallivant
14:24
Daniel Persson
Рет қаралды 285
Support each other🤝
00:31
ISSEI / いっせい
Рет қаралды 81 МЛН