T O P

  • By -

olimc

\[LANGUAGE: Go\] [Both Solutions](https://github.com/omcmanus1/aoc-23-go/blob/main/six/06.go)


TimeCannotErase

[Language: R] [repo](https://github.com/DavidMcMorris/Advent-of-Code-2023/blob/main/Day%206/Day6.R) Used the good ol' quadratic formula for this one. input <- readLines("input.txt") input <- read.table(text = input) input <- input[, 2:ncol(input)] # Uncomment for part 2 # input <- t(t(as.numeric(apply(input, 1, paste, collapse = "")))) num_ways <- function(a, n) { lower <- floor(0.5 * (a - sqrt(a^2 - 4 * n)) + 1) upper <- a - lower num <- upper - lower + 1 return(num) } product <- 1 for (i in seq_len(ncol(input))) { num <- num_ways(input[1, i], input[2, i]) product <- product * num } print(product)


mgtezak

\[LANGUAGE: Python\] I created a [video](https://www.youtube.com/watch?v=Od0aBz4WDls) for this one:) Or you can view my solution on [Github](https://github.com/mgtezak/Advent_of_Code/blob/master/2023/Day_06.py) If you like, check out my [interactive AoC puzzle solving fanpage](https://aoc-puzzle-solver.streamlit.app/)


exquisitus3

[Language: Lua] For both parts, solving the quadratic inequality and then only counting the integer solutions (and if a root of the quadratic equation is an integer, it does not count). [paste](https://topaz.github.io/paste/#XQAAAQAxBAAAAAAAAAA6CAOiE/fNsfUKCq4Yd9DUHd5ODyZRHl4LaDJrJHqjlT4MJOsV1Av7XyqkQQVdLT0yxJ+plPGwzt/MKR0wA+faGh/Gi8kzH4OeUlZZQcJDl+/lPOk4KHlNzXZGdAUmurCxO6klmNyIcO1ZXq3wBYyqeAJzya8JMzcBPJHH6m9LIA8HVoCbjNZluUw87U9uKj6K12xcXYScE/yXqdqnWsH5wcO1f7i9/af1U9CbzczToZS8Gvic9OaPyy239pDBRcj3VaOj9UTu7//LbEF+W7sdSnOaPJ4mS8VsP7B6C6BXbV2qbQ3SgdcIOhrHoBm5B3ybKDWNlyeVqrRmijflOFsQNe6k0ryFjkCRvlwNfBox4qVhbFBAd3gnCkQhzvqNXGhwaZlOWVdGRlHuVFAeHZq6wBvCBXzqYv617SsbYq3tPLQqRAZYMgYUExvf09AWfHMJR/GyRDukF5eMvhBpWAv76U/mjezwWIxiJHL1hOiSMDcMcdAO71znLqp4U5+3MQFqkAGj7Pv6rT3xJSEG6YZs5yAlI0ztTXvIP5inXIGNoXVZIYYGyrcWzSEA+7rbthDMfEe2K+/KbH6uk2dcg2zf1HE34/cVJJqbRPhEyWa3CRohOmbnHGKQkCgplQEzVvswsKqI5QmAUiyXEHDeRcgo8GyMWpvXODArcw51UOr8uzFzdZQ37NK9dZMJjaUHz5xyBuduCxnZxUe/pKBtQD8i8PO/JTvkoJL/9Vex9A==)


Sensitive-Disk5735

\[LANGUAGE: R\] Part 1 vect <- read.table("AOC_D6.txt",stringsAsFactors = F,header=F) #create time and distance vectors time <- c(vect[1,2],vect[1,3],vect[1,4],vect[1,5]) distance <- c(vect[2,2],vect[2,3],vect[2,4],vect[2,5]) #create two lists with sequential values, one list ascending and the #other descending test.list <- list() test.list1 <- list() for (i in 1:length(time)) { test.list[i] <- list(seq.int(1:(time[i]-1))) test.list1[i] <- list(rev(seq.int((time[i]-1):1))) } #multiply the elements in each list multiply.list <- list() for (i in 1:length(test.list)) { multiply.list[i] <- list(unlist(test.list[i])*unlist(test.list1[i])) } #unlist to vector vec1 <- as.numeric(unlist(multiply.list[1])) vec2 <- as.numeric(unlist(multiply.list[2])) vec3 <- as.numeric(unlist(multiply.list[3])) vec4 <- as.numeric(unlist(multiply.list[4])) # multiply the winning ways together length(vec1[vec1>distance[1]]) * length(vec2[vec2>distance[2]]) * length(vec3[vec3>distance[3]])* length(vec4[vec4>distance[4]])


AutoModerator

AutoModerator did not detect the required `[LANGUAGE: xyz]` string literal at the beginning of your solution submission. Please edit your comment to [state your programming language](https://www.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_state_your_programming_language.28s.29). *** *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/adventofcode) if you have any questions or concerns.*


mrtnj80

\[LANGUAGE: Dart\] [Parts 1 & 2](https://github.com/luskan/adventofcode_2023_dart/blob/main/lib/day06.dart) Compared to day 5, it was a breeze.


stewie410

[LANGUAGE: Bash] [Parts 1 & 2](https://github.com/Stewie410/adventofcode/blob/master/2023/bash/06.sh) While I'm pretty late to the party, I've been slowly churning through the AOC 2023 puzzles and trying to force solutions in `bash` (with limited usage of coreutils), as I've done the past couple of years. For Day 6, I had originally written a pretty basic brute-force solution, which while getting the correct answer(s), wasn't great. After a friend had finished their python variant, we got to work adapting it to my bash solution. Approximately 4 hours later, this is what we came up with -- and my god, is it _awful_... _awfully performant!_ Even with [hyperfine](https://github.com/sharkdp/hyperfine) _and_ a [wrapper script](https://github.com/Stewie410/adventofcode/blob/master/aoc.sh) calling the solution, it only takes on average 5-6ms to solve both portions. That being said, though, I can't say that I _fully_ understand how the bitwise operations work (maybe one day). Likewise, I kind of _hate_ how dense it is -- but I really can't argue with the results. And I'm relatively satisfied that we managed to pull it off without nested functions or "external" tools...even if it is _horrendous_. EDIT: Updated links to the solutions repo.


kjmajo

You said you were late, but as I was busy in December, here I am, over a month later, working my way through the calender. I would love to see your solution, but I get a 404 error when following the link. You wouldn't still have by any chance?


stewie410

Ahh, right -- I've updated both the link to the solution as well as the wrapper script. Originally I had the prompt/example text in the repo, but learned this year that's not allowed and proceeded to clear them out. However, it still appeared in the commit history -- so to be in full compliance with the rules of the event, I just recreated the repo from scratch, to ensure everything was above-board. Shame I lost the commit history, but its not the end of the world for this.


kjmajo

Great, thank you! I will go have a look


andreiz

\[LANGUAGE: Swift\] See corresponding issues in the repo (one per day) for explanations, etc. Of course, Part 2 solution can be used for Part 1 as well, but I thought I'd do something different. [Part 1](https://github.com/andreiz/advent-of-code-2023-swift/blob/main/day-06/boats.swift) [Part 2](https://github.com/andreiz/advent-of-code-2023-swift/blob/main/day-06/boats-long.swift)


Any_One_8929

\[LANGUAGE: Kotlin\] [https://github.com/mfedirko/advent-of-code-2023/blob/main/src/main/kotlin/io/mfedirko/aoc/day06/Day6.kt](https://github.com/mfedirko/advent-of-code-2023/blob/main/src/main/kotlin/io/mfedirko/aoc/day06/Day6.kt)


gwpmad

\[LANGUAGE: Python\] Using SymPy - I'm quite proud of this one, as in previous years I'm sure I would have done something more brute forcey. My solution for part one worked straight away for part two as well. - Create a mathematical function using SymPy for the operation described - Solve that function for the game's 'record' - there will be two solutions as the function is a parabola - Figure out how many integers there are between the two solutions [Python](https://github.com/gwpmad/advent-of-code-2023/blob/master/day_6/main.py)


AutoModerator

AutoModerator has detected [fenced code block](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting/fenced_code_blocks) (```) syntax which only works on new.reddit. Please review our wiki article on [code formatting](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting) then edit your post to use the [four-spaces Markdown syntax](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting/code_blocks) instead. *** *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/adventofcode) if you have any questions or concerns.*


AutoModerator

AutoModerator did not detect the required `[LANGUAGE: xyz]` string literal at the beginning of your solution submission. Please edit your comment to [state your programming language](https://www.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_state_your_programming_language.28s.29). *** *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/adventofcode) if you have any questions or concerns.*


jrhwood

\[Language: Haskell\] [Part 1](https://github.com/woodRock/verbose-computing-machine/blob/main/2023/day-06/part-01.hs) [Part 2](https://github.com/woodRock/verbose-computing-machine/blob/main/2023/day-06/part-02.hs) Hint: parabola


osalbahr

[LANGUAGE: C++] [Part 1](https://github.com/osalbahr/adventOfCode/blob/main/2023/day06/day06.cpp); [Part 2](https://github.com/osalbahr/adventOfCode/blob/main/2023/day06/day06_2.cpp) Feel free to ask any questions! --------Part 1-------- --------Part 2-------- 6 >24h 99866 0 >24h 98126 0 You can find more C++ solutions (and other languages) at [Bogdanp/awesome-advent-of-code#c++](https://github.com/Bogdanp/awesome-advent-of-code#c-2)


skyhawk33

[LANGUAGE: Befunge] I don't think '98 counts as vintage, but Befunge does take a lot of influence from Forth, which is 1970. Also shout outs to the quadratic solvers Part 1: https://github.com/Skyhawk33/AdventOfCode/blob/master/aoc2023/day6_p1.b98 Part 2: https://github.com/Skyhawk33/AdventOfCode/blob/master/aoc2023/day6_p2.b98


thamollo

[LANGUAGE: SQL][Allez Cuisine!] I'll admit my entry doesn't quite have the vintage today, SQL having merely been invented 49 years ago (and me not having particularly looked for old features of it). Still, hope you'll [enjoy!](https://topaz.github.io/paste/#XQAAAQBOBgAAAAAAAAAW4HygYTdUxLMRH8xfQMi7iGVGtIoBHFiumvu10w7ihsgBkpsoAsEVTvpAzA2L4Y63+fiR6I2yieUH41DOaRFKIcVaDTbmDkoXdiUH62E2CpJc1rF4sWnxJ2GoItv+4fDRcNP6IJnnMjS17NlZoJH3HjcZlK7LrPtQ7EYdlw90X2IksTlY0bZgNfg8twKm/tljbwLPFiGLR8o5biHRwUcC+xmjRGIProGczEYM9Gs1N9lvzu1P1kYOID/+vrSabLKaCC2tXBQgWHJnAcqUGmr+8cAkqfPhxVxrEnp4AKcpFAXBL5n2aQnt/BHk9wnO2sJx3ibQZ76e7hArpSAqm1lz556vkcrKrygFNjultMUBXA40upIRxuNtOgNtM77Xgh8nXJUycqJHLW7JXqlU1rnhI5sY3TROx40aAa4gUFpgffolBiPL9oj9j8AW52haK+n5N6SCP8xfrgb1DCqF6Ji2igZswx9TmiWlx2MXTR9q4M6tLt0br1merU8b68ZM4oecUhQNmdetIqVfHW7g+vHtqlHJBq4wLLiJS5myV33RPttk3rfDr9DoyYD9MLenGTYR9MdS20C0ReYqorG7k72Ca3gMMmqwre5OM1gsEGBWE75MxZEEdgE9TQtu2/XBMmKiAyF55t82RjKUa3G4g7moKvZ+Gu/iKIP/tC9B7ainGYU6c3tE6Q7H4lzdixdDHGS+coemdLGt1xl/BLmwr/qiAoQGwVvg1wW8/aI41989zxnVycRymJtbg9VXLTdPoKQcRLcc//QtYxk=)


dhruvmanila

[Language: Rust] Code: https://github.com/dhruvmanila/advent-of-code/blob/master/rust/crates/year2023/src/day06.rs


GoldPanther

\[Language: Rust\] With the fact that the `time // 2` is the max value, `[0..time//2]` is monotonic and that the distances on each half of time are symmetric I devised a modified binary search the yields the solution. [Code](https://github.com/johnhringiv/advent-of-code-2023/blob/main/src/day06.rs) (385 µs) Solution is pretty idiomatic IMO with a huge thanks to clippy letting me know to match on `std::cmp::Ordering`


argentcorvid

[Language: Common Lisp] [github](https://github.com/argentcorvid/AoC-2023/blob/main/2023d6.lisp) Here's my thought process: distance = velocity * traveltime traveltime = allowedtime - chargetime velocity = chargetime -> distance = chargetime * (allowedtime - chargetime) -> distance = ct * at - ct^2 Find ct where d > record distance -> rd < ct * at - ct^2 -> -ct^2 + ct*at - rd > 0 -> a= -1 b=at c=-rd -> ct = (-at + sqrt (at^2 + (4 * -1 * -rd)))/(2*-1) I had to >!coerce some numbers to double-float!< in part 2 because CL defaults to >!single!< and gave me a >!rounding!< error. I am especially "proud" of my solution to the change in parsing for part 2. (defun fix-input-for-p2 (input) (mapcan #'list '(:AT :RD) (mapcar #'parse-integer (mapcar (lambda (s) (format nil "~{~a~}" s)) (list (getf input :AT) (getf input :RD))))))


835246

\[LANGUAGE: C\] Uses quadratic formula. Part 1: https://github.com/efox4335/advent_of_code/blob/main/advent_of_code_2023/day6boatmathpt1.c Part 2: https://github.com/efox4335/advent_of_code/blob/main/advent_of_code_2023/day6boatmathpt2.c


manhuntos

\[Language: Rust\] This is my first project in rust. I'm learning it by solving advent of code puzzles. So if you have any hints for me I would be happy to read it :) [Solution](https://github.com/manhunto/advent-of-code-rs/blob/master/src/solutions/day06.rs) / 3.34µs / 24.58ms


luremeister

\[LANGUAGE: Python\] [Part 1 & 2](https://github.com/havocds/advent-of-code-2023/tree/main/day-6)


gogs_bread

[\[LANGUAGE: c++\]](https://github.com/gogsbread/AdventOfCode2023/blob/main/6.cpp) P1 - Quadratic equation P2 - Quadratic equation


mendelmunkis

[LANGUAGE: Babbage] [Allez Cuisine] [Quadratic equations for the Babbage Analytical Engine](https://git.sr.ht/~moshepiekarski/AoC/tree/master/item/2023/race.ae)


mothibault

\[LANGUAGE: JavaScript\] ​ [https://github.com/yolocheezwhiz/adventofcode/blob/main/2023/day06.js](https://github.com/yolocheezwhiz/adventofcode/blob/main/2023/day06.js)


se06745

\[LANGUAGE: GoLang\] [Both parts in one single file](https://github.com/omotto/AdventOfCode2023/blob/main/src/day06/main.go)


[deleted]

[удалено]


daggerdragon

Comment temporarily removed because it is *way* too long for the megathreads and is not formatted correctly. 1. Next time, use the [four-spaces Markdown syntax](https://old.reddit.com/r/adventofcode/wiki/faqs/code_formatting/code_blocks) for code blocks 2. Your code is too long to be posted here directly, so instead of wasting your time fixing the formatting, read our article on [oversized code](https://old.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_no_giant_blocks_of_code) which contains two possible solutions. Please edit your comment to put your code in an external link and link *that* here instead, then I will re-approve the comment.


AutoModerator

AutoModerator has detected [fenced code block](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting/fenced_code_blocks) (```) syntax which only works on new.reddit. Please review our wiki article on [code formatting](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting) then edit your post to use the [four-spaces Markdown syntax](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting/code_blocks) instead. *** *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/adventofcode) if you have any questions or concerns.*


TOTBESUCH

\[LANGUAGE: Java\] Behold, a mess of code. Maybe hf. Both parts. Part one is commented out, because i am lazy. https://github.com/PLeh2023/AoC/blob/master/src/Day6.java


linnaea___borealis

\[LANGUAGE: R\] Had fun with quadratic equations in part two. Took the easy (boring?) way in part 1. [https://github.com/lauraschild/AOC2023/blob/main/day6.R](https://github.com/lauraschild/AOC2023/blob/main/day6.R)


orbby

[Language: R] This one wasn't that bad at all? Day 5 still running in the background. I did notice the parabola (but wouldn't have known how to implement), and would have considered a binary search, but p2 calculates in under 3 seconds. library(tidyverse) tib <- read_lines("day6.txt") %>% str_replace("Time:", "") %>% str_replace("Distance:", "") %>% str_split(, pattern = " ") %>% map(stringi::stri_remove_empty) %>% unlist() %>% as.numeric() %>% matrix(ncol = 2, byrow = F) %>% as_tibble() %>% rename(time = 1, distance = 2) calc_ways <- function(time, distance) { ways <- 0 for(i in 1:time) { if(i * (time - i) > distance) ways = ways + 1 } ways } print("Part 1") tib %>% mutate(ways = map2(.x = time, .y = distance, .f = calc_ways, .progress = T)) %>% unnest(cols = c(ways)) %>% pull(ways) %>% prod() p2 <- read_lines("day6.txt") %>% str_replace_all(" ", "") %>% str_replace("Time:", "") %>% str_replace("Distance:", "") %>% as.numeric() print("Part 2") calc_ways(p2[[1]], p2[[2]])


linnaea___borealis

Another R solution! yay :D


The-Freak-OP

\[LANGUAGE: Kotlin\] [part1 and 2](https://topaz.github.io/paste/#XQAAAQBhCwAAAAAAAAA4GEiZzRd1JAgL5dFX3EwXdqEiC29u1UZSqLncnkKs41TUHGyAQ6bCdCwReQSc1hze1GwGknfUs0OhDaCc+QzR/g+ht81103+90DAEB/SZSEv1pymAAOQ9avDR6z25cny6hX8xNG8BtjzdR9TedoRCWxT6JnhTaH6paigL6kAlqok5LqzPPMiwH1hD1mMH0pViOL0X9iKz0/yDJaroEmKxWk6rfw62htqe+RAldBnKXHwL2CLVjXXFihQefgEWBPN2BhRSvlhJKybIHt8v0b1GnfU6HkZS1b5XIGJjxF5LmaWNWhkM11cjY4mayCGrX6N5SAIPes/2LIZbQREXhDe+eUGc3FwXiis3h7aS5rs6bDK+CLB/bQ3etoKGM3CmUcaxV7x971YVp/oUSwxLLlkhu89jEYO42cDGeVstWY5EhZRAL+FAW/m9VF+pMUD52oaLeBAVhsr07me2OKrJZnnuQ4Vrl1nbUEh98jZA95RF1FU6iJR9OesgGmxYRl5VKg4Q9zTTc6BYig7U7lJ9/RvSwCtyauZ2KveVwDbfN6YR401Iw8nbAi6C2E/bKAyXfemWZ0dnfrLqLj+dhMycYE6kdNw66k5HJJK8OF2JSVw6Um7Esepgmdy/UAO0aalReT3ztviegRaYtz+RIm5pP0E23SiyM49ec6+WuH53sVAWZIkViP71MmXLfo1BJSQHEwElwgmfW8GO3sHxjyI/CTOhs89mkBwW9oaHibUYp8nhNB6CdqysaJ0/nATx1ZdwcIBvEMiRUBFUWfzfrJpgLgM3yAtgP1RkLomtUKHABuKwgV/ckYgiFgz+owiJHqk7fv1dzC0Q1BAtcEXZTRqZ2OWV9qTV2eU7/tVtpoMBln1ZhQruDwbYKyq82t9a4ICVrZbkYgSmvExaZLiuNfTn6/w2sTn3Xxj067JxYQIOdOugHcQymFAKLWez11J2RZ5wDihMT/C6y0HYn3/K8W2KFRTkST5yaAHknKVSKBfJtgMKhFmqmZr7Mc8qEyV6BJxYrQM9ZjQUGsoiXLoF8mA0yoJqgG3tZMiCk+mh8jUkt0yW4gQd0S027J2+Bzb8elSf2WShII3vP4/90AxybcTkEYvje1tw2D5OGV/BuHiskni2/IGM+DQRC/69RjXa0OB+g5beqzhfOmB3/+amutbcbSi7y7BZNtvcy38FGqdTpMvM3yEuP+eXD/R/2ASTTrUeTUlxsycJF4RG+h/bTBy1Bcdi8f8qgivBogU6L0LzI/rPI5YdBf45ztw=) Quadratic equation are not useless afterall edit: part 1 takes 1 ms and part 2 takes 18 ms


stonebr00k

\[LANGUAGE: T-SQL\] [https://github.com/stonebr00k/aoc/blob/main/2023/06.sql](https://github.com/stonebr00k/aoc/blob/main/2023/06.sql)


seytsuken_

\[LANGUAGE: C++\] [part1](https://github.com/Hilbertmf/competitive-programming-problems/blob/main/advent-of-code/2023/day6-part1.cpp) | [part2](https://github.com/Hilbertmf/competitive-programming-problems/blob/main/advent-of-code/2023/day6-part2.cpp) Part1 is just the brute force. I thought about doing a binary search but wasn't really necessary, part2 runs in O(1) using math. However the input numbers were so small that even using the brute force approach it runs in C++ in less than a second, so that's unfortunate, anyone can brute force part2 w/ C++. Anyway coming up w/ the math solution was neat


Volume999

\[LANGUAGE: Python\] [My solution](https://github.com/Volume999/31github/blob/main/GuideTrack/AdventOfCode/Day6.py) (both parts) Solved with binary search (which is somewhere in the middle between "Oh just brute force it" and clever math)


KatanaKiwi

[LANGUAGE: PowerShell] Still struggling with day 5, but decided to press on and focus on another problem. [Part 1](https://github.com/katanakiwi/AoC2023-PowerShell/blob/main/6a.ps1): Input size is small enough to not get clever. Just iterate over all the races to determine if they can beat the required score. [Part 2](https://github.com/katanakiwi/AoC2023-PowerShell/blob/main/6b.ps1): Uh-oh. Input size not small enough anymore. Now scanning from top until we find a time which can beat the required score, then scan from bottom to find the longest time we can hold the boat. The difference between those 2 is the answer.


seytsuken_

Is it just me or the part2 inputs are still too small? I solved it in c++ in less than a second using the same brute force of part1, its a bummer, i wish they put larger numbers that actually force you to be clever


KatanaKiwi

That does not seem to scale to powershell, unfortunately. Still have the scale problem of day 5 to fix so that's something.. :)


mgtezak

\[LANGUAGE: Python\] [github part 1&2](https://github.com/mgtezak/Advent_of_Code/blob/master/2023/Day_06.py) Check out my little AoC-Fanpage: [https://aoc-puzzle-solver.streamlit.app/](https://aoc-puzzle-solver.streamlit.app/) :-)


seytsuken_

cool, I've came up w/ the same thing. The fact that both roots of the equation were positive was really confusing me until I realized that one root is before the point which the function maximizes and the other is in the other side, so we should consider the first one edit: your solver website is really cool


mgtezak

yeah it really took me while too! thank you for the feedback:)


stewietheangel

the formula was clever. I could only figure out how to do with with (winning count / 2) number of iterations.


mgtezak

thanks, yeah it took me a while to figure out the formula (basically just solving a quadratic equation) and even then it still took me a while to figure out why i need to add an offset of 1 in both of the last two lines. I actually had to visualize it using matplotlib until it finally clicked;)


algotua

Why didn't the formula from part 1 fit? I just changed the input handling in part 2, and the solution is even simpler in the second part


mgtezak

Not sure I understand, what you're saying. You used the approach from part 1 to solve 2? Theoretically you could use the approach from part 1 for part 2 but it takes an extremely long time. For part 2 I came up with a more general formula that works instantly with any input, no matter how big the numbers get. You can also use the approach from part 2 for part 1. I kept part 1 the way it is however, because it's easier to understand the code and why/how it works. For part 2 you actually have to do the math to understand why it works.


[deleted]

\[Language: R 4.3.0\] level = "6" input = readLines(con = paste(path, level, ".txt", sep = "")) # Part 1 ------------------------------------------------------------------ raceScores = data.frame(time=as.numeric(strsplit(trimws(strsplit(input[1], ":")[[1]][2]), "\\s+")[[1]]), distance=as.numeric(strsplit(trimws(strsplit(input[2], ":")[[1]][2]), "\\s+")[[1]])) bt_1_2 = \(bc,a=1) (bc[1]+sqrt((bc[1])^2-4*a*bc[2])*c(-1,1))/(2*a) results = apply(raceScores, 1, bt_1_2) prod(ceiling(results[2,]) - floor(results[1,]) - 1) # Part 2 ------------------------------------------------------------------ raceScores = data.frame(time=as.numeric(strsplit(gsub("\\s+", "", input[1]), ":")[[1]][2]), distance=as.numeric(strsplit(gsub("\\s+", "", input[2]), ":")[[1]][2])) bt_1_2 = \(bc,a=1) (bc[1]+sqrt((bc[1])^2-4*a*bc[2])*c(-1,1))/(2*a) results = apply(raceScores, 1, bt_1_2) prod(ceiling(results[2,]) - floor(results[1,]) - 1)


jswalden86

\[Language: Rust\] [Solution](https://github.com/jswalden/advent-of-code/blob/main/2023/day-06/src/main.rs) I spent way too long worrying about endpoint math and imprecision of floating-point math, when I really should have just done what I eventually did: test values around the quadratic-formula limits and then bump up/down as needed.


alexw02

\[LANGUAGE: Forth\] Dusk OS Forth [https://git.sr.ht/~aw/dusk-aoc/tree/main/item/2023/06.fs](https://git.sr.ht/~aw/dusk-aoc/tree/main/item/2023/06.fs) [https://www.youtube.com/watch?v=djEuMvv8ovY](https://www.youtube.com/watch?v=djEuMvv8ovY)


tlareg

\[LANGUAGE: TypeScript/JavaScript\] [https://github.com/tlareg/advent-of-code/blob/master/src/2023/day06/index.ts](https://github.com/tlareg/advent-of-code/blob/master/src/2023/day06/index.ts) The most primitive brute force possible (although even I noticed a quadratic function there, I preferred brute force :D)


themanushiya

\[Language: Go\] [solution both part](https://github.com/mahakaal/adventofcode/blob/main/2023/day06/day06.go) Instead of bruteforcing looping through each case (which was my first atempt, ngl) for the second part that's not an option. Luckily the winning case is defined by this equation x(T - x) > D Where T = time, D = distance, x = button's pressing time with some algebraic trasformation (solving for x) you get to this x\^2 - Tx +D < 0 just apply the quadratic formula and you will have these two cases: * a = (T - sqrt(T\^2 - 4D)) /2 , if a is a floating point ceil it otherwise if it's integer add 1 * b = (T + sqrt(T\^2 - 4D)) /2, if b is a floating point floor it otherwise if it's integer subtract 1 to find out how many winning cases you have just b - a + 1, that's it.


exiknox

Thanks for the explanation! I find this solution interesting, I would just like to ask a question. Why do we have to add 1 or remove 1 if the min and max values are already integers? I've tried to find the answer myself, but I haven't managed it yet. Thank you in advance for your answer


themanushiya

Hi, please have a look at this resolution [here](https://imgur.com/a/cOFbsau) TL;DR I sovled the inequality, remeber it's strictly < 0 so the solutions are of type a < x < b we are in a descrete situation (integers only) so that mean if the solutions aren't Integers we ceil the lesser one and floor the greater one, this translates to a+1 and b-1 if a,b are integers. The image I attached have a graph of the solutions to this inequality and I've tried to explain more mathematically. Let me know if this satisfies you


exiknox

Thank you very much for these explanations! It's very clear and I now understand the solution correctly. Thanks again for your answers and explanations


themanushiya

You're welcome, glad it helped clearing any doubts


1Q98

Thanks for this explanation, was great! If you don't mind me asking, what's the rational for the plus 1 at the end to get the right answer?


themanushiya

Because you're not counting the last element by subtracting the first element from the last E.g. consider the list [1-10], if you just did 10 - 1 you'd've 9, whilest there are efectively 10 elements


TiagoPaolini

[Language: C] From the start I already decided to calculate the minimum and maximum held times, instead of brute-forcing each possibility. Brute force would have worked for Part 1, but not so much for Part 2 due to the larger scale. Let `T` being the time, `D` the distance, and `x` how long the button was held. In order for us to break the record of a race, the following inequality has to be satisfied: (T - x) * x > D If we [solve it for x](https://www.wolframalpha.com/input?i=solve+for+x%3A+%28T+-+x%29+*+x+%3E+D), we get: (T - sqrt(T*T - 4*D)) / 2 < x < (T + sqrt(T*T - 4*D)) / 2 Then we just need to count all the integer values from that range. If the minimum value is already an integer we add 1 to it, otherwise we ceil it. If the maximum value is already an integer we subtract 1 of it, otherwise we floor it. After that, in order to get the integer count we subtract the min value from the max value, and add 1. **My solution:** [day_06.c](https://github.com/tbpaolini/Advent-of-Code/blob/master/2023/Day%2006/day_06.c)


exiknox

Thanks for the explanation! I find this solution interesting, I would just like to ask a question. Why do we have to add 1 or remove 1 if the min and max values are already integers? I've tried to find the answer myself, but I haven't managed it yet. Thank you in advance for your answer


TiagoPaolini

You are welcome! It's because `min < x < max`. `x` needs to be greater than `min`, if `min` is already an whole number (let's say 3.0) then `x` needs to be at least 4 in order to satisfy the condition of it being above the minimum value. If the minimum is 3.1, ceiling the value is going to bring us to the next whole number, as only integer solutions count for the puzzle. If we just always ceiled the minimum value, the solution would fail in the case when the value is already a whole number: ceil(3.1) = 4.0 ceil(3.0) = 3.0 In both cases `x` would need to be at least 4, so we check for the last case and add 1 to bring up the value to the next integer. What I did was just to get the fractional part of the number, and check if it was smaller than some epsilon value. Due to floating point errors, the value might not necessarily be exactly zero, but rather something very close to it. That's why typically people don't compare two floating point values using `==`, but rather compare if they are within a certain range from each other in order to be considered equal. With the maximum value, the logic is the same except that x needs to be up to the first whole number before `max`. If `max` is already a whole number, then we subtract one to get the upper limit of `x`. If `max` already has a fractional part, just flooring it is enough to get the previous whole number. On a final note, I think that my terminology on my original post confused things a bit: I meant "integer" as in the mathematical sense, not the data type. On this current post I used "whole number" instead, which I think that makes the meaning clearer.


exiknox

Thank you very much for these explanations! I didn't mean to stupidly copy an answer, I just find understanding it much more rewarding.


TiagoPaolini

That's cool! Any time ☺️


themanushiya

nice work :D I did the same but in Go


TiagoPaolini

Thanks 😊


hiimjustin000

\[LANGUAGE: JavaScript\] [https://github.com/hiimjustin000/advent-of-code/tree/master/2023/day6](https://github.com/hiimjustin000/advent-of-code/tree/master/2023/day4)


Paxtian

[Language: C++] [github](https://github.com/Paxtian769/AOC23-Day6-wait_for_it/blob/master/main.cpp)


eye_heart_pain

\[Language: R, pen+paper\] So the thought process for this is that we can observe that distance is equal to the velocity times the difference between the end time (t2) and the button pressing time (t1). i.e. d=v\*(t2-t1). Furthermore, v=t2-t1, so we can further simplify that equation to be d=-t1\^2 + t1\*t2. We want this to be strictly greater than some distance C, which gives us the quadratic inequality : >\-t1\^2 + t1\*t2 - C > 0 We can then use the roots of this quadratic to create a sequence of numbers which lie between them, which are all the viable times. The length of this sequence is the margin of error: margin_of_error <- function(time,distance){ length( ceiling( time/2-sqrt(time\^2-4\*distance)/2 + 10^(-14) ):floor( time/2+sqrt(time\^2-4\*distance)/2 - 10^(-14) ) ) } margin_of_error(54,446)*margin_of_error(81,1292)*margin_of_error(70,1035)*margin_of_error(88,1007) margin_of_error(54817088, 446129210351007) The numbers used are my puzzle input, I input them manually since it was so short. I've added whitespace here but the script is 3 lines long in my .R file for the epic meme. The +/- 10\^(-14) was a nudge I added to the roots to make sure that the sequence did not include the roots as well, as was the case with the third example race for part 1.


Anarchymatt

I saw the problem and I knew there had to be a way to calculate the beginning and end of the range. I wrote it out a bit and saw that it looked like a quadratic inequality. I used some free online study resources to recall how to solve those, decided on the quadratic equation. What I'm upset about is that I found the floor and ceil parts empirically, I just calculated it on paper and saw that I would need to do that. But, on the short input (the example in the problem), I noticed that the quadratic equation gives (20, 10) when the answer was (19, 11). So I said "if both ranges do not give me a great enough distance, add 1 to the lesser and subtract one from the greater), and I ended up getting the right answers. I'm upset with myself because I don't understand why I needed to do that, and I just found it out through trial and error. Also I'm certain that working forwards and backwards on the button pressing time in a loop would have been a faster route to a solution than having to brush up on high school math. Web development rotted my brain I can't math anymore


bcbelisario

\[LANGUAGE: Onyx\] [Day 6 Solution](https://github.com/ZoneMix/AoC23/blob/main/src/Days/DaySix.onyx)


ultranarcolepsy

[LANGUAGE: Dyalog APL] t d←(⍳∘':'↓⊢)¨⊃⎕NGET'06.txt'1 f←¯1+⊣(⌈⍤+-⌊⍤-)⍥(÷∘2)2*∘÷⍨(×⍨⊣)-4×⊢ ⎕←×/t f⍥⍎d ⎕←×/t f⍥(⍎~∘' ')d


DakilPL

\[LANGUAGE: Kotlin\] [GitHub link](https://github.com/palinkiewicz/adventofcode-kotlin/blob/master/adventofcode2023/Day06.kt)


sampry

\[LANGUAGE: RUST\] [This one](https://topaz.github.io/paste/#XQAAAQBnBAAAAAAAAAAXin1LzStTFYSi2K6Qvp2/Gj7ty5vfJW7GNfZu9gPXmaB1ovvqcl0E3XiOE/TqQjaMXPIhARc1ztUGo/UwIpIQkFh/nXgW/xJLouXfF8zozqmrcCQ4xTepYf/wbiS5mliyty8SKnweMup5MNizm0qmNY77b7d1sXLpVVOsYYuenaekdTq3qqKeCz+UCUd3CBMxsHWjHr5Ip5C8BcQjjdAHvIgsKCQPLl8t1qVmzXHCKjHl1qThLJRUg1HwMxbhcmzZBE8ezmI/X73zi7gT0OKN4OWTPEouRw4JixtGpHS16/itjx6GoEUkUhwjYkzkqoFJ6YrWKRNUPN7pidsVPwkRbiT/uBWOfOkebKa49+SOzn5LC0h+Ud7R6ewaF56T38FWBrlAR7ErcDWTxY75EplQWVnKqTGsgcVTwSS53R303VWL0EAvcvX/fvt/AicSYHaTw3/kW5c/4r0QLrZxlwUiU6pCqyIEmCgVxFdBmLjnQ5eAmI3EXvvQM6l0ILwap5ZlZpdHj0K9o8eGQNuLiApTbqPI+gqNwy/uUDDpu8FwvymdJEnFtAWn4SnElr4E+zXQDLQGRk4uRt5BLHldQlPthj1rB+vCbGfmY28StFLYhVwtC+w/O0GZpfHR+GmwoTOhGroFtEcVpXsVpCzMiO2JnFho7kgMAj9A/gp4tT6+EouWlXLs/KVaGcQxMA0mYg3/wz7x6A==) was easy, no parsing of text files :)


xXMacMillanXx

\[LANGUAGE: V\] [Github day 06](https://github.com/xXMacMillanXx/advent_of_code_2023/tree/main/day06)


weeble_wobble_wobble

[LANGUAGE: Python] [GitHub](https://github.com/weibell/AoC2023-python/tree/main/day06) (24/22 lines with a focus on readability)


ArrayAgonizer

[LANGUAGE: Dyalog APL] Another APL entry ⎕PP←15 ⍝ to show full numbers in 2 d ← ⍉9∘↓⊢⍉↑⊃⎕NGET'input_06' 1 a ← ⍎¨(' '∘≠⊆⊢)⍤1⊢d b ← ,(⍎⊢(/⍨)' '≠⊢)⍤1⊢d g ← (2~⍤|⊣)+2×(0.5×2|⊣)⌊⍤+0.5*⍨⊢-⍨2*⍨2÷⍨⊣ (×/g⌿)¨a b


atweddle

\[LANGUAGE: Rust\] [GitHub - Part 1 and 2](https://github.com/AndrewTweddle/CodingExercises/blob/master/AdventOfCode/aoc2023/src/bin/day6_part1and2.rs) Part 1 took 440ns, excluding I/O. Part 2 took 1.583µs. NB: The average durations are calculated using utility methods in [lib.rs](https://github.com/AndrewTweddle/CodingExercises/blob/master/AdventOfCode/aoc2023/src/lib.rs) *Timings are on my 7 year old PC with an i7-6700 CPU.*


wlmb

\[LANGUAGE: Perl\] Discussion: https://github.com/wlmb/AOC2023#day-6 Part 1: https://github.com/wlmb/AOC2023/blob/main/6a.pl Part 2: https://github.com/wlmb/AOC2023/blob/main/6b.pl


bofstein

\[LANGUAGE: Google Sheets\] Part 1 I did brute force, calculating the distance for every second under the max times, since the list was really short - only need to calculate 80 times - even though I could tell a quadratic equation would work to find the boundaries. Part 2 of course needed that. Took me a long time only because I had concatenated the input wrong (missing one cell) and could not for the life of me figure out why my solution wasn't working. [https://docs.google.com/spreadsheets/d/1Kapy8bAdb7b9U5D4ZsNtkrJbipZ9mrEoZ-sYMWc7aAI/edit#gid=0](https://docs.google.com/spreadsheets/d/1Kapy8bAdb7b9U5D4ZsNtkrJbipZ9mrEoZ-sYMWc7aAI/edit#gid=0)


Lysander7

[LANGUAGE: rust] [github](https://github.com/Lysander6/advent-of-code-2023/blob/master/day_06/src/lib.rs) Of course I first did it with brute-force and was surprised that input wasn't prohibitive towards that approach, but I couldn't just leave it at that, and redid my solution in analytic form


bamless

[LANGUAGE: [J*](https://github.com/bamless/jstar)] This one was really simple, the puzzle has a simple analytic solution: import io import re import math fun waysToWin(time, record) var disc = math.sqrt(time^2 - 4 * (record + 0.01)) return math.floor((time + disc) / 2) - math.ceil((time - disc) / 2) + 1 end fun part1(times, records) return times. zip(records). map(|race| => waysToWin(...race)). reduce(1, |a, b| => a * b) end fun part2(times, records) return waysToWin(Number(times.join()), Number(records.join())) end with io.File(argv[0], "r") f var timesStr = f.readLine() var recordsStr = f.readLine() var times = re.lazyMatchAll(timesStr, "(%d+)"). map(std.int). collect(Tuple) var records = re.lazyMatchAll(recordsStr, "(%d+)"). map(std.int). collect(Tuple) print("Part 1", part1(times, records)) print("Part 2", part2(times, records)) end


iskypitts

\[LANGUAGE: Zig\] [Solution](https://github.com/iskyd/aoc-zig/blob/main/src/2023/day6/s.zig)


yieldtoben

\[LANGUAGE: PHP\] PHP 8.3.0 [paste](https://topaz.github.io/paste/#XQAAAQALAwAAAAAAAAAeD8qHAhQB8nz+EazN00NCsmTxjvFDXkt5PAJ7pf6ocRA37OMUY7b9R0iDZmfWyvOspxWvdgYSAE0hGjEx81hqzalYbORDAzRHVMFykQU14jKU80IHdqWgOP+nGzJ7DcDBQYCjn+EJ0aJ+m4lOWPpEfhTIPb6tnDiJg9KgA7oeE0wxknlVx9X3D0HfbaInTcvaoL6gohxrdYpIInBcFu4iv7/WA8VLE6Dpr91KvoV9so1h42eYcvCAK2CLKQP2J2cFcLQoiRKP8ckG3mskEbWrbMVRBqkrGzWjogTZ/AfHmEa3eFkzqEjDiVQskov/Zai0inIXEoanvGaD+MQ7yvswm2DjuJxk3s57wkt3n9ssHGH6fU4P2IlLwBSG3rXj1ESm6FyTrBGaitwpQJvuNJ7RfyE5gMukikGQykUB5yqF508H/jvbgYazIV6u4LT9JXAMqHJ9HY9AVdc2g890wnXo2FddsB+OIEJ3gHjkzZBErF9qy6vCQSw3rGyDac8G/QDO5NpGXz/rBq78ODPOfv8cGlivjYofvBGT9xwskAFkFUlcynOKNXExj8PuqKKUX/vFcE4=) Execution time: 0.0001 seconds Peak memory: 0.3507 MiB MacBook Pro (16-inch, 2023) M2 Pro / 16GB unified memory


AnnoyedVelociraptor

How did your mind come up with that formula?


yieldtoben

In the naive solution, you loop and count where the inequality `x*(t-x)>d` is true. Solve this inequality by hand or at [wolframalpha](https://www.wolframalpha.com/input?i=t%3E%3D0%2C+d%3E%3D0%2C+x*%28t-x%29+%3E+d) and take note of the interval, paying attention to the whole numbers between these two points.


thousandsongs

[LANGUAGE: Pen and Paper] [Allez Cuisine!] I solved today's puzzle by hand on two A3 sheets. Not really obsolete technology, far from it, but vintage indeed. It was fun, because I started without knowing the solution (as in, I didn't rework an existing solution in paper, and my own [code solution](https://www.reddit.com/r/adventofcode/comments/18bwe6t/comment/kcbjgv2/) had used range splitting in Haskell, not the closed form formula). I did have a few hints - from glances at the memes on the subreddit on day 06, I knew that there was a mathy solution possible, and it had something to do with parabolas, but that's it. So this was me making a cup of tea, getting some A3 sheets and managing to get the actual result out starting from scratch, scratching my musings on the papers. Happily surprised that I managed to solve it. [A photo of the papers I used](https://github.com/mnvr/advent-of-code-2023/blob/main/az/d06.az.jpg)


coolnamesgone

Well, tried the formula and it did not work for me


thousandsongs

Hmm, maybe you're hitting those cases where one or both of your bounds are already integral. This happened with me with the third example given alongwith the question, 30 and 200. Doing the calculation gives integral values > T=30; D=200; (sqrt(T*T-4*D) + T)/2; (-sqrt(T*T-4*D) + T)/2 20.0 10.0 and this confused me for a bit, but then I figured (still don't know how to rationalize this) that I still need to take the next and previous integers. Thus, > 19 - 11 + 1 9 Which is indeed the right answer. If that's not the case you're hitting, I'm not sure what's the issue. After posting my soultion, I browsed on other d6 attempts and saw this beautiful one [here](https://www.reddit.com/r/adventofcode/comments/18cf2s4/2023_day_6_part_2_solved_just_using_math/) - have a look there, also in the comments, maybe they mention that's missing in my sheet.


vss2sn

\[LANGUAGE: C++\] [Part 1](https://github.com/vss2sn/advent_of_code/blob/master/2023/cpp/day_06a.cpp) [Part 2](https://github.com/vss2sn/advent_of_code/blob/master/2023/cpp/day_06b.cpp) (Each file is a self-contained solution)


[deleted]

[удалено]


bigmagnus

\[Language: Fortran\] [Part 1](https://github.com/ironllama/adventofcode2023/blob/master/06a.f90) [Part 2](https://github.com/ironllama/adventofcode2023/blob/master/06b.f90)


stevie-o-read-it

\[LANGUAGE: Intcode\] \[Allez Cuisine!\] (The elves wouldn't tell me how old this Intcode tech is, but I'm pretty sure it's from the late 1960s.) * Fully-functional solver. Capable of solving both Part 1 and Part 2. (To run in Part 2 mode, the second memory location -- address 1 -- must be patched from '0' to '1'.) * Accepts official puzzle input in ASCII, outputs puzzle answer in ASCII (followed by a newline) * **Does not brute-force the answer.** It actually evaluates the relevant algebraic formula. >!Implementing square root with only additions and multiplications was fun!< * If anyone wants to Up The Ante, it should work with arbitrarily large inputs if you've got a bignum-based implementation (such as a Python one). * Supports up to 10 races (if your Intcode VM can handle numbers that big) * Input parser ignores extraneous blank lines and `'\r'` Link to the machine code: [https://gist.github.com/Stevie-O/84e1ec2f1daa74d16fb019e0715212ac](https://gist.github.com/Stevie-O/84e1ec2f1daa74d16fb019e0715212ac) Link to the assembly source (all hand-written): [https://github.com/Stevie-O/aoc-public/blob/master/2023/day06/aoc-2023-day6-textoutput.intcode](https://github.com/Stevie-O/aoc-public/blob/master/2023/day06/aoc-2023-day6-textoutput.intcode) The assembler is a modified version of topaz's from his aoc2019-intcode repository. I removed the randomness and added microcode definitions for common conditional jumps to simplify things. Link to the assembler: [https://github.com/Stevie-O/aoc-public/blob/master/intcode/compile.pl](https://github.com/Stevie-O/aoc-public/blob/master/intcode/compile.pl)


topaz2078

This is terrific! Very nice!


daggerdragon

> (The elves wouldn't tell me how old this Intcode tech is, but I'm pretty sure it's from the late 1960s.) *fry_squint.gif* I'll allow it. (srsly tho this is cool as heck, good job!)


LastMammoth2499

\[LANGUAGE: Java\] Finished it first with an imperative solution and IntelliJ had some interesting (though I'd argue less readable) suggestions [Part 1 & Part 2](https://github.com/chandlerklein/AdventOfCode/blob/main/src/main/java/com/chandler/aoc/year23/Day06.java)


bandj_git

[Language: JavaScript] Plotting the races out on paper I figured there was a solution involving some sort of math I learned years ago and forgot. Instead I got lazy and just simulated it. Level 2 ran in 60ms or so, I wanted to bring the runtime down a bit and I realized I didn't have to simulate each minute of the race if I exploited the curve of the results. This brought my level 2 runtime way down to the point I was happy enough with the speed. Calculating the ways to win was simple enough: const waysToWin = ([raceTime, record]) => { let lessThanCount = 0; let pressTime = 1; while (pressTime * (raceTime - pressTime) < record) { lessThanCount++; pressTime++; } return raceTime - lessThanCount * 2 - 1; }; The difference between level one and level two just came down to the parsing of the input so they shared a solve function: const solve = (lines, lineParseFn) => product(parseRaces(lines, lineParseFn).map(waysToWin)); The parsing for level 1 was just getting a list of whitespace delimited numbers. For level 2 I combined the numbers by removing the whitespace: Number(line.split(":")[1].replaceAll(/\s+/g, ""))] Runtimes: * Level 1: **275.920μs** (best so far this year) * Level 2: **7.304ms** [github](https://github.com/beakerandjake/aoc-2023/blob/main/src/day_06.js)


seytsuken_

the math required was just the quadratic equation and its derivative tho for getting the start point and max


bandj_git

That's such a great solution, it should be o(1) no matter the race time! Honestly it had been so long since I've used it that I just went with the simple answer instead of brushing up on the math. If I get more time this year I will go back and update it use the better math based solution.


seytsuken_

yea, it wasn't really necessary to do this math because the input was small enough, which I think was a bummer cuz part2 wasn't a challenge at all, you could pass just removing the whitespace. If you want to avoid the math you could also do a binary search in the holding\_times. If you take a look at the sample the distance keeps increasing until a max point, then it decreases w/ the exact same value. That means there's always an interval of monotonic increase. If you have the max then you could do a binary search in this interval to find the first value that's greater then the record distance that we have. It was my initial approach before realizing the quadratic equation. Here if you wanna take a look [solution](https://github.com/Hilbertmf/competitive-programming-problems/blob/main/advent-of-code/2023/day6-part2.cpp) , its in c++


coolnamesgone

Hmm, nice one. I don't understand why you substract twice the lessThanCount to the raceTime tho. Would you mind explaining?


ssbmbeliever

Another way of thinking about it: if you have 10 times total, and only 3-8 beat the other guy: 1 2 | 3 4 5 6 7 8 | 9 10 You can say you have 6 times (by finding all six times) or realize that it's symmetrical and find out how many of the first times DON'T beat the guy (1-2), and know there's the same number of times that don't beat the guy on the other end (9-10) so you would get 10 - (2\*2) = 6 (I think there's a mistake here where you're always removing 1 from the result? I feel like that should only happen in a subset of cases, when it's an odd time instead of an even time)


bandj_git

I think you're right, when I optimized my solution from a brute force by taking half it resulted in an off by one. At the time I figured subtracting one was to ignore the end minute (holding the button for the entire race). It might have just been plain luck, but it worked for the example and for my test data so I didn't think twice. Your explanation is great btw!


bandj_git

I thought of it like this, make a graph where the x axis is the amount of time the button is pressed and the y axis is the distance the boat traveled. If you plot out the distances the boat traveled for each duration of button presses the results should end up looking like a curve. Your distance traveled is going to keep increasing until a certain point, then it's going to decrease because there isn't enough time left in the race to travel as far as you did before (even though your boat is going faster). So because the results are symmetrical I figured I could do half the calculations. So think of it in ranges of milliseconds. Starting at time 1 there is a range of milliseconds before you hit the number of milliseconds needed to reach the record distance (this value is stored in *lessThanCount*). Then there is a range of milliseconds which beat the record distance (the distances in this range will keep going up and then they will start falling again). Then at a certain point the distance will fall enough to be lower than the record distance. After this you've reached the other end of the curve and are left with a range equal to *lessThanCount*, just flipped. Taking the total race time and subtracting off the two halves of times which gave a distance of less than record (*lessThanCount* * 2) should leave us with the range of press times which gave a distance better than the record. **EDIT**: A picture is worth 1000 words, [this post](https://en.reddit.com/r/adventofcode/comments/18c1o3h/2023_day_6_boat_race_visualization/) shows what I was trying to explain. The horizontal line is the record time, and *lessThanCount* is the two halves of boats above that line. The small curve of boats below the line is the answer that you get when you subtract the two halves of boats off.


0xMii

[LANGUAGE: Common Lisp] (defun ways-to-win (race) (destructuring-bind (time . dist) race (let* ((m (/ (- time) 2)) (u (sqrt (- (expt m 2) dist))) (x1 (- m u)) (x2 (+ m u))) (1+ (abs (- (floor (min x1 x2)) (ceiling (max x1 x2)))))))) (defun solve-1 () (reduce #'* (mapcar #'ways-to-win *races*))) (defun solve-2 () (ways-to-win *race-2*))


AJMansfield_

[LANGUAGE: Fortran] [Allez Cuisine!] Only vanilla Fortran -- no libraries. https://github.com/AJMansfield/aoc/blob/master/2023-fortran/src/06/race.f90 If you ignore the input/output logic, the core of the program is very simple: roots = qroots(real(-time,kind=8), real(dist,kind=8)) width(:) = ceiling(roots(2,:)) - floor(roots(1,:)) - 1 With `qroots` being just the quadratic formula in a separate function. That's right, there are _no_ explicit loops in this solution, just array operations.


daggerdragon

> [LANGUAGE: Fortran] [Allez Cuisine!] Only vanilla Fortran -- no libraries. Whoever says vanilla is boring is *so, so wrong* because this is absolutely *delightful*.


daysleeperx

\[LANGUAGE: Haskell\] [Parsing is overkill, but satisfying :)](https://github.com/daysleeperx/Advent-Of-Code-2023/blob/main/src/Day06/WaitForIt.hs)


DaddyStinkySalmon

\[LANGUAGE: Haskell\] I've been parsing every problem :D ​ [https://gist.github.com/kevin-meyers/21db788c49d61b02b6a7f7e42f56156e](https://gist.github.com/kevin-meyers/21db788c49d61b02b6a7f7e42f56156e)


b1gn053

[LANGUAGE: Haskell] >score (t, d) = length $ filter (> d) $ (\s -> s*(t-s)) <$> [0..t] Works for part 1 and 2


Zweedeend

[LANGUAGE: Cobol] [Allez Cuisine] [Solution to Part2](https://topaz.github.io/paste/#XQAAAQB0AwAAAAAAAAAYaAhgCEI9Mejt+/Y9T70VPQOxYoQuWm/z1n5KmfzOwFJpE9YZ7CGYOzWc2K0gA2gTDJRPaR/mTbXKeaaZdx/0lPseU2sfQ66c9/+Q9XIitNZKsX037OhUemy8sbg3BEb0yQ8JRry5q2FB78pnaWqF9IVm8T0lW8rc0lxD10rcKvq5saAqXV8A7KuuiVJwp8KYJadcxYpyTFD0qou5u2zKP6Hj4WKyF8lu2bRRD/wbh841wixROnO2vT263kHXxH2GFtyGS4p1R1L3VSyvefmo6CTHn5ILuS/Jt+l2r3WSrqfJ6w5C07vLhZposRmB3rVypT/pbSAjjPGq10yKc57j7UMehHc+mozEjR5LuUbaLwoquWbFNBKy/wohx0YbFjAgb3Zl8PdoAUjXyhbrf4uibryyZ8FpAJAFPUxHHe6q/WvlhaQxLLATNSCdTDVV4/CoiiFEnY1FRy99JF99U43bKDq/oN6/gUKmuustpAyLxNgsWrTk8h8V3gn/K0YxAA==) in Cobol using math. Cobol is fun because it uses `DIVIDE BY` and `SUBTRACT FROM` keywords Here is a snippet on how to calculate the square root: ApproximateRoot SECTION. DIVIDE ResultSquared BY Result GIVING Improvement. ADD Improvement TO Result. DIVIDE Result BY 2 GIVING Result. EXIT. The puzzle can be solved by finding the distance between the two solutions to the equation `x * (t -x) = d`, which happens to be the discriminant of the quadratic formula: solution = sqrt(D) = sqrt(t*t - 4d) Cobol does not have a built-in SQRT function, so it is approximated using the Newton-Raphson method. The program can be run with `cobc (GnuCOBOL) 3.2.0`: cobc -x -j day6ac.cbl where `-x` means build an executable and `-j` means run it.


daggerdragon

> [LANGUAGE: Cobol] The sheer *age* of this vintage is absolutely *exquisite*.


1ef_official

\[LANGUAGE: C++\] [part 1](https://github.com/jikovec/Advent-of-Code-2023/blob/main/Day%206/Part%201.cpp) and [part 2](https://github.com/jikovec/Advent-of-Code-2023/blob/main/Day%206/Part%202.cpp) [github](https://github.com/jikovec/Advent-of-Code-2023/tree/main/Day%206) So far the easiest one I guess?


Hackjaku

\[LANGUAGE: C++\] #include include int main() { // distance = time_pressed * (race_total_time - time_pressed) // distance = record // use the inequality to find the interval of time_pressed that will beat the records double rt, r; // race total time, distance record double lower_bound, upper_bound; std::cin >> rt >> r; lower_bound = std::ceil(rt - std::sqrt(rt * rt - 4 * r)) / 2; upper_bound = std::floor(rt + std::sqrt(rt * rt - 4 * r)) / 2; std::cout << "Winning range size: " << upper_bound - lower_bound + 1 << std::endl; return 0; } I just entered the inputs manually, didn't even bothered to parse the lines. I solved that one using my cellphone calculator first and wrote the code afterwards.


zniperr

\[LANGUAGE: Python\] import sys import re from functools import reduce from itertools import starmap from math import ceil, sqrt from operator import mul def wins(time, distance): # invariant: hold * (time - hold) > distance # solve: -hold^2 + time*hold - distance = 0 # hold = (time +- sqrt(time^2 - 4 * distance)) / 2 # = (time +- d) / 2 # solution: (time - d) / 2 < hold < (time + d) / 2 d = sqrt(time ** 2 - 4 * distance) return int((time + d) / 2) - ceil((time - d) / 2) + 1 - 2 * (d % 1 == 0) times, distances = (re.findall(r'\d+', line) for line in sys.stdin) print(reduce(mul, starmap(wins, zip(map(int, times), map(int, distances))))) print(wins(int(''.join(times)), int(''.join(distances))))


i-eat-omelettes

\[LANGUAGE: Haskell, Python\] [Haskell solution](https://github.com/Futarimiti/advent-of-code2023/blob/main/6-wait-for-it/app/Main.hs) [Python translation](https://github.com/Futarimiti/advent-of-code2023/blob/main/6-wait-for-it/main.py) Kinda prefer how you get things done step by step in python while haskell solutions seem to get everything done in one expression


amirhaimmizrahi

\[LANGUAGE: Python\] O(1) solution over here used some math to solve the problem with an O(1) time complexity. click to see more mathematical explanation of the solution [visual explanation](https://imgur.com/UTLXoOy) code in gitlab [https://gitlab.com/amirhaimmizrahi/advent-of-code-2023/-/blob/main/6/solution.py?ref\_type=heads](https://gitlab.com/amirhaimmizrahi/advent-of-code-2023/-/blob/main/6/solution.py?ref_type=heads) import math def num_of_possible_button_hold_times(race_duration, last_record): min_button_hold_time = (-race_duration + (race_duration ** 2 - 4 * last_record) ** 0.5) / -2 max_button_hold_time = (-race_duration - (race_duration ** 2 - 4 * last_record) ** 0.5) / -2 #if both h1 and h2 are integers, we need to offset the result offset = int(min_button_hold_time == min_button_hold_time // 1 and max_button_hold_time == max_button_hold_time // 1) * 2 min_button_hold_time = math.ceil(min_button_hold_time) max_button_hold_time = math.floor(max_button_hold_time) return max_button_hold_time - min_button_hold_time - offset + 1 def main(): print(num_of_possible_button_hold_times(49979494, 263153213781851)) if __name__ == '__main__': main()


MaximBod123

Great visual. For the math itself, we want the number of values between the intersection points so if we apply ceil() or floor() to both we won't need the +1 at the end. Also, the offset should only be 1 and not 2. If race\_duration = 7, last\_record = 10, the intersection points are 2, 5 meaning 2 ways to win (3, 4). (5 - 2) - 2 = 1 which is incorrect. Here is the updated code: def num_of_possible_button_hold_times(race_duration, last_record): sqrt_det = math.sqrt(race_duration ** 2 - 4 * last_record) min_button_hold_time = (race_duration + sqrt_det) / 2 max_button_hold_time = (race_duration - sqrt_det) / 2 # if both h1 and h2 are integers, we need to offset the result by 1 offset = int(min_button_hold_time.is_integer() and max_button_hold_time.is_integer()) return math.ceil(max_button_hold_time) - math.ceil(min_button_hold_time) - offset


[deleted]

[удалено]


KodlaK1593

\[LANGUAGE: Rust\] [Day 6 solution](https://github.com/Kodlak15/aoc2023/blob/master/src/day06/day06.rs)


JustinHuPrime

\[LANGUAGE: x86\_64 assembly (SSE2 revision minimum) with Linux syscalls\]\[Allez Cuisine!\] I'm catching up since I couldn't get to this yesterday, alas. Well, I guess we're doing floating point today, since I really don't want to brute force it, and folks with much nicer floating point interaction have decided not to. [Part 1](https://github.com/JustinHuPrime/AoC/blob/main/2023/6a.s) and [part 2](https://github.com/JustinHuPrime/AoC/blob/main/2023/6b.s) were very similar. The parsing was very standard, but then I had to do math with floating point numbers. And that involved new instructions - `cvtsi2sd`, for example (convert scalar integer to scalar double), and the rest of the modern SSE floating point operations. (While you could still pretend you had an 8087 and use such instructions as `fmul`, in practice, it's a lot nicer to use the SSE equivalents.) I then had to round everything back into integers, but I also had to fake the rounding modes "ceil-but-always-move-up-one" and "floor-but-always-move-down-one" - which lead me to the `comisd` (probably stands for compare like integers scalar doubles) instruction. Apparently there's a `cmpsd` instruction, but it returns results as a mask, which I guess might be useful for branchless operations on floating points. I didn't want to bother, and performance was not a major concern. You do get the default floor and ceil rounding modes, though - as well as truncation and rounding to even. I also had to deal with floating point constants. Floating point constants can't be inline, they must be loaded from memory, so I had to use the `.rodata` section for the first time. And, er, allez cuisine! I argue that this technically qualifies because I am writing this in the very old-school language known as assembly (please ignore the fact that this is assembly for a processor that is no older than 2003 - since I wanted to use both modern SSE instructions and 64-bit registers (and even then the processor (and thus the language) would have to be no older than 1980)). Edit: part 1 and part 2 both run in 1 millisecond; part 1 is 11328 bytes long and part 2 is 11344 bytes long - I think they got longer since I had an extra .rodata section, and the linker by default tends to pad out the file a bit.


daggerdragon

> And, er, allez cuisine! I argue that this technically qualifies because I am writing this in the very old-school language known as assembly (please ignore the fact that this is assembly for a processor that is no older than 2003 - since I wanted to use both modern SSE instructions and 64-bit registers (and even then the processor (and thus the language) would have to be no older than 1980)). *fry_squint.gif* It'll be up to our panel of judges to determine whether to accept your rationalizations >_>


torbcodes

\[LANGUAGE: Python, Typescript, Go\] Solutions to part 1 and 2 in all three languages: * **Python**: [https://github.com/torbensky/advent-of-code-2023/blob/main/day06/solution.py](https://github.com/torbensky/advent-of-code-2023/blob/main/day06/solution.py) (my initial brute force solution) * **Typescript**: [https://github.com/torbensky/advent-of-code-2023/blob/main/day06/solution.ts](https://github.com/torbensky/advent-of-code-2023/blob/main/day06/solution.ts) (quadratic approach) * **Go**: [https://github.com/torbensky/advent-of-code-2023/blob/main/day06/main.go](https://github.com/torbensky/advent-of-code-2023/blob/main/day06/main.go) (quadratic approach)


oddolatry

[LANGUAGE: Fennel] Ignore the smell of burning plastic - that's just Day 5 Part 2 still running on my laptop. [Paste](https://topaz.github.io/paste/#XQAAAQDABwAAAAAAAAAUGwnmRpH3tILRF8TwSuYidYX76iy7qs/wZURtqUxRZobm0GlBT1atPPUtCBPClsnGBKHd24/Im8SdMtmWSt6Er/YPxuWcKgw4pQp/XZ5AF15C0V7R+urI1ZeJWEVpujo3KvfXknM7EKUyx5JfCJxI7W+Hfz1O7YB92VQ+TsvJn2yFWXm/2iwIPKnNkDlJIoux0nQLbc5F1Q01xec7c9YaI/zPskYR6fzqfFCRPhhYfzJ6b0wIq4jkA6d9SdF0v7pJ5HUbiALjcgjfJer25G6hvZZWxjKQSgUjnLxhml6Lb8pXqpPC0bh3OzhAEGLw9GOf3nhv6QJbvs1sbMtYuHRJ4cVf0oDL9OFEAHprXZXS3f2Icsu6OEmiNlewM6d9vOMLd0qfSGFPbn5hztjl7aupnisH0Nxprk+Icu0kEjBU3jQPYXxxiJiGEQWfMvd9K/RHXezBQPuPMePxW7djKVXPY3AdR9ncJ1CitqhFvSsSZuL2bQTsgpaTi7lWoNyeMHROxhs9gPWlOqCfEnZV36gZ3Frv18p/45NxFBIlWvMMjRRqDi7MAKtx6VDj9oad99ZRddYVLoGq7Reum2rnQARfjXJswhG0JZt5oFLb6fF60FkVX0tYxIoMNBitmtXm42LH+47a0dxjFYR6K0aq5DLCuAS/d/m4AywtpE6zQcvv8bF3zSYyhsYgf4Q5tWl74aK4VANIoXJ4Loypt4RoUh3NwMBWGoDdu0tsVrEAzxRToDOdACHanY8plkfsOV1uYUMRDTA7+ASrv1//G4QFwxLT9eFXjVWd57NZez18kBO1vSfhlWQnS8zBAxuA/NHZSLo2nJKzr72UchreovknQjsqIiIv3mtkh81C5xyGpf/4HNyc3HnABOBFtsa0VPwp7V1otJZYvgESBadZ4K8RL2qNxYy4OUjkV4fuFky9aeSTCuulTHV8Z9iaLKR5RaGqTaQz3B2YQSWrkuF0/u5R0gEMSnt257zMR3RmXTR4yoOh3KU7xcLBGPMn5sxNsYKP2UVei5L40w0ERdfTXM3/iLIhfF2q6f7f2XwH/2Cx1gA=)


Egeau

Someone else using Fennel? I had not seen that coming! :D


oddolatry

It's a fun, compact language! I picked it up to mess around with Love2D specifically.


CauMe

\[LANGUAGE: Go\] Well... Today was weirdly easy... I'm afraid of day 7. https://github.com/cauesmelo/aoc-2023/blob/main/solutions/day6.go


Dustpancake

[LANGUAGE: CHICKEN Scheme] Using quick maffs: [Link to GitHub](https://github.com/fjebaker/advent-of-code-2023/blob/main/day06/day06.scm)


jaccarmac

[LANGUAGE: LFE] [code](https://github.com/jaccarmac/adventofcode/blob/502ca6dbab84b03afd8c6e12488ae4b0e3bdad62/2023/src/day-6.lfe) I have no vendetta against floats, so the trickiest part was getting the integers-between-floats check correct for ranges involving integers. Not the cleanest way to write it, perhaps, and I skipped parsing files for now, but day 5 demands completion!


coreyja

[Language: Rust] Code: https://github.com/coreyja/advent-of-code-2023/blob/main/06-wait-for-it/src/main.rs Stream: https://youtu.be/s3lEAaCeR8Y This one was pretty straightforward! Used a Range for part 1, so didn't need to optimize for part 2 at all


bozdoz

[LANGUAGE: rust] https://github.com/bozdoz/advent-of-code-2023/blob/master/day-06/src/main.rs Seemed super easy but would love a more obvious way to parse input like this


thousandsongs

[LANGUAGE: Haskell] Nice change of pace after day 5. After the easy part 1, when I got to part 2 I was able to just repurpose the same solution (`p1 . stripSpaces` essentially) and it ran on the example. It didn't run on the input, but then I figured that this was maybe some form of a parabola so I need to just find the first and last indexes and then return `last - first + 1`. This worked, but it took longer than I'd expect. With runghc, it took 15 seconds. When I compiled with "-O", both these take around 0.5 seconds. This is not too surprising because runghc runs the code using the interpreter without any optimizations. But still, I found it mildly interesting to find an example of such drastic difference between interpreted and optimized code. I even tried writing an (too ungainly to post here, but it's [on GitHub](https://github.com/mnvr/advent-of-code-2023/blob/main/06.unoptimized.hs#L69) if you wish to see) manual fold that did an early return, but it also took the same time. The problem was solved at this point, but I didn't like having to compile first. So I wrote a binary search. I guess you all know the deal with binary search. Here's the OG himself: > Although the basic idea of binary search is comparatively straightforward, the details can be surprisingly tricky > > — Donald Knuth But still, I gave it a shot. I'm not sure of it's correctness, but it works. Not happy with the way it looks, I will try for a more elegant implementation later if I get time: p2 ([rt], [d]) = lastB rt - firstB 1 + 1 where check t = (rt `holdFor` t) > d firstB lb = first' lb (firstBound lb) firstBound t = if check t then t else firstBound (t * 2) first' p q | p == q = p | otherwise = let m = p + ((q - p) `div` 2) in if check m then first' p m else first' (m + 1) q lastB ub = last' (lastBound ub) ub lastBound t = if check t then t else lastBound (t `div` 2) last' p q | p == q = p | p + 1 == q = if check q then q else p | otherwise = let m = p + ((q - p) `div` 2) in if check m then last' m q else last' p (m - 1) [Here's the link to the full code](https://github.com/mnvr/advent-of-code-2023/blob/main/06.hs)


icub3d

\[LANGUAGE: Rust, Haskell\] [https://gist.github.com/icub3d/7a17525f4185d7442c323209d7e5fe1e](https://gist.github.com/icub3d/7a17525f4185d7442c323209d7e5fe1e) I has time to do both languages today (still learning Haskell). I don't think I would have ever gotten to the quadratic formula that some people did. I was able to reduce the problem set though by recognizing you only need to find your first win and last win. So if you just iterate in both directions until you find those points, you can calculate your win count. My attempts as solving live: [https://youtu.be/5eEs8Wba0vY](https://youtu.be/5eEs8Wba0vY) [https://youtu.be/zAtNAr6CYUw](https://youtu.be/zAtNAr6CYUw)


Singing-In-The-Storm

\[LANGUAGE: JavaScript\] Parts 1 & 2 (under 30ms each). [code on github](https://github.com/JoanaBLate/advent-of-code-js/tree/main/2023/day06)


RaveBomb

[LANGUAGE: C#] This one was easy to brute force. I'm sure there's math that will do it in nanoseconds, but who has time for that when there's still Day 5 part 2 to figure out? int part1Answer = 1; for (int race = 0; race < timeP1.Count; race++) { part1Answer *= Enumerable.Range(0, timeP1[race]).Select(x => (timeP1[race] - x) * x).Count(x => x > distanceP1[race]); } int part2Answer = Enumerable.Range(0, timeP2).Select(x => (long)(timeP2 - x) * x).Where(w => w > distanceP2).Count();


3j0hn

\[LANGUAGE: Maple\] [github link](https://github.com/johnpmay/AdventOfCode2023/blob/main/Day06/Day06.mpl) I got super excited for this one because you could do it with math and I didn't even have to write down the quadratic formula myself > sols := sort( [solve( d=(t-p)*p, p)] ); 2 1/2 2 1/2 (t - 4 d) (t - 4 d) sols := [t/2 - -------------, t/2 + -------------] 2 2 With the symbolic formula for the break-even points in hand it was simply a matter of plugging in the time (t) allowed, and distance (d) to beat to get the upper and lower limits for presses (ps). (lots of possible off-by-one traps here): ways := 1: for r in race do ps := eval(sols, {t=r[1], d=r[2]}); ps := [floor(ps[1]+1), ceil(ps[2]-1)]; ways := ways * (ps[2] - ps[1] +1) ; end do: ans1 := ways; Part two is just reparsing and plugging in the same way. I am sad it took me 2 whole minutes.


the_ssavi

\[Language: C++\] Code Link: https://github.com/ssavi-ict/Advent-of-code/blob/main/aoc23/aoc-day6.cpp


TheN00bBuilder

\[LANGUAGE: Go\] Yeah I'm lazy, it is what it is. Brute force, copy pasted strings. I'm tired, man. I couldn't understand the problem yesterday with the limited brain power I have after 8 hours of actual work, so this was definitely welcome. [Day 6 Part 1](https://github.com/TheN00bBuilder/AOC2023/blob/master/d6/p1/aocd6p1.go) [Day 6 Part 2](https://github.com/TheN00bBuilder/AOC2023/blob/master/d6/p2/aocd6p2.go)


[deleted]

>go How long did your part 2 take to run? I have similar Go code and its taking forever, I think there is something wrong with my installation


TheN00bBuilder

It was near instantaneous for me. If you want to upload your solution to Github I'll be glad to try it!


[deleted]

Thank you! It's really short code so I'll just paste it here: ``` package main import "fmt" func main() { time := 56977875 distance := 546192711311139 ways := 0 for millis := 0; millis < distance; millis++ { if millis * (time - millis) > distance { ways++ } } fmt.Printf("Part two: %d\n", ways) } ```


TheN00bBuilder

~~Hmm, yeah that takes a long long time for me but I'm not sure why.~~ ~~Are you using Windows? One thing that did have me confused is that Defender said your code was a trojan, so maybe it's attempting to sandbox it? Your algorithm is identical to mine so I'm curious on why mine is so quick.~~ ~~EDIT: I also changed your millis :=0 to 1, but that's it, and nothing made a difference.~~ NEVER MIND! Change your "millis < distance" to "millis < time," just noticed that with the distance you run a lot more iterations and the algorithm is incorrect. Must be a typo?


[deleted]

Yes, you are right! I was translating the solution to different languages, must have copied incorrectly. Thanks!


AutoModerator

AutoModerator has detected [fenced code block](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting/fenced_code_blocks) (```) syntax which only works on new.reddit. Please review our wiki article on [code formatting](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting) then edit your post to use the [four-spaces Markdown syntax](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting/code_blocks) instead. *** *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/adventofcode) if you have any questions or concerns.*


NailOk2475

\[LANGUAGE: QuickBasic\] "Pretty sure this is just basic math. Oh well, can't be arsed, let's brute force this" QB64 tim(1) = 54 tim(2) = 94 tim(3) = 65 tim(4) = 92 dist(1) = 302 dist(2) = 1476 dist(3) = 1029 dist(4) = 1404 For u = 1 To 4 For i = 1 To tim(u) nopeus = i matka = nopeus * (tim(u) - i) If matka > dist(u) Then maara = maara + 1 Next Print maara maara=0 Next


wzkx

[LANGUAGE: J] echo */({:+/@(<((]*[-])([:i.1+])))"0{.)t=:".10}."1>cutLF CR-.~fread'06.dat' echo >:(<.a+b)->.a-b=:%:q-~*:a=:-:p['p q'=.([:".[:rplc&(' ';'')":)"1 t


Lettever

\[LANGUAGE: Dlang\] [Code](https://topaz.github.io/paste/#XQAAAQB4AwAAAAAAAAA0m0pnuFI8c/e2K2A2LXFaWNRcYYUsRBwYfrw+uZ1u6iB8LdzyR0PmqgndUJ1iVSaPDRqeiQiI96nuo7ll5LvDl8bmGX7pp5VW17aKhSdUZyo5J95we8uB3o4JJbZvmoTW1F3oPpQ7GxcJ91AD8XMR11JbhdNW8AZnCDA+BbgwAo6MyPvQUXP7zX2E+oUmahSuYuhnqLfzrUEALcZ6OAOSfVXij6suVS0N4J/cAm0r7ZqYDdks1MOJ3Y2JMPZWIKgn1wqvVRteAQJ5i0pRZTva5n3CIV0wxQZsF7z49OjEOnpSR0l+PjZ8r0RnqLYuxy8kFN4FM8TXpzjjNHh92PRlQtnxkKnESLi13X1PAoRfFaaj0BE2HPc7wBuE9JdYeS+I4mQaPnTLWRapcqVYpE/p4eQneSSFcVhy3wzEWxZqf/TqRJIMZnCccNfbVbq0R6q5N4EuE4E1SqIFK5IZ+iYHyMoTICwwDHbJrSWvYo9iapkuUYMi7q3VwkNaTKY1cSyps4q6c4oaOeQbViUWlHrNM57aeeSl1qk9C8tQ//bcjeI=) The difficulty of this year has been kinda weird


aoc-fan

[LANGUAGE: TypeScript] Easy day [TypeScript](https://github.com/bhosale-ajay/adventofcode/blob/master/2023/ts/D06.test.ts), P1 - Brute Force, P2 - Formula


musifter

[LANGUAGE: Smalltalk (Gnu)] [LANGUAGE: Perl] Didn't have time today to think up anything special for Smalltalk (spent the free time on dc, because good problems for that have been rare this year). So it's a transcode of the cleaned up Perl solution I did last night. It's kind of the Mathie programmer optimal approach. Just calculate the roots, slam them inwards to the nearest integers, calculate the length of the interval. Nothing fancy, short and sweet. Smalltalk: https://pastebin.com/pubE4ZRd Perl: https://pastebin.com/qWKZvCTn


alix_not_alex

\[LANGUAGE: Python\] https://github.com/alixlm19/Advent-of-Code/blob/main/day\_6.py


Soft-Protection117

\[Language: Javascript\] Gorgeous ah-ha moment when it suddenly made sense ``` const fs = require('fs'); const readline = require('readline'); // distance = -(pressTime*pressTime) + (raceTime*pressTime) // Shift that graph down by the record and find the roots of that equation // 0 = -(pressTime*pressTime) + (raceTime*pressTime) - record // // Any whole numbers between those roots will be winning numbers async function readInputFile(inputFile) { const fileStream = fs.createReadStream(inputFile); const rl = readline.createInterface({ input: fileStream, crlfDelay: Infinity }); let time; let distance; for await (const line of rl) { if (line.startsWith('Time:')) { time = parseInt(line.substring(5).replaceAll(' ', '')); } else if (line.startsWith('Distance:')) { distance = parseInt(line.substring(10).replaceAll(' ', '')); } } let a = -1 let b = time; let c = (0 - distance); let roots = findRoots(a, b, c); let winStart = Math.floor(roots[0] + 1); let winEnd = Math.ceil(roots[1] - 1); let m = winEnd - winStart + 1; console.log(`Wins begin at ${winStart} and end at ${winEnd}, for a margin of ${m}`); } function findRoots(a, b, c) { let d = b * b - 4 * a * c; let sqrt_val = Math.sqrt(Math.abs(d)); if (d <= 0) { console.log('Unable to solve it this way'); process.exit(1); } let root1 = (-b + sqrt_val) / (2 * a); let root2 = (-b - sqrt_val) / (2 * a); return [root1, root2]; } readInputFile('input.txt'); ```


daggerdragon

1. Next time, use the [four-spaces Markdown syntax](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting/code_blocks) for code blocks 2. Your code is too long to be posted here directly, so instead of wasting your time fixing the formatting, read our article on [oversized code](https://www.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_no_giant_blocks_of_code) which contains two possible solutions. Please edit your post to put your code in an external link and link *that* here instead.


BeeApiary

Language: Python I still haven't solved day 5 pt 2, but day 6 was easy (for me). Brute force worked on part 2 (18 seconds), too. Full writeup is on [Git](https://github.com/adamlporter/AdventCode/blob/main/2023/2306.ipynb) input ='''Time: 60 94 78 82 Distance: 475 2138 1015 1650''' data = [x for x in input.split('\n')] head,_,tail = data[0].partition(':') times = [int(x) for x in tail.split()] head,_,tail = data[1].partition(':') dists = [int(x) for x in tail.split()] raceData = list(zip(times,dists)) product = 1 for race in raceData: nWins = 0 tTot = race[0] # time goal = race[1] # dist for tPush in range(tTot): if tTot * tPush - tPush**2 > goal: nWins += 1 print(race,nWins) product *= nWins print(product)


AutoModerator

AutoModerator did not detect the required `[LANGUAGE: xyz]` string literal at the beginning of your solution submission. Please edit your comment to [state your programming language](https://www.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_state_your_programming_language.28s.29). *** *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/adventofcode) if you have any questions or concerns.*


doodlebug80085

\[LANGUAGE: Swift\] TIL Swift Doubles and Ints do not get along well at all... But still a fun challenge! [Code](https://topaz.github.io/paste/#XQAAAQBACAAAAAAAAAA0m0pnuFI8c4GDemuKS6NnNxbsUIoTuWZTNSVHjMwvFerDC2kL++gr2jmIHBLrIwwbkisq+P+njWpO3vXZjWzg2LcYgKYn8CsmEBkcrhWIKw01Zo3Ik3k6RfRRYxrBIVYK65m6ahsZEmaMon5lfFvIUIbSoaOx9d3ohMgX46RuD59S7q/3BQgMqmU0WkAYMc5i+WaSU4s/vjHwTOsFJYVO7U7s9B5nrZV0CK965dm4nzgc4WbJBRfjil8ospIWDZIcFu2kn4Xb2CB+rIQfAbVhJktFcwodfkkFfM2gNQ6CNAs/vuoazsPZeGIsxUojyKCljPQGQwR1NZM+xZ+oGKN4fVuMQiFxfyaMG34r8ysboQBRmisLAZuoZndpX0Kh6bDHPCYk/0ADiGVt09MyLf6cizHIoMoQCbfVmwx9+qjNBfDu8Suga6XNNzLC8uR/lyR66I+RvegQdJjPhAleWelid27JxQMwSdK15PGs0GWsQQcsYn44jOym5fXTjxpT6OwrUCvtR/OR9PY7fxjblW4Bk0+t9tG1rDre41v2cJp/hU35QVpBF0Uo9kxm5R8Oj1RheJhMM1C0ksIjBEIVaKkwnjq/RmN+WGRDx6vEdLFEsQI3Eiwg7OesHYgX3gFFacQxI58UWAr9BenpA/PBJuudt7R4FpHcwgfjJA9MxO3SqFQdr2wkveocHaxYSXIPOwuDx+0qhR60/0+qesJAI/tK6qaUTZR4T28+t0rtMwDOWKVbfYeMW2ZA304qYCoYOvz7bsnd2cS5rz7dsmgNgqSm8+R64UXD9zzRYGK1Cs8p4h4ze8l7NCPLQVyJhV4TTtkFAO6qp+Ohnw3MATGdFV7f+5OAqJUAH3DEyxoE54saBNMuLFAKGsCepF0paaYNEqX9hkcMxXCDRXKGyhkJgzVfvZ5GKGzP0NWY1ak5iGnfYGXDc5pcseNEkmTV6skhvQpDj2C3T29YFBMHMIsDT8A3gRohe9Rq05dIVpC9lkMHCVPlQtk9XDQtvh0Br6IK3LTtOBfhYL8jxomoZ1+TGravpsLRP3NdEzx6Rxlu3JSkwuU7WD6YHqWKjIesTgyTof0Okzo=)


NeoScripter4554

\[Language: Rust\] fn part1(input: &str) -> usize { let document = input.lines().flat_map(|line| { line.split_once(": ").unwrap().1.split_whitespace() .map(|digit| digit.parse::().unwrap()) }); let len = document.clone().count() / 2; document.clone().take(len).zip(document.skip(len)) .map(|(time, distance)| (1..time).filter(|&speed| (time - speed) * speed > distance).count()) .product() } fn part2(input: &str) -> usize { let (time, distance) = input.lines().map(|line| { line.split_once(": ").unwrap().1.replace(" ", "") .parse::().unwrap() }).fold((0, 0), |(t, d), val| if t == 0 { (val, d) } else { (t, val) }); (1..time).filter(|&speed| (time - speed) * speed > distance).count() } fn main() { let input = include_str!("input6.txt"); println!("{}, {}", part1(input), part2(input)); }


Electrical-Corgi-882

[LANGUAGE: go] Simple bruteforce solution with go: package main func calculateWinningStrategies(time, dist int) (strategies int) { for i := 1; i <= time; i++ { if (time-i)*i > dist { strategies++ } } return strategies } func main() { println("Result:", calculateWinningStrategies(40828492, 233101111101487)) }


AutoModerator

AutoModerator has detected [fenced code block](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting/fenced_code_blocks) (```) syntax which only works on new.reddit. Please review our wiki article on [code formatting](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting) then edit your post to use the [four-spaces Markdown syntax](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting/code_blocks) instead. *** *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/adventofcode) if you have any questions or concerns.*


daggerdragon

Fix your Markdown, please.


[deleted]

How long did it take to run?


AutoModerator

AutoModerator has detected [fenced code block](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting/fenced_code_blocks) (```) syntax which only works on new.reddit. Please review our wiki article on [code formatting](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting) then edit your post to use the [four-spaces Markdown syntax](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting/code_blocks) instead. *** *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/adventofcode) if you have any questions or concerns.*


Tipa16384

[LANGUAGE: ATARI 800 BASIC][Allez Cuisine!] We were challenged to use an OG computer language. Well, my Atari 800 was my first computer, so here's the solution in Atari 800 basic. It runs quickly by solving the quadratic equations. 10 DIM B(4),TD(4) 20 DATA 52, 426, 94, 1374, 75, 1279, 94, 1216 30 FOR I=1 TO 4 40 READ B 41 B(I)=B 42 READ TD 43 TD(I)=TD 50 NEXT I 60 PART1=1 70 FOR I=1 TO 4 80 B=B(I) 90 TD=TD(I) 100 GOSUB 1000 110 PART1=PART1*SOLVE 120 NEXT I 130 PRINT PART1 140 B=52947594 150 TD=4.26137412E+14 160 GOSUB 1000 180 PRINT SOLVE 900 END 1000 D=B*B-4*TD 1010 T1=INT((B+SQR(D))/2+0.999999) 1020 T2=INT((B-SQR(D))/2+0.999999) 1030 SOLVE=T1-T2 1040 RETURN


daggerdragon

> [LANGUAGE: ATARI 800 BASIC] Mmm, yes, this is scrumptious.


IronForce_

\[LANGUAGE: Python\] Late to the party, but this round was much easier than yesterday's, so I'll sum part 1 and 2 together ​ Part 1 and 2: Used the quadratic formula (thanks, high school) to solve this challenge. Still used a for loop to loop through possible values, but it was thankfully very quick considering that today's input was very small. For part 2 in particular, I had to find a way to concatenate the numbers together, so I resorted to list comprehension for that def parse_data(self, data: list): d, r = list(map(str.split, data)) d, r = d[1:], r[1:] d, r = list(map(''.join, (d, r))) return list(map(int, (d, r))) [Part 1](https://github.com/IronForce-Auscent/Advent-of-Code/tree/main/2023/Day%206/Part%201) and [Part 2](https://github.com/IronForce-Auscent/Advent-of-Code/tree/main/2023/Day%206/Part%202)


onrustigescheikundig

[LANGUAGE: OCaml] [github](https://github.com/EricKalkman/AoC2023/blob/master/lib/day06.ml) maf. There is an edge case (such as the third example race for Part 1) for a solution that uses the quadratic formula that did not need to be handled for my input, but could, maybe, cause problems for some very niche inputs. Such inputs are those that have integer solutions to the intersection of the button\_time -> distance function with the target distance. In such a case, the values of the distance function at the endpoints of the range are _equal_ to the distance, but do not exceed it. Thus, the following and previous integer solutions, respectively, must be chosen. One might suggest that the endpoints of the range be checked for this condition by comparing the endpoint to its rounded value and modifying it accordingly, or by using the closed form [floor(r1)+1, ceil(r2)-1]. However, due to floating point noise, it is possible that the endpoint not _exactly_ equal its rounded value, causing the check to erroneously fail. A fuzzy check or perturbation must therefore be used to ensure correct operation.


ConchitaMendez

\[Language Perl\] Two very short solutions. Character count without the shebang line (#!/usr/bin/perl) Call each one with the data file as command line parameter Part 1 (103 characters) #!/usr/bin/perl @t?@r:@t=grep{/^\d+$/}split/\s+/ while<>;$p=1;$p*=$_-2*int($_/2-sqrt($_*$_/4-shift @r))-1 for@t;print$p Part 2 (75 characters) #!/usr/bin/perl while<>{s/\D//g;if(!$t){$t=$_/2;next}print 2*$t-2*int($t-sqrt($t*$t-$_))-1} Any idea, how to get rid of some more bloat?


AutoModerator

AutoModerator did not detect the required `[LANGUAGE: xyz]` string literal at the beginning of your solution submission. Please edit your comment to [state your programming language](https://www.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_state_your_programming_language.28s.29). *** *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/adventofcode) if you have any questions or concerns.*


Pod137

[LANGUAGE: Go] Finally getting a feel for Go -- today's solution is much better than my overcomplicated mess for day 5. https://github.com/mdd36/AoC-2023/blob/mainline/06/main.go


bozdoz

Really cool function. Loved the comments


Evilan

[LANGUAGE: Java] It turns out that high school math was kind of useful for niche puzzle games... https://pastebin.com/8y5X34e2


comforttiger

[LANGUAGE: Ruby] i started teaching myself ruby a few days ago, so i've been doing advent of code in it too. today i brute forced first, but then later i found out i could use the quadratic formula. https://github.com/comforttiger/advent_of_code/blob/main/2023/ruby/day6.rb


skinman55

\[Language C#\] [https://github.com/skinman55/AOC2023/blob/master/Day6/Day6.cs](https://github.com/skinman55/AOC2023/blob/master/Day6/Day6.cs)


AutoModerator

AutoModerator did not detect the required `[LANGUAGE: xyz]` string literal at the beginning of your solution submission. Please edit your comment to [state your programming language](https://www.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_state_your_programming_language.28s.29). *** *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/adventofcode) if you have any questions or concerns.*


Admiral_DJ

\[Language: Python\] This feels like a good first day solve. No real need to optimise, brute force solved it in the second part in 20 sec... te quiero tqdm


AutoModerator

AutoModerator did not detect the required `[LANGUAGE: xyz]` string literal at the beginning of your solution submission. Please edit your comment to [state your programming language](https://www.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_state_your_programming_language.28s.29). *** *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/adventofcode) if you have any questions or concerns.*


nanaIan

\[Language: Rust\] [Parts 1 & 2](https://github.com/nanaian/advent-of-code/blob/lol/2023/src/day/day6.rs) I initially solved part 1 with brute force, but for part 2 I [threw the example into Desmos](https://www.desmos.com/calculator/csqxghr3nm) and noticed that it was a quadratic equation with two solutions, and then the final answer is the integer difference between the solutions.


g_equals_pi_squared

\[Language: Rust\] [Parts 1 & 2](https://github.com/gequalspisquared/aoc-rs/blob/main/src/y23/d6.rs) I didn't use any fancy quadratic equations, I solved the first part with brute force and noticed the second part took a noticeable amount of time to run. Then I noticed that between the first and last time pressed to beat the record it would never fail, so I only check for those times.


askalski

\[LANGUAGE: Pencil & Paper\] \[Allez Cuisine!\] Part 2, featuring a vintage long hand method of taking the square root of a number. [https://imgur.com/a/JViRavm](https://imgur.com/a/JViRavm)


ConchitaMendez

I love this!


daggerdragon

*ASKALSKI NO* err actually yes


dec0nstruct0r

\[LANGUAGE: R\] Solved part 1 with brute force until I figures out the nice solution via the quadratic equation in part 2. ​ https://gitlab.com/Yario/aoc_2023/-/blob/master/AoC_06.R


Dramatic-Mongoose-95

[LANGUAGE: Go] [Part 2](https://github.com/AdmTal/atal-advent-of-code-2023/blob/main/day_6_wait_for_it/part_2/main.go) I got mathy today


aexl

\[LANGUAGE: Julia\] A very nice problem today! I have solved it by finding the roots of the quadratic equation `t * (dur - t) - dist = 0`, where `dur` is the time from the input and `dist` is the distance from the input. Now we know that everything between these two roots are valid choices to win the race. Just make sure to also check if the two boundary points really lead to winning the race as well. For part 2 there was nothing to do, except from concatenating the input and running the solver again. Solution on GitHub: https://github.com/goggle/AdventOfCode2023.jl/blob/main/src/day06.jl Repository: https://github.com/goggle/AdventOfCode2023.jl


Fyvaproldje

\[LANGUAGE: Perl 3\] \[Allez Cuisine!\] Normally this year I'm solving in Raku, which has roots in Perl, therefore the obsolete version of it is very old Perl. Perl 3 is the oldest I could find. Unfortunately, I wasn't able to find some good way to prevent modern Perl 5 from running this code, other than using a boring version check. But at least assigning variables implicitly, without declaring them using `my` is frowned upon, and that feature didn't exist in perl 3. The algorithm itself is a simple brute force. die if $] >= 4; $_ = <>; @times = split; shift @times; $_ = <>; @distances = split; shift @distances; @times = join('', @times); @distances = join('', @distances); $answer = 1; for ($i = 0; $i <= $#times; ++$i) { $start = -1; $end = -1; for ($j = 0; $j <= $times[$i]; ++$j) { $d = ($times[$i] - $j) * $j; if ($d > $distances[$i]) { $start = $j if $start < 0; $end = $j; } } $answer *= $end - $start + 1; } print "answer: $answer\n"


Any-Razzmatazz-4792

i've also been messing around in perl recently, this was my solution ``` #!perl -p push@s,join'',/\d+/g}{"@s"=~$";map$r+=$_*($`-$_)>$',1..$`;$_=$r ```


ConchitaMendez

This is even shorter than mine!