r/adventofcode • u/daggerdragon • Dec 01 '23
SOLUTION MEGATHREAD -❄️- 2023 Day 1 Solutions -❄️-
It's that time of year again for tearing your hair out over your code holiday programming joy and aberrant sleep for an entire month helping Santa and his elves! If you participated in a previous year, welcome back, and if you're new this year, we hope you have fun and learn lots!
As always, we're following the same general format as previous years' megathreads, so make sure to read the full posting rules in our community wiki before you post!
RULES FOR POSTING IN SOLUTION MEGATHREADS
If you have any questions, please create your own post in /r/adventofcode with the Help/Question flair and ask!
Above all, remember, AoC is all about learning more about the wonderful world of programming while hopefully having fun!
NEW AND NOTEWORTHY THIS YEAR
New rule: top-level
Solutions Megathreadposts must begin with thecase-sensitivestring literal[LANGUAGE: xyz]- Obviously,
xyzis the programming language your solution employs - Use the full name of the language e.g.
JavaScriptnot justJS - Edit at 00:32: meh, case-sensitive is a bit much, removed that requirement.
- Obviously,
- A request from Eric: Please don't use AI to get on the global leaderboard
- We changed how the List of Streamers works. If you want to join, add yourself to 📺 AoC 2023 List of Streamers 📺
Unfortunately, due to a bug with sidebar widgets which still hasn't been fixed after 8+ months -_-, the calendar of solution megathreads has been removed from the sidebar on new.reddit only and replaced with static links to the calendar archives in our wiki.
- The calendar is still proudly displaying on old.reddit and will continue to be updated daily throughout the Advent!
COMMUNITY NEWS
- Veloxx will continue to drop some sick beats for 1.5 hours after today's unlock!
- /u/jeroenheijmans is back again this year with their Unofficial AoC 2023 Participant Survey!!
Advent of Code Community Fun 2023: ALLEZ CUISINE!
- I will be your chairdragon for this year's community fun event: ALLEZ CUISINE!
- Full details, rules, timeline, templates, etc. will be in the Submissions Megathread!
- (I may have been binging episodes of Iron Chef Japan lately why do you ask >_>)
AoC Community Fun 2023: ALLEZ CUISINE!
We unveil the first secret ingredient of Advent of Code 2023…
*whips off cloth covering and gestures grandly*
Upping the Ante!
You get two variables. Just two. Show us the depth of your l33t chef coder techniques!
ALLEZ CUISINE!
Request from the mods: When you include a dish entry alongside your solution, please label it with [Allez Cuisine!] so we can find it easily!
--- Day 1: Trebuchet?! ---
Post your code solution in this megathread.
Read the full posting rules in our community wiki before you post!
- State which language(s) your solution uses with
[LANGUAGE: xyz] - Format code blocks using the four-spaces Markdown syntax!
- State which language(s) your solution uses with
- Quick link to Topaz's
pasteif you need it for longer code blocks. What is Topaz'spastetool?
•
u/daggerdragon Dec 01 '23
Message from the mods: please be patient while we work out the kinks with AutoModerator. We tested each rule separately in a sandbox before deploying it in /r/adventofcode, but users will always find the obscure edge cases. :P
20
u/voidhawk42 Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Dyalog APL]
I make video walkthroughs of AoC solutions on my Youtube channel, will probably post one for this later today.
15
u/Smylers Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Vim keystrokes]
This isn't Vim's built-in scripting language, just keystrokes from editing a file. To follow along, load your input into a Vim window then just type the following to edit the input into the solution. (If you normally have
gdefaultenabled, first turn it off with:se nogd.) This gives you part 1:At that point the only thing left in the Vim buffer should be the required integer. You can copy it to the clipboard for pasting elsewhere with
"+yiw.)For part 2, reset to the start (press
ua bunch of times), then do it again but with these additional lines between the first two commands above:[Update: Don't actually do all this. See reply below with a much shorter way.]
How's it work? The first
:sduplicates the contents of each line, so that lines with only one digit on them will have it twice. (It also adds a#between the copies, because that's handy for part 2.) The longer:swith the\Ds in it finds the first and last digit in each line, and replaces the entire line with just those digits (captured in parens, then available in\1and\2in the replacement string), and sticks a+sign before them.That leaves the buffer with a big sum ‒ an expression to evaluate. The final line of keystrokes is as close to a design pattern as Vim keystroke solutions have: select the entire input (
v{, since the cursor was already on the last line), join it on to a single line (J), go to the start of the line (0) and replace its entire contents (C). Once in insert mode,⟨Ctrl+R⟩=prompts for an expression to evaluate and insert the contents of. At that prompt⟨Ctrl+R⟩-inserts the contents of the small†-delete register,"-, with is where theCstored the text it deleted. Press⟨Enter⟩to end the expression, and there's your answer. I expect that'll crop up quite often over the next 24 days.(The
+before the first number in the expression is unnecessary, but gets interpreted as unary plus rather than addition, so is harmless.)For part 2, the additional
:scommands turn the words into digits. There's some tricksiness:fiveightwe need to match bothfiveat the beginning andeightat the end, even though they share ane. Duplicating the entire string first, tofiveight#fiveightmakes it easy to find both of them.fivebefore eight, to make5ight. But for the final digit, we need to findeightbeforefive, to makefiv8; the entire string needs to become5ight#fiv8. So we need separate transformations for each end.#between the copies: one set of transformations for words to the left of the#, and another for those after.e(one,three,five, andnine) have to be matched beforeeight, which has to be matched beforetwo. Sooneightwobecomes1ightwo. Buttwoalso needs to be matched beforeone, so thattwoneightwobecomes2neightwo.twolast, but have the pattern foroneactually be/\v(tw)@<!one/, which only matchesonewhen the preceding characters aren'ttw, leaving it fortwoto match it later.twobeing/\vtwo(ne)@!/, sotwonegets left foroneto match later.It's good to be back and see you all again. Happy Advent, everybody.
† ‘Small’ because it doesn't have any line-breaks in it, even though in this case the line is actually very long.
5
u/DFreiberg Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Mathematica] Mathematica, 515 / 631
Made the same mistake everybody else did; in Mathematica, you have to explicitly specify
Overlaps -> All, or else it will parse an input likeeightwothreeand recognize theeightand thethree, but not thetwoin between.Part 1:
Part 2:
4
u/JustinHuPrime Dec 01 '23 edited Dec 01 '23
[LANGUAGE: x86_64 assembly with Linux syscalls][Allez Cuisine!]
I am once again doing this in assembly, and gosh, is it ever clear that the contest wasn't designed for assembly!
Part 1 was a very nice string comprehension thing that involved moving a few pointers around.
Part 2 was really nasty, and since I decided not to implement
strcmp-but-it-stops-at-newlines-for-the-second-argument, I had to do the comparisons directly. This was quite troublesome and I had a bug where I seemingly decided that if something wasn't greater than 5, than it couldn't possibly be greater than 4. I really should have just implemented the string comparison function.Both parts ran in under a millisecond; part 1 is 11056 bytes long, and part 2 is 11960 bytes long - the three hundred or so extra lines of assembly really weighted it down.
I am also going to claim this satisfies the restriction of "no more than two variables" since assembly doesn't have variables, so technically I used zero variables. Technically.
4
u/Medical_Ad_8018 Dec 01 '23 edited Dec 01 '23
[LANGUAGE: JavaScript]
i'm not sure you can even call mine a solution 💀💀💀 edit: turns out this was pretty common
4
u/Smylers Dec 01 '23 edited Dec 02 '23
[LANGUAGE: Perl]
Part 1 is solved with:
Update: Better version in a reply below. Apologies for not thinking of that in the first place.
That also works as a one-liner:
which can be golfed down to:
Or, with a different approach there's:
which golfs to:
For part 2, add this definition of the digits at the top:
and do this at the start of the
whileloop:4
u/keithstellyes Dec 01 '23
[LANGUAGE: Common LISP]
Just Part 1, I'm recovering from wisdom teeth surgery and wanted to learn LISP this year, too exhausted to try and make it work for Part 2. Feedback generously accepted
[LANGUAGE: Python]
Part 2, the language I am most comfortable with for quickly creating a solution
3
u/Lindii98 Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Python]
I hate the nr. 8 from now on
3
u/thousandsongs Dec 01 '23
[LANGUAGE: Haskell] - The [Allez Cuisine!] solution
My regular solution is here in my previous comment.
For today's Allez Cuisine, we need to write the program using only two variables. Here I'm not sure what all I should count as variables. Haskell doesn't actually have variables (really), it only has bindings. We can take a variable to mean a binding though, that's a pretty common way to look at things coming from other languages. However, this means that each argument to the function also counts as a binding, and I can't (yet) find a way to do it with only 2 bindings if I count both functions & their arguments towards the total.
However, if I disregard function parameters, then indeed, it can be done with 2 variables, both of which are the functions
parseandfirst.In the above code snippet, there are only two custom functions -
parseandfirstand no other variables as such (see my note above, I'm cheating by discounting function arguments). So that is my allez cuisine submission.I was also able to code golf it to exactly 280 characters, so now it fits a tweet!
Fun!
I still need to figure out an elegant way to avoid repeating
reverseinf (reverse s) reverse, using some sort of dup combinator. Will keep thinking.Runnable Haskell files for all these versions are here.
3
u/[deleted] Dec 01 '23 edited Dec 01 '23
[LANGUAGE: C#]
Today I learned about the right-to-left processing option of the Regex class, neat! Solution: paste.
[Allez Cuisine!]
One variable to store a look up table. What remains is an abhorrence that should never have been brought into existence.
2
u/ImpossibleSav Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Python] & [Allez Cuisine!]I am back again, working on solving as many Advent of Code problems as I can in a single line of Python code just like last year! Feel free to follow along on my GitHub repo. Here's my one-line solution for both parts of Day 1 (GitHub link):
I'm not sure if it counts for Allez Cuisine, but the only variable I used outside of the print statement is
q[1], which contains the contents of the input file.Also, is this considered oversized? 🤔 Please let me know!
2
u/caseyweb Dec 01 '23
[LANGUAGE: Nim]
Every year I use AoC as a chance to learn a new language and this year it is Nim!
2
u/prescient-potato Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Python 3.11.5]
pretty easy but managed to mess it up somehow. for p2 i tried solutions using .replace() and .find() but both had their problems. had to settle with the naive solution. good q for day 1.
2
u/sreyas_sreelal Dec 01 '23 edited Dec 01 '23
[Language: Rust]
Day 1 solution
2
u/[deleted] Dec 01 '23 edited Dec 02 '23
[LANGUAGE: Forth]
Specifically: Dusk OS Forth
https://git.sr.ht/~aw/dusk-aoc/tree/main/item/2023/01.fs
Video explanation: https://www.youtube.com/watch?v=EWvNm4A6FG4
2
u/p88h Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Mojo]
I will try implementing each day in Mojo and run performance comparisons between Python (3.11), PyPy (3.9) and latest build of 🔥
(Since Mojo is positioning itself as performant Python replacement)
Today, PyPy wins. Strings are not one of Mojo's strength, I think.
https://github.com/p88h/aoc2023/blob/main/day01.mojo
vs
https://github.com/p88h/aoc2023/blob/main/day01.py
Part1: Python 0.8 ms, PyPy 0.2 ms Mojo 0.3 ms / 0.06 ms (parallelized x 16 cores)
Part2: Python 8.7 ms, PyPy 0.8 ms Mojo 4.3 ms / 0.9 ms (parallelized x 16 cores)
on a 1000x larger data set, parallelized Mojo is slightly faster than PyPy, but still within 10%, but PyPy is not parallelized, so ...
UPDATE:
I added the string reversal trick, with quite unexpected results -> see
https://github.com/p88h/aoc2023/blob/main/BENCHMARKS.md
For benchmark values, but it does seem that with this implementation Mojo comes out quite a bit faster.
2
u/flwyd Dec 01 '23 edited Dec 01 '23
[LANGUAGE: sed] (on GitHub)
[ALLEZ CUISINE!] Only two variables you say? Look ma, no variables!
day1part1.sed:day1part2.sed:Oh, I've got variables left over? I suppose I'll use one to loop over the input files.
day1sed.sh:2
u/BoggartShenanigans Dec 02 '23
[Language: Shakespeare Programming Language 1.2.1]
Bit late to the party, but... https://github.com/PanoramixDeDruide/AoC-2023 , repo also contains patched sources for the SPL->C transpiler to work with modern compilers (tested on whatever gcc build Debian Testing uses), plus amd64 binaries for the library because I forgot to remove those.
Managed to finish last year's AoC's first week all in this absolute beauty of a language, trying the same this year.
67
u/CCC_037 Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Rockstar], part 1
49
u/pred Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Python], GitHub
So yeah,
I wonder if anyone else did something like that ...
28
u/[deleted] Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Google Sheets]
Assuming the input is in A:A
Part 1
Part 2
Part 2 (Alternative solution that doesn't require explicitly writing the numbers)
18
u/DrunkHacker Dec 01 '23
[LANGUAGE: Python]
13
u/jonathan_paulson Dec 01 '23
[LANGUAGE: Python 3] 29/3. Solution. Video. Excited for 2023!
25
u/4HbQ Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Python]
Managed to get it down to punchcard size:
Edit: This was my original version using regex (7 lines). This is a different idea that I now prefer. The regex and translation dict are built from the same string:
9
u/Radiadorineitor Dec 01 '23
[LANGUAGE: Dyalog APL]
9
u/Cloudan29 Dec 01 '23
[LANGUAGE: J]
I um... I'm sorry for this.
Admittedly this is inspired by other solutions on here cause I couldn't be bothered to figure out how Regex works in J. This works just as well.
9
u/Pyr0Byt3 Dec 01 '23 edited Dec 03 '23
[LANGUAGE: Go] [LANGUAGE: Golang]
https://github.com/mnml/aoc/blob/main/2023/01/1.go
Hardest day 1 yet imo, the possibility of overlaps in part 2 made it a bit of a nightmare. I initially tried to use strings.NewReplacer to replace the words with digits all in one go, and it did work for the sample input! Just not for the real input... classic.
8
u/VeritableHero Dec 01 '23
[LANGUAGE: T-SQL]
Not sure if I'm the only one silly enough to attempt this in Microsoft SQL Server but here goes.
14
u/nthistle Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Python 3], 16/23. original, messy code, solve video (once YT finishes processing it)
Pretty happy with my first day this year although I definitely lost a few ranks for silly reasons (when I initially solved, I forgot to copy my answer so had to alt tab back to get it). I think a lot of it was nerves, since this year my company is sponsoring and I got a fancy "Sponsor" tag, so I have to represent us well :-)
7
u/morgoth1145 Dec 01 '23
[LANGUAGE: Python 3]
Cleaned up code
Oof, this was quite a bit more involved than I anticipated for day 1. Admittedly my performance also suffered due to some dumb oversights on my part (I thought I was clever using the parse library to extract all numbers and entirely forgot that it would get two digit numbers too, then I botched handling that too!) But this also seems more like something I'd expect a few days in, hopefully I was just approaching this wrong and the rest of the days won't be this much harder than typical!
7
u/trevdak2 Dec 01 '23 edited Dec 01 '23
[Language: Javascript Golf]
Part 1, 94 chars:
Part 2, 187 characters
7
u/Sharparam Dec 01 '23
[LANGUAGE: Ruby]
Using the lookahead trick to get overlapping matches with regex.
(GitHub link)
7
u/0rac1e Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Raku]
6
u/duckyluuk Dec 01 '23 edited Dec 02 '23
[Language: Python Golf]
This year I'll be trying to solve every day in as little python code as possible.
Input Parsing:
Part 1 was pretty straightforward:
Part 2 was a bit of a struggle to find out, but once I found a solution it was quite simple to get it to a (rather long) oneliner:
Stats:
Input Parsing: 11 bytes
Part 1: 72 bytes | 0.013 seconds
Part 2: 182 bytes | 0.061 seconds
Total: 265 bytes
6
u/ArturSkowronski Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Kotlin]
O Lord, have we sinned so grievously in the past year with ChatGPT that we merit such punishment on the first day?
https://github.com/ArturSkowronski/advent-of-code-2023/blob/main/src/Day01.kt
PS: It was so much fun for the Day 1 😃
PS2: Commit message says everything about my morning
6
u/dznqbit Dec 01 '23 edited Dec 01 '23
[Language: Python 3]
Github Link
But I would like to refer you to this beauty of a line:
6
u/Hungry_Mix_4263 Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Haskell]
https://github.com/alexjercan/aoc-2023/blob/master/src/Day01.hs
6
u/Markavian Dec 01 '23
[Language: JavaScript]
https://github.com/johnbeech/advent-of-code-2023/blob/main/solutions/day1/solution.js
Gosh that second part was painful. I had to search for "the trick" because I at first assumed that I'd need to search backwards through the string at the same time as searching forward through the string... but to have an overlap... that was not telegraphed in the example.
6
u/6745408 Dec 01 '23
[LANGUAGE: GOOGLE SHEETS]6
u/Civil_Blackberry_225 Dec 02 '23
[LANGUAGE: C#]
6
u/TiltSword Dec 04 '23
[LANGUAGE: Java]
Part 1-
Part 2-
Part 2 was, admittedly, a mess for me but it works x)
5
u/_jstanley Dec 01 '23 edited Dec 01 '23
[LANGUAGE: SLANG] 2186/1475
https://github.com/jes/aoc2023/tree/master/day1
https://www.youtube.com/watch?v=__MAbMwpGF8
I'm doing Advent of Code on my homemade 16-bit CPU again. Funnily enough I have been working on a regex library this week, although it isn't ready enough to use. Would have come in super handy on part 2!
I wasted some time on part 1 because I hadn't clocked that the result was going to be wider than 15 bits. I modified my program to use bigints, but in hindsight I could have made it print out the result as an unsigned integer, or even mentally corrected the negative value, and got the right answer sooner.
My part 2 took 8m32s to run, it wastes a lot of time because the
strlen()call (although only called on short fixed-length strings) almost doubles the runtime becausestrlen()andstrncmp()do basically exactly the same job, exceptstrlen()compares against 0 andstrncmp()compares against its argument.5
u/mosredna101 Dec 01 '23
[LANGUAGE: Nodejs]
Hardest first day ever :D
I feel there must be a smarter way then what I did here.
5
u/Sandbucketman Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Python]part 1:Part 2:
5
u/mendelmunkis Dec 01 '23
[LANGUAGE: C]
String parsing? I don't need no string parsing
1.537/1.956 ms
5
u/TheBlackOne_SE Dec 01 '23
[LANGUAGE: Python]
Part 1: https://github.com/TheBlackOne/Advent-of-Code/blob/master/2023/Day1_1.py
Part 2: https://github.com/TheBlackOne/Advent-of-Code/blob/master/2023/Day1_2.py
Regex ftw. Overlapping in my puzzle input really threw me off.
5
u/Naturage Dec 01 '23
[Language: R]
Solution here. And so it begins - honestly, at higher speed than I expected. Part 2 threw a sizable wrench in the works - less because it was difficult and more because R does not like PERL by default and you need to fiddle a bit.
Here's to 24 more!
5
u/9_11_did_bush Dec 01 '23
[LANGUAGE: Bash]
Wasn't going to post because this is kinda a silly solution, but since I haven't seen anyone else post a bash solution: https://github.com/chenson2018/advent-of-code/blob/main/2023/01/shell/01.sh
5
u/marcus_cemes Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Rust]
Simple string comparison (LUT) and digit parsing over a rolling window
&line[i..].No regex, no heap allocation (to my knowledge), only std library. Solutions benchmarked at 33.1 µs and 42.1 µs on an Intel i7-11800H @ 2.30 GHz. Repo.
5
u/im_sofi Dec 01 '23
[LANGUAGE: Elixir]
This was a lot harder than I was expecting for a first day.
https://github.com/soupglasses/advent-of-code/blob/main/2023/day_01.exs
5
u/Heizor Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Python3] [Allez Cuisine!]
Why waste time write lot line when few line do trick? And why bother with variables? Readability is also completely overrated. I spent way to much time writing this abomination and now you have to suffer looking at it as well. The goal was 1 line per task and 0 variables. Part 2 might be a bit prettier by using the 'one'->'o1e' trick that many other solutions use.
Part1: filter out all non-digits -> get first and last value -> concatenate -> cast to int -> sum
Part2: find all digits and spelled digits -> get first and last value -> convert spelled digits to digits -> concatenate -> cast to int -> sum
5
u/Witchpls Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Java]
Struggled a bit with part 2 as my initial solution didn't take the strings "oneight", "twone" and "eightwo" into account. I wish that was a bit clearer in the example but luckily I found someone here in the thread that had noticed it! :)
Github Day 1
5
u/IlliterateJedi Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Python]
Python solution - This runs in about 2ms on my machine. I used a Trie and cut off the substring search as soon as I hit a dead end each time. This felt like a day 10 problem to be honest. I was surprised to get hit by this first thing in the morning today.
5
u/Zweedeend Dec 04 '23 edited Dec 04 '23
Spam
[Language: Python]
[Allez Cuisine]
Haha, I had no idea python could still make sense of all the variables. I'm surprised it even worked! This is part 2, with lots of spam:
Here is the full recipe
9
u/dehan-jl Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Rust]
Github - 39loc with some boilerplate
No special tricks here, other than using
filter_mapto basically do all the error handling for me.4
u/ivanjermakov Dec 01 '23 edited Dec 01 '23
[LANGUAGE: TypeScript] 2780 / 162
github
If only I didn't forget how to convert a string to a number is JS... again.
4
u/hrunt Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Python]
Code
This was the first time I ever had a need to use raw f-strings. The examples clearly showed overlapping matches, so a zero-width lookahead assertion allows for the use of
re.findall(), which only finds non-overlapping matches.4
u/princeandin Dec 01 '23
[LANGUAGE: Swift]
Day 1
AOC author came out swinging with that part 2 difficulty jump.
4
u/Shot_Conflict4589 Dec 01 '23 edited Dec 01 '23
[LANGUAGE: swift]
Code
It ain't stupid if it works.
For part 2 I was to lazy to use regexes or smth like that. So I just hardcoded the digit strings, reversed the string, and checked if the reversed string starts with the reversed digit string. Quite ugly and slow, but gets the job done.
4
u/AlexTelon Dec 01 '23 edited Dec 01 '23
[LANGUAGE: python], 16 lines or a more compact 10 lines
(All solutions are for both parts)
10 lines python In this solution I used
def to_digit(x): return (nums.index(x) % 9) + 1to convert to digits which allowed me to append the nums for part 2 nicely.8 lines python Here I feel I'm taking it too far. But hey I learned that you can use the walrus operator
:=in a function call.Another thing I did in these recent solutions is to use
_, l = min( ((row+x).find(x), x) for x in nums)instead ofl = min(...)[1]which is something I quite like doing.Im working on getting a minimal solution that is still readable.
4
u/s3aker Dec 01 '23 edited Dec 01 '23
[LANGUAGE: raku]
4
u/Comfortable_Key_7654 Dec 01 '23 edited Dec 01 '23
[LANGUAGE: rust]
I would also try the same in different languages. You can check my solutions here.
4
u/Tipa16384 Dec 01 '23
[LANGUAGE: Haskell]
I only started learning the language today, so... be kind.
4
u/hrabrica Dec 01 '23
[LANGUAGE: Kotlin]
Didn't even think of replacing and just went down the search path which turned out to be a good idea looking at all the issues people had with overlapping numbers.
Part 1: https://pastebin.com/Avk118QN
Part 2: https://pastebin.com/G7AMCH2h
4
u/azzal07 Dec 01 '23
[LANGUAGE: Awk]
I use a regex
"one|two|...|eight|nine"to locate the spelled out digits. The (one based) index of the digit in the regex then maps to the number with the expressionint(1.2+index/5).To find the last digit, I just reverse the line, the regex, and the decoded digit (
10 - d).4
u/allak Dec 01 '23
[LANGUAGE: Perl]
NoPaste snippet
A simple solution in Perl. Greatly simplified after I learned about the greedy operator in regex ...
4
u/Annual-Management613 Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Ruby]
The first part was easy but the second one was really frustrating until I realised that the regex needs the lookup
?4
u/Diderikdm Dec 01 '23
[LANGUAGE: Python]:
4
u/miloszowi Dec 01 '23
[LANGUAGE: bash]
https://github.com/miloszowi/aoc2023/tree/main/01_bash
Well, hardest (but still easy) day 1 ever in advent of code, strings like '2oneight' was the hard part to find - you should treat that string as 28, not 21.
4
u/bigmagnus Dec 01 '23
[Language: Fortran]
Part 1
Part 2
4
u/Fyver42 Dec 01 '23
[LANGUAGE: RISC-V assembly]
https://github.com/fyvonnet/AdventOfCode-2023-Assembly/blob/main/day01-part1.asm
https://github.com/fyvonnet/AdventOfCode-2023-Assembly/blob/main/day01-part2.asm
4
u/fozzzyyy Dec 01 '23
[LANGUAGE: Google sheets]
Column A: Data
Column B: =REGEXEXTRACT(A1, "\D*(\d)" )
Column C: =REGEXEXTRACT(A1, ".*(\d).*$")
Coulmn D: =C1+10*B1
Column E: =SUM(D:D)
Column G: =REGEXREPLACE(REGEXREPLACE (REGEXREPLACE (REGEXREPLACE (REGEXREPLACE (REGEXREPLACE (REGEXREPLACE(REGEXREPLACE (REGEXREPLACE (A1, "one", "o1e"), "two", "t2o"), "three", "t3e"), "four", "f4r"),"five","f5e"),"six","s6x"),"seven","s7n"),"eight","e8t"),"nine","n9e")
Column H: =REGEXEXTRACT(G1, "\D*(\d)" )
Column I: =REGEXEXTRACT(G1, ".*(\d).*$")
Column J: =I1+10*H1
Column K: =SUM(J:J)
4
u/simpleauthority Dec 01 '23
[LANGUAGE: Google Sheets]
(Hope it's ok to post twice for different solutions, if not please forgive me dear AoC overlords.)
Link to Sheet
4
u/__Abigail__ Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Perl]
I initially solved the first part by grepping all the digits out of each line, and using the first and last one. But that doesn't work for the second part, or at least not easily, due to overlap:
eightwoandseveninefor example. So, I just used a two regexes: one for the first match and one for the last match.First some initialization, where I map each word to its value:
We can loop over the input, find the first and last matches of each line, and sum them, using ten times the value of the first match:
4
u/fent665 Dec 01 '23
[LANGUAGE: Rust]
I'm using this year's AOC as a way to learn Rust, day 1 proved to be a little more difficult than I was anticipating.
https://github.com/fent665/aoc_rust_2023/blob/master/sols/day1/main.rs
4
u/mckgn Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Python]
4
u/unclefritz Dec 01 '23
[Language: q/kdb+]
tried a few random things for part 2 but this mapping is pretty elegant
https://github.com/sl0thentr0py/aoc/blob/main/aoc2023/1/foo.q#L8-L12
4
u/huepfler Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Clojure]
4
u/megagon_extreem Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Python]
I did my best to golf both parts.
Assumes the variable t is the input, so it does cheat a bit.Edited to useopen(0)magic that the people in the Python discord also use.Part 1 (95 chars):
Part 2 (251 chars):
TIL that you can use
(?=(regex thing))to makere.findallinclude overlaps.4
u/Vzwolf Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Python]
Part 2:
4
u/SegganWasTaken Dec 01 '23
[LANGUAGE: Metis]
Simply remove all the
replacefor part 1 code.4
u/CoralKashri Dec 01 '23 edited Dec 02 '23
[LANGUAGE: C++](GitHub)
~209 micro for the second part- the most efficient way I could think of.
The second part really caused me use modern C++ features, which is interesting condsidering it's only the first day this year LOL
4
u/xelf Dec 01 '23
[LANGUAGE: python]
With pandas! 🐼🐼
remove the first replace to get part1, keep it for part2
4
u/Aromatic-Piccolo4321 Dec 01 '23
[Language: Rust] 🦀 Explored P1 & P2 challenges in Rust: My Solutions 🦀
Would love to get your insights and feedback!
4
u/litezzzOut Dec 01 '23
[LANGUAGE: asm]
Masm 64bit
https://github.com/rahulsenna/Advent-Of-Code/blob/master/done/2023/Day01/main.asm
4
u/meat-eating-orchid Dec 01 '23 edited Dec 02 '23
[LANGUAGE: shell]
Part 1:
Part 2:
4
u/Muted_Preparation_85 Dec 02 '23
[LANGUAGE: Python]
I just reverse the string and reverse the dict and search.
4
u/0rac1e Dec 02 '23
[LANGUAGE: BQN]
Just a translation of a refactor of my J solution
4
u/robertkingnz Dec 02 '23
[LANGUAGE: Rust]
Advent of Code Rust Solution 🚀 | 5-Min Video
4
u/aspargas2 Dec 02 '23
[LANGUAGE: Jazelle bytecode]
https://github.com/aspargas2/advent-of-code-2023/tree/main/day01-jazelle
Solved in handwritten Java bytecode executed natively on a 3DS using Jazelle. See the readme at the link above for details.
4
u/RelativeLead5 Dec 02 '23 edited Dec 02 '23
[LANGUAGE: ruby]
PART 1
PART 2
4
u/jotac13 Dec 02 '23
[LANGUAGE: rust]
As always, criticism and suggestions are welcome to improve as an engineer and rustacean:
https://github.com/joao-conde/advents-of-code/blob/master/2023/src/bin/day01.rs
4
u/Potential-Series-105 Dec 02 '23 edited Dec 02 '23
[Language: Ruby]
143 bytes solution
4
u/nicuveo Dec 05 '23
[LANGUAGE: Brainfuck]
too long to fit in this text box! the logic is small, more than half of the code is just to print an int32 at the end. :D
https://github.com/nicuveo/advent-of-code/blob/main/2023/brainfuck/01-A.bf
4
u/lsloan0000 Dec 06 '23
[Language: Python]
I'm joining late. I just learned about Advent of Code yesterday!
My solution…
It's not as short as some others, but I don't feel right about using the
'zero'→'z0o', etc. translation dictionaries that I see others using. I guess because it works in English, but it might not in other languages. I mean, what if the names of digits could share more than just one beginning and ending characters?4
u/Expensive_Register19 Dec 06 '23
[PYTHON]
Got stuck with eightwo situation too. What helped was to treat the edge-cases as two digits and it solved the problem of whether they are first or last.
4
u/vengamer Dec 06 '23
[LANGUAGE: C++]
Part 1
Part 2
I probably could have shortened my code for part 2 but I have finals coming up lol.
3
u/goldenlion5648 Dec 01 '23
[LANGUAGE: Python] 945/ 497
Github
Excited for this year!
3
u/Boojum Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Python 3] [Allez Cuisine!]
(Comment out spelled out digits in the dict for Part One solution)
Got tripped up working out how overlapping spelled out digits were supposed to work.
Edit: Added [Allez Cuisine!] tag, since now that I've read the description I see that my post unintentionally qualified for today. Python feels a bit like cheating, though.
3
u/xoronth Dec 01 '23
[LANGUAGE: Python 3]
I fell asleep and woke up like 30 minutes after it started :/
Super lazy python solution for now (paste) while I polish off the rusty gears in my head and get back into the groove.
3
u/UseUnlucky3830 Dec 01 '23
[LANGUAGE: Julia]
Attempted part 2 three times before getting it right. I don't code well while I'm on trebuchets :D
link
3
u/astro_wonk Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Python]
Ugh, my Part 1 solution involved
.isdigitwhich was easy enough, but then I tried.indexfor Part 2 which worked for the example but I didn't consider two instances of the digit as word likeonetwo2oneso that burned me. Ultimately rewrote using regex, and what's linked there is a reconstruction of Part 1 using the updated code that was left after I rushed through Part 2.Definitely harder than any previous Day 1 I have tried!
EDITED to say: Oh no, I didn't account for
zerobut somehow that didn't cause me any problems? Did I get super lucky or iszeronever in any inputs?EDITED AGAIN: Wait, no, the problem statement explicitly mentions every word but
zerothough I don't know if I actually internalized that at the time...3
u/MarvelousShade Dec 01 '23
[LANGUAGE: C#]
It was a good starter, but I made a lot of typo's (especially in the numbers one to nine).
Because of some keyboard settings I tried to compare with the numbers öne and ëight...
https://github.com/messcheg/advent-of-code/tree/main/AdventOfCode2023/Day01
3
u/Recombinatrix Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Python]
Using list comprehension and a quick and dirty find and replace to deal with overlapping words.
EDIT: I suppose since we only care about first and last it doesn't matter if there are overlapping words, but I have a little hunch this will come up again.
3
u/synack Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Ada]
Part 1
Part 2
3
u/Cloudan29 Dec 01 '23
[LANGUAGE: Python 3]
Code
Cleaned it up obviously, but if you can just imagine that with a bunch of code repetition, that's what I had when I submitted.
I didn't pay attention to the examples and didn't realize Python's built-in regex package "re" doesn't do overlapping matches. So I just found one that does do overlapping matches lol.
3
u/Abomm Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Python]
some basic regex goes a long way
paste edited the bug I introduced when I thought I was cleaning up my code
Unfortunately I locked myself out on part 1 because of a small bug. It cost me my first leaderboard spot. Part 2 was a mess since I forgot my regex, used some capture groups when they weren't needed and did a poor job reading the question, not realizing there was a mix of digits and words.
I'd say this is the hardest day 1 ever. I don't think the top 100 leaderboard has taken this long to fill up since advent started (ignoring the year with server issues).
3
u/recursive Dec 01 '23
[LANGUAGE: typescript]
So I built yet another web UI framework, and an online sandbox for it. In addition to showing the answer, it shows its work. You can edit the code and run it online.
https://mutraction.dev/link/c3
I'll be posting the rest of the days here. https://mutraction.dev/cases/aoc2023
3
u/PrudentWish Dec 01 '23
[Language: Swift]
GitHub
Part 2 was pretty tricky for me
3
u/ondrsh Dec 01 '23
[Language: Kotlin]
3
u/ValiantCookie Dec 01 '23
[LANGUAGE: Kotlin]
solution
I was really thrown a bit in part 2 since I had just expected Kotlins Regex.findAll would find overlapping matches (AoC really brings the joy of learning new language quirks). Not to mention the example conveniently left out any overlapping matches like "threeight".. this was a difficult day 1. I ended up using a reversed regex on a the reversed string to find the last digit in part 2.
3
u/__Juris__ Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Scala]
3
u/vikingly56 Dec 01 '23
[LANGUAGE: Ruby]
3
u/raevnos Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Common Lisp]
Paste
Edit: CL's
formathas an~Rdirective that (Among other uses) spells out an integer in English;(format nil "~R" 1) => "one"for example. That came in handy; who wants to type out a bunch of cardinal number strings?3
u/szamuro Dec 01 '23 edited Dec 01 '23
[LANGUAGE: javascript]
3
u/osalbahr Dec 01 '23 edited Dec 02 '23
[LANGUAGE: C++]
Part 1; Part 2
Feel free to ask any questions!
You can find more C++ solutions (and other languages) at Bogdanp/awesome-advent-of-code#c++
3
u/quodponb Dec 01 '23 edited Dec 02 '23
[LANGUAGE: python3]
Python3
Full solution
A surprisingly rough start! I woke up too late for a good placement, so decided to look for a pleasant solution, and had a trickier time with part 2. At first I tried "replace" for each of the nine words, but that ran into problems since it would hide other possibly earlier words. For instance "eightwo" would turn into "eigh2", and I'd not find the "8".
I was pleased with two things: I made two maps from token to digit, and combined them for part 2:
I also liked how I extracted the tokens from each line:
You could do a lot better than this, performance wise, by searching backward for the second digit instead of finding them all. But I like reading this, so that's what I'll stick with.
3
u/CutOnBumInBandHere9 Dec 01 '23
[LANGUAGE: Python]
Part 1:
Part 2 here: I used a global find/replace; to account for the potentially overlapping strings I padded the replacement strings. Not the cleanest solution, but it works
3
u/j_ault Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Swift]
Got stuck on the overlapping words bit.
3
u/SpaceHonk Dec 01 '23
[Language: Swift] (repo)
no regex, plain string searching. Part 2 still took longer than expected :-)
3
u/AppanKarKeVekhaya Dec 01 '23
[Language: Python]
code (topaz's paste)
It was a challenging first day, but enjoyed it overall. When my first attempt at part 2 was wrong, I thought it might be due to overlap characters and tried to solve it by that approach. After several failed attempts I rewrote my code assuming overlaps are allowed and voila, it worked! Stupid bugs in my first attempt :p
On the bright side, at least now I have 3 different ways of mapping letters to digits.
3
u/sim642 Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Scala]
On GitHub.
Part 2 was surprisingly annoying. Initially I didn't even think about overlaps and thought just string replacements would reduce it to part 1, but of course not. For a clean solution, I ended up writing the transformation using
.tailsand.startsWithat each position for each digit string.Also, I haven't used Scala since the previous AoC, so my skills are probably deteriorating, but I cannot be bothered to do it in any other language since I've built up such a helper library.
3
u/Leniad213 Dec 01 '23 edited Dec 01 '23
[LANGUAGE: JavaScript]
def not ideal, gets the job done.
had to check reddit to see that part 2 had overlapping words, pretty tricky.
first time participating tho :)
https://github.com/LeinadAsid/adventofcode/tree/main/2023/day1
3
u/OhGodImCooooooooding Dec 01 '23
[LANGUAGE: rust]
https://github.com/ZizoAdam/AdventOfCode2023/tree/main/Day%20One/day_one_part_two
I'm pretty happy with my part two solution. I'm a rust beginner who came in with no intentions of getting on the leaderboard (given day 1 part 1 was supposedly cleared in 12s I think that was the right call) but moreso experimenting with the language and trying different patterns.
This really reminds me of programming back in my first couple years of university where a lot of the tasks were nice succint tasks not grand projects to lose my life to.
3
u/Perohmtoir Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Excel]
Input is put in column A starting from A1. Formulas are put in B1/C1 and dragged down. Besides converting the various fix data array using F9 and changing my input location to A1 to make reading easier, this is what I used to find both solutions.
First part:
Second part, definitely trickier. Had to do some trial and error so this function is not clean and probably does some useless steps.
3
u/radulfr2 Dec 01 '23
[LANGUAGE: Python3]
I wrote several versions (one of which was regex) before finally getting one that worked for both parts. Final version.
3
u/Fyvaproldje Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Raku]
https://github.com/DarthGandalf/advent-of-code/blob/master/2023/Day01.rakumod
Update: [Allez Cuisine!]
By variables you mean self-modifying (self-varying) code, right? right?!
Here's the solution which basically self-modifies itself to the previous solution by replacing VAR1 and VAR2 in its own source code to avoid enumerating all the one/two/three/etc several times.
https://github.com/DarthGandalf/advent-of-code/blob/master/2023/Day01x.rakumod
3
u/Exact_Apple_9826 Dec 01 '23
[LANGUAGE: PHP]
be careful of edge case in part 2 "eightwo" becomes "82", simple string replace will not work
https://github.com/mikedodd/AdventOfCode2023/tree/main/day_1
3
u/WilkoTom Dec 01 '23
[LANGUAGE: Rust]
https://github.com/wilkotom/Aoc2023/blob/main/day01/src/main.rs
Aha, I thought. Day One will be a "read a bunch of numbers and perform some calculation on them, it always is, I can do that in my sleep then go back to bed."
Nope. A bit more nuance than a lot of previous day ones. That'll learn me.
3
u/TetrisGG Dec 01 '23
[Language: Python3]
Topaz Code
P1 was quick, P2 had me stumped at first, until the realization kicked in that it needed overlaps. Was fun and can't wait for the next days :D
3
u/sanraith Dec 01 '23
[LANGUAGE: Scala]
Full code is available on my github: Day01.scala
Solved both parts with Regex, used a map to convert single or text digits in part 2.
3
u/leftfish123 Dec 01 '23
[Language: Python]
Holy moly, last time I coded was in February, when I got the final star for AoC 2022. This time I got off to a rough start, trying to figure out why I cannot get the overlaps right. I decided not to use regex, mostly because I'm not familiar enough with them and you're not supposed to have to learn new things on day 1, right? Right? Turns out I'm going to try to rewrite it with regex just for fun. This is what I love in AoC!
Anyway, here's my attempt. Iterating with two pointers for part 2.
3
u/theSpider_x Dec 01 '23
[Language: C]
For a first day this was a bit more challenging than I expected honestly.
For the second part I lazily just reversed the string and searched for the reversed names of the digit names
code
3
u/0rac1e Dec 01 '23 edited Dec 02 '23
[Language: J]
I realised that the function I wrote for Part 2 can be used for Part 1... so, have a refactor
3
u/Hoinkas Dec 01 '23 edited Dec 04 '23
[LANGUAGE: Python]
Used re library and I am very happy with outcome (fewer lines of code than expected), mostly thanks to that solution with nice
re.findall()function which allowed to exclude every custom string and any digit from input.Any tips or advice are welcome!
https://github.com/Hoinkas/AdventOfCode2023/blob/main/FirstDay/FirstDayPuzzle_Hoinkas.py
Some unit tests to use for first day puzzle
3
u/neelrr1 Dec 01 '23 edited Dec 01 '23
[LANGUAGE: OCaml]
[code]
Wanted to use this year's AOC to learn Ocaml but wow this first one was a doozy. I'm sure there's a way to do this better but coming up with this solution broke me.
Any advice for a functional programming n00b would be appreciated!
3
u/AllanTaylor314 Dec 01 '23
[LANGUAGE: Python] 227/381
Code: HEAD (26545f3)
Part 1: Pretty easy - extract the digits, concat the first and last, cast to int and sum (should have used filter, but I knew how to do the list comp)
Part 2: On the first go, replace "one" with "1" etc., then realise that "oneight" is a thing. Start replacing "one" with "one1one" and it works. Advent of horrible hacks, here I come.
3
u/razetime Dec 01 '23
[LANGUAGE: Maude]
Full code
Very difficult. Starting from file I/O to manipulating strings, to working with lists, Maude is very specific, and although it is quite reasonable for programming purposes, the rewriting nature of it i makes the job harder than it should be.
3
u/Zach_Attakk Dec 01 '23
[LANGUAGE: Lua]
Oh if it wasn't for that edge case where a string name can overlap, I would've had this in a few minutes. Luckily when I checked reddit I found the hint immediately. One small change in the code and it worked. I feel like one of the example lines should've included one of those "double words" as the last digit and we would've caught it immediately.
Part 1
Part 2
3
u/ChartRaiser Dec 01 '23
[LANGUAGE: C]
Part 2 on Github
My approach is very different from most of the answers here.
3
u/[deleted] Dec 01 '23 edited Mar 18 '25
Still no one knows it just the same, That Rumpelstiltskin is my name.
3
u/Jorge_Fakher Dec 01 '23
[LANGUAGE: Elixir]
Tricky start with part 2.
3
u/Dullstar Dec 01 '23
[LANGUAGE: D]
https://github.com/Dullstar/Advent_Of_Code/blob/main/D/source/year2023/day01.d
Decided not to use Python this year. I usually end up putting so many type annotations anyway, may as well just use something statically typed to begin, plus every once in a while the compiler optimizations make a noticeable difference. From my previous experiences with it, D seemed like a nice in-between from Python and C++, while retaining much more C-like syntax compared to, for example, Rust.
Plus D can easily call cstdlib functions, which I can't say I was expecting to be useful, but strncmp works just fine for a substring comparison. Probably could have made something work with slices now that I think about it, but I thought of that first.
I was able to make a faster solution for part 2, but it's rather large and unwieldy (it uses knowledge of the possible cases to ultimately do fewer comparisons, although of course we have to explicitly specify way more of them instead of having nearly all of the comparisons being performed nicely hidden away in a library function whose internals we never need to think about). For that reason, I didn't include it in the main solution, but if you REALLY want to see that monstrosity, it's here. It would probably be possible to make it even faster by having a separate, similar function find the second digit, but I definitely don't feel like writing that at this time.
3
u/fiddle_n Dec 01 '23
[LANGUAGE: Python]
Uses third-party regex module with the overlapped kwarg.
Full solution: https://github.com/Fiddle-N/advent-of-code/blob/master/year_2023/day_01/process.py
3
u/arthurno1 Dec 01 '23 edited Dec 01 '23
[Language: EmacsLisp]
https://github.com/amno1/AOC2023/
3
u/Artraxes Dec 01 '23
[LANGUAGE: Kotlin]
3
u/solareon Dec 01 '23 edited Dec 01 '23
[LANGUAGE: excel]
Excel
Well we busted out the lambdas early this year. Also not covering all the edge cases in the sample data is a nice anti-gpt move.
It's a very wide solution with lots of helper columns that could be optimized but that's for another day.
3
u/semicolonator Dec 01 '23
[LANGUAGE: Python]
Python, 20 Lines
https://github.com/r0f1/adventofcode2023/blob/main/day01/main.py
4
u/enelen Dec 01 '23
[Language: R / Rlang]
Solution
The most fun and frustrating part of the year is back!
3
u/[deleted] Dec 01 '23
[LANGUAGE: shell]
3
u/blacai Dec 01 '23
[LANGUAGE: FSharp]
Glad to be doing it another year with F#. I think this is the "hardest" first puzzle I remember. I had to debug the part 2 a couple of times :|
Part 1 with regexp to get the numbers
Part 2 finding first and last occurrence of digit
https://github.com/blfuentes/AdventOfCode_Main/tree/2023/AdventOfCode_2023/day01
3
u/rooneyyyy Dec 01 '23
[Language: Shell]
Part-1
Part-2
3
u/Anna-cz Dec 01 '23
[LANGUAGE: Python]
Hey,
I am not a programmer. I usually program just once a year during December the advent of code. So my code is usually very straight forward and naive. Sometimes when I get stuck and I search for inspiration here I get lost because of all the libraries and more advanced techniques that people use and I have no idea what does it do (and I would have to spend days figuring it out).
I decided to do lots of comments to my code and maybe it will help some other non-programmers like me :)
Part 1
Part 2
3
u/rumkuhgel Dec 01 '23
[LANGUAGE: Golang]
https://github.com/rumkugel13/AdventOfCode2023/blob/main/day01.go
3
u/0x2c8 Dec 01 '23 edited Dec 01 '23
[LANGUAGE: C]
https://github.com/alexandru-dinu/advent-of-code/blob/main/2023/01/solve.c
Substring search. Keep a single array of needles:
1-9&one-nineand perform two searches: leftmost (for first valuea) and rightmost (for second valueb). The result is10 * a + b.For part 1 we look only for
1-9, whereas for part 2 we also look forone-nine.3
u/fennecdore Dec 01 '23 edited Dec 01 '23
[LANGUAGE: PowerShell]
3
u/padjal Dec 01 '23
[LANGUAGE: C#]
My solution is based around the reverse search regex expression option, which was the only way I managed to find my way around the "sevenine (79)" type of cases.
new Regex("(one)|(two)|(three)|(four)|(five)|(six)|(seven)|(eight)|(nine)|[0-9]", RegexOptions.RightToLeft)3
u/s3mj Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Python]
This is my first ever python script, so if there are improvements I can make, please feel free to share!
Part 1:
Part 2:
3
u/Sosowski Dec 01 '23
[LANGUAGE x86 Assembly]
Here's a solution to the first part of day 1 in 192 bytes
3
u/flammschild Dec 01 '23 edited Dec 01 '23
[LANGUAGE: PowerShell]
3
u/greycat70 Dec 01 '23
[LANGUAGE: tcl]
Part 1, Part 2. I also uploaded Part 2 but broken for grins.
Part 1 is simple. Part 2 is shockingly difficult for a day 1 puzzle, especially given the ambiguity in the problem statement.
My initial try for part 2 was to replace "one" with 1, and so on. This did not work. My second try used two passes, the first to replace "oneight" with "oneeight" and so on, and then the second pass to replace "one" with 1, etc. This happened to give the right answer, but it's not right -- I just got lucky. This solution doesn't handle arbitrarily long chains of number-words correctly, e.g. "twoneight" should become "twooneeight", but 1b-broken only gives "twooneight".
So in the interest of having an actually correct solution for day 1, I scrapped that and went with the forward-and-backward approach. Replacing "one" with 1 (etc.) gives the first digit correctly, but to get the last digit, we need to reverse the string, and then replace "eno" with 1, "owt" with 2, and so on.
3
u/Seanie987 Dec 01 '23
[LANGUAGE: python]
Part 1:
Part 2:
3
u/Brief-Presentation-4 Dec 01 '23 edited Dec 03 '23
[LANGUAGE: c#] c# solution
3
u/bnberg Dec 01 '23
[LANGUAGE: Bash]
3
u/mc_ Dec 01 '23
[Language: erlang]
https://github.com/2600hz/advent-of-code/blob/james-2023/src/day01.erl
3
u/FrontPale Dec 01 '23
[LANGUAGE: Julia / Julialang]
3
u/Krryl Dec 01 '23
[LANGUAGE: Python]
3
u/willpower_11 Dec 01 '23
[LANGUAGE: Bash]
I didn't bother to fetch the input programmatically yet so I just downloaded it and saved it as
in.txt. For Part 2, thanks for the kind stranger who pointed outoneightshould be parsed as18.Part 1:
Part 2:
I feel this is a pretty good abuse of
sedandawk.3
u/intersecting_cubes Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Rust]
Final runtime was 2 milliseconds. Some performance notes:
Code:
3
u/WonderfulGanache5991 Dec 01 '23
[Language: Python]
-Part 1-
-Part 2-
3
u/blueboss452 Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Python]
Using dictionary, "startswith" method, removing one char at a time.
3
u/AbsolutelyNoAmbition Dec 01 '23 edited Dec 01 '23
[LANGUAGE: Java]
3
u/blazemas Dec 01 '23
[LANGUAGE: C++]
On my machine a 7ms run solution. I was tortured like many with part 2 for a bit.
https://github.com/jbush7401/AoCCPP/blob/main/AoCCPP/2023/Day1.h
https://github.com/jbush7401/AoCCPP/blob/main/AoCCPP/2023/Day1.cpp
3
u/Aeonian9 Dec 01 '23 edited Dec 06 '23
[LANGUAGE: Julia]
Github Part 1, Github Part 2
My attempt at Part 2 felt very hacky.
3
u/laBalance Dec 01 '23
[LANGUAGE: Kotlin][Allez cuisine!]If I was willing to run my regexes 2x per line, I could eliminate all variables except for the
stringsToIntsmap and could technically put the entire solution of part 1 on a single line and part 2 on 2 lines, but at that point I'm sacrificing performance for flair and throwing readability in the garbage.3
u/stridera Dec 01 '23
[LANGUAGE: go]
First time writing in Go. I'm curious what I could improve on to make it more go-ish
3
u/jpjacobs_ Dec 01 '23 edited Dec 01 '23
[LANGUAGE: J]
Ah nice feeling to get going with AoC again! Got stuck well on the underdocumented cornercase in part 2 though.
3
u/mmonstter Dec 01 '23 edited Dec 02 '23
[LANGUAGE: kotlin]
3
u/quebst Dec 01 '23
[LANGUAGE: Python]
Replacing the second char in the number with the digit allows overlapping words to still be found.