r/adventofcode • u/daggerdragon • Dec 02 '23
SOLUTION MEGATHREAD -❄️- 2023 Day 2 Solutions -❄️-
OUTSTANDING MODERATOR CHALLENGES
THE USUAL REMINDERS
- All of our rules, FAQs, resources, etc. are in our community wiki.
Community fun event 2023: ALLEZ CUISINE!
- 4 DAYS remaining until unlock!
AoC Community Fun 2023: ALLEZ CUISINE!
Today's theme ingredient is… *whips off cloth covering and gestures grandly*
Pantry Raid!
Some perpetually-hungry programmers have a tendency to name their programming languages, software, and other tools after food. As a prospective Iron Coder, you must demonstrate your skills at pleasing programmers' palates by elevating to gourmet heights this seemingly disparate mishmash of simple ingredients that I found in the back of the pantry!
Solve today's puzzles using a food-related programming language or tool
- How about an appetizer of Chef or Wireshark(-fin soup)?
- Mascots count, too… *side-eyes Rust while reaching for the Old Bay*
- Add some salt to your hashbrowns, cookies, and breadcrumbs
- Serve us up a nice big bowl of spaghetti code slathered with
RagúRaku tomato sauce - Conclude our tasting with a digestif of Java, CoffeeScript, or even Perl milk tea with double syntatic sugar
- All file names, function names, variable names, etc. must be named after "c" food
- Go hog wild!
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 2: Cube Conundrum ---
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
13
u/770grappenmaker Dec 02 '23
[LANGUAGE: Kotlin] Placed #76/#50, my second leaderboard ever (first time was yesterday).
11
u/simonlydell Dec 02 '23 edited Dec 04 '23
[LANGUAGE: Fish] and [LANGUAGE: JavaScript]
For part 1, I grepped away the impossible games:
For part 2, I turned the input into JavaScript and eval:ed it (which is my theme for the year):
Generated JavaScript code for the example of part 2
7
u/Markavian Dec 02 '23
[Language: JavaScript]
https://github.com/johnbeech/advent-of-code-2023/blob/main/solutions/day2/solution.js
Managed to bash out part one while my baby crawled around trying to slap my laptop screen. Part two had to wait until the evening... but was a pleasant enough solution - I was momentarily misled thinking I'd need the minimum value(s) from each bag; but the maximum values I'd already computed did the job.
5
u/OhGodImCooooooooding Dec 02 '23
[LANGUAGE: rust]
https://github.com/ZizoAdam/AdventOfCode2023/tree/main/Day%20Two/day_two/src
Another overengineered solution, but that's half the fun. I'm not here to be fast I'm here to be memory safe with an expressive type system (crab emoji).
3
u/zach1502 Dec 02 '23
[Language: Python]
First Leaderboard placement ever. Yolo'ing paid off.
5/6
pt1
pt2
2
u/Boojum Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Python3] [Allez Cuisine!] 719/672.
Fairly straightforward, the obvious way is mostly just a bit of nested string splitting. That's how I did it at first.
On the other hand, since I abused regexps yesterday, the fun way to do part 2 is use it to find all of the "(d+) red" substrings and take the max of the numbers, etc. Look ma, a single statement answer:
Edit: Renamed identifiers to c-food to be [Allez Cuisine!] compliant. :-) For some reason, I am now thinking of "One Fish, Two Fish, Red Fish, Blue Flush."
2
u/Comfortable_Key_7654 Dec 02 '23 edited Dec 02 '23
[Language: Rust] PasteBin
Figuring out the appropriate structures to store the data and writing their
FromStrimplementations took some time. Otherwise, the problem was a breeze. Loved solving it.UPD: Solved it in GoLang as well!
[Language: Go, Part 1] GitHub
[Language: Go, Part 2] GitHub
2
u/nlowe_ Dec 02 '23
[LANGUAGE: Go] 699 / 633 GitHub
Much better today, although My laptop kept dropping to 500MHz for some reason :(
Sure, I'll blame missing the leaderboard on that :) Definitely my laptop's fault
2
u/ramrunner0xff Dec 02 '23
[Language: C]
some kind of C food crab free
2
u/stribor14 Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Airline Food] [Allez Cuisine!]
Link to interpreter: https://github.com/jamie-large/Airline-Food/blob/main/Airline-food.pyDefault interpreter prints ascii characters, so I changed line 203 to output raw number from stack.
The following solution solves Part 2 of Day 2 problem. It finds max count of colored dice for each game, so in theory, it could be "easily" converted to Part 1. Input is given as following (number by number): ID0 num0 type0 num1 type1 ... 0 ID1 num0 type0 num1 type1 ... 0 ID2 num0 type0 num1 type1 ... 0 ... 0
Number 0 is to detect the end of the line (numx == 0) and also the end of the input (if ID == 0)
Just to be consistent, I edited by hand input strings to the required input with search/replace in Bluefish editor. Without further ado, enjoy my solution written as a one-sided talk between two stewardes aboard a flight. (and a more readable one)
https://github.com/stribor14/AoC23-2
2
u/_rabbitfarm_ Dec 02 '23
[Language: Perl]
I think my regex skills were strengthened for Day 2 after the unexpected challenge of Day 1. I also liked that Part 2 ended up being few lines of code than Part 1.
Part 1: https://adamcrussell.livejournal.com/46907.html
Part 2: https://adamcrussell.livejournal.com/47280.html
2
u/themanushiya Dec 04 '23 edited Dec 04 '23
[LANGUAGE: Go] using structs, pretty straightforward after you choose an easy model/datastructure
solution here
24
u/[deleted] Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Google Sheets]
Assuming the input is in A:A
One formula for both parts
22
u/YenyaKas Dec 02 '23
[LANGUAGE: Perl]
This is pretty straightforward when one realizes it is not necessary to parse the whole input line. Using
List::Util::maxI got this code for part 2:All my solutions in Perl: https://www.fi.muni.cz/~kas/git/aoc.git/
17
u/Radiadorineitor Dec 02 '23
[LANGUAGE: Dyalog APL]
34
u/4HbQ Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Python] Code (7 lines)
Today's trick: using a defaultdict to keep track of the maximum counts:
Edit: Replaced
defaultdict(int)with an explicit dictionary, thanks /u/mocny-chlapik and /u/blueg3.Additionally, here is a golfed version using a different technique (120 bytes):
16
u/Werqu90 Dec 02 '23
[Language: Python]
Part 1&2
Today was a great occasion to use one of my favourite Python classes:
Counter!After parsing is just a one liner for both parts!
14
u/Smylers Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Vim keystrokes]
For part 1, load your input, type this, and the solution will appear:
:g/…/ddeletes all lines that contain any 3-digit number of cubes, or any 2-digit number starting with2-9.:g/…/is likegrep†: it selects all lines matching its pattern and invokes the following command on each of them. The following command here is:d, which deletes the specified line.:g/…/ddoes the same thing but for quantities in their teens, a different limit for each colour. By this point we've removed all the impossible games.:%s///removes everything else, including the\nline-break character at the end of each line. It also puts a+sign before each number, so you end up with a single line. For the sample input it's+ 1 + 2 + 5.Cis the design pattern I mentioned yesterday to evaluate the expression, adding up the numbers.And that's all there is to it. It worked first time, and I reckon I typed it as a one-off transformation faster than I'd have written a program to do the same thing.
I haven't got to part 2 yet, though. I'll update if I work it out.Update: Part 2 required an entirely different approach
† In fact,
:gis so likegrepthat it's actually what thegingrepstands for, the command coming originally from Ed, which was extended into Ex, then visualized as Vi, and improved as Vim. Therestands for ‘regular expression’ and thepfor:p, the print command (as in, display on the screen, not send to a printer). In all those editors,:g/RE/pwill display the lines matching the specified regular expression, hence Ken Thompson choosing the namegrepfor the standalone command doing the same thing, in 1973.(Update: Realized the /g flag wasn't actually doing anything in the final version, so removed both that and the then-unnecessary message about the gdefault setting.)
13
u/jonathan_paulson Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Python 3] 25/18. Solution. Video.
I don't think the problem was very clear about what to do in part 2 if some color wasn't present (by the wording of the problem, I think the power should be 0, but my accepted solution doesn't handle that correctly). I guess it just doesn't come up?
12
u/lboshuizen Dec 02 '23 edited Dec 11 '23
[LANGUAGE: F#]
github
11
u/chubbc Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Uiua]
God I'm loving this language. Its simultaneously the ugliest and most beautiful code in the world lol. I only did Part 2 in it (might come back later and put in Part 1 as well...)
Edit: link to the Uiua pad if you want to play around with it
11
u/Cloudan29 Dec 02 '23 edited Dec 02 '23
[LANGUAGE: J]
This language is absolutely cursed. I first create a 3xn matrix containing all of the maximums in order red, green, blue. Then, well, the rest is kind of trivial funny enough. Check compared to bounds, min to get 0s, I. to get indexes of 1s, increment one, sum. And part two is just multiply reduce then add reduce.
Edit: Changed the order of a few things to get rid of ugliness and got rid of some parens I didn't realize I didn't need
10
u/BoggartShenanigans Dec 02 '23
[LANGUAGE: Shakespeare Programming Language 1.2.1]
Look at all you people with your fancy string splitters! Let THE BARD teach you how it's done:-)
https://github.com/PanoramixDeDruide/AoC-2023/tree/main/02 for today's work, the repo https://github.com/PanoramixDeDruide/AoC-2023/ has an updated version of the SPL2C transpiler that works on modern systems.
9
u/nomisjp Dec 02 '23 edited Dec 02 '23
[LANGUAGE: excel]
Excel solution
Part 2 is just muliplication of the 3 maximums and summate.
Columns used: 35.
9
u/Ethoxyethaan Dec 02 '23 edited Dec 03 '23
[LANGUAGE: Javascript]
codegolfing in web console solution 223 bytes for both part 1 & 2.
8
u/JustinHuPrime Dec 02 '23
[LANGUAGE: x86_64 assembly with Linux syscalls]
So, er, apparently Topaz decided to make part 2 really similar to part 1 today to compensate for yesterday.
Part 1 was a bit of a pain working through the file parsing - I ended up having an off by one error where I forgot to skip the trailing newline, and this lead to segfaults (about the only way other than producing an answer that I get any feedback about the correctness of my code). I realized that I'd need to maintain a few values across loop iterations, but as it turns out, that's not all that bad if I decide to keep some values on the stack.
Part 2 was a very trivial modification on top of part 1 - in fact, part 2 might have been easier than part 1.
Part 1 runs in 1 millisecond, and is 11200 bytes long.
Part 2 runs in 2 milliseconds, and is 11160 bytes long.
8
u/chubbc Dec 02 '23
[LANGUAGE: Julia]
Isn't the most elegant... but it gets the job done.
8
u/s3aker Dec 02 '23
[LANGUAGE: raku]
day 2 solution
8
u/catzzilla Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Perl]
Part 1:
Part 2:
8
u/amalloy Dec 02 '23
[LANGUAGE: Haskell]
Code on GitHub and solution video on YouTube.
I considered a 3-Int type like I see in many of the other solutions here, but I think the
Map Color Intalternative worked out better. No custom combining code,unionWithalready does everything you need.7
u/dhruvasagar Dec 02 '23
[LANGUAGE: Zig]
7
u/CrazyMerlyn Dec 02 '23
[LANGUAGE: OCaml]
6
u/MarcusTL12 Dec 02 '23
[LANGUAGE: LEG64 Assembly] Both parts on Github
This is assembly code for the processor I made in the game 'Turing Complete'.
6
u/arthurno1 Dec 02 '23 edited Dec 02 '23
[LANGUAGE: EmacsLisp]
6
u/Smylers Dec 02 '23
[LANGUAGE: Vim keystrokes]
This is for part 2 — as ever, just load your input, type this, then watch as the numbers dance up the screen:
Sorry I don't have time right now to explain it, but if you just type it in and watch what's happening, it should be clear enough. It relies on each game requiring at least 1 of each colour of cube, which was true for my input.
The
:redr|sl20mis entirely gratuitous: it:redraws the window then:sleeps for 20 ms, just so we can see the state after each game has been processed, providing an animation of the loop.6
u/litezzzOut Dec 02 '23
[LANGUAGE: asm]
Masm 64bit
Both Parts
11
u/pred Dec 02 '23
[LANGUAGE: Python] GitHub
One thing that helped here was just completely ignoring that the problem description suggests a semantic difference between ";" and ",", when it doesn't make a difference for either part.
2
u/lightalpha Dec 02 '23
[LANGUAGE: Python]
First run was with regex. This second one is without imports code. 16 lines and readable I would say. Probably too much split.
5
u/leijurv Dec 02 '23
[LANGUAGE: Python3]
29th place, 19th place
Screen recording: https://youtu.be/pHlLc0Lp8WI
5
u/DFreiberg Dec 02 '23
[LANGUAGE: Mathematica] Mathematica, 2679 / 1837
I went slowly and methodically through the problem, not rushing, making sure I got each component right...and then lost six minutes on part 1 because I typed
11instead of12for theredcheck. I tested the sample inputs, cleaned up the code, added a check just in case one game had no cubes of a certain color drawn, the whole works, before realizing my mistake.But at least writing part 1 correctly meant that part 2 was a piece of cake.
Part 1:
Part 2:
5
u/Hungry_Mix_4263 Dec 02 '23
[LANGUAGE Haskell]
https://github.com/alexjercan/aoc-2023/blob/master/src/Day02.hs
5
u/CCC_037 Dec 02 '23 edited Dec 02 '23
[Language: Rockstar]
You can read the full code for part 1 here
5
u/jitwit Dec 02 '23 edited Dec 02 '23
[Language: J]
github
5
u/frabjous156 Dec 02 '23 edited Dec 02 '23
[Language: C#]
Code
EDIT: Better formatting of the code. Any criticism is much appreciated :-)
5
u/TheBali Dec 02 '23
[Language: Rust]
Literally my 2nd day ever writing Rust, roasting appreciated. Yesterday folks told me about clippy and the auto-formatter, so hopefully it's better now.
Yesterday I explicitly avoided the match syntax, this time I gave it a shot. I'm not sure if imbricating match calls is the way to do it, but it works so ¯_(ツ)_/¯ ?
linky to code
5
u/clbrri Dec 02 '23
[Language: C/C++]
Part B in 19 lines on Commodore 64: https://imgur.com/a/Hz5MWF6
5
u/bbrrck Dec 02 '23
[Language: Python]
Solution using pandas.
5
u/nicuveo Dec 13 '23
[LANGUAGE: Brainfuck]
a mere 300 lines for part 1, i haven't tried part 2 :)
VOD: https://www.twitch.tv/videos/2003360103
code: https://github.com/nicuveo/advent-of-code/blob/main/2023/brainfuck/02-A.bf
4
u/Pyr0Byt3 Dec 02 '23 edited Dec 03 '23
[LANGUAGE: Go] [LANGUAGE: Golang]
https://github.com/mnml/aoc/blob/main/2023/02/1.go
Much easier than day 1. I finally have a "real" max function in the standard library through
slices.Max()! Still a bit janky due to having to convert the values to a slice, but a lot better than the float64 conversion dance thatmath.Max()required before.4
u/0rac1e Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Raku]
4
u/Leniad213 Dec 02 '23
[LANGUAGE: JavaScript]
Github
No gotchas compared to day 1. was pretty fun.
Might switch to TS tho, spent more time fighting the language than the problem itself lol.
4
u/xelf Dec 02 '23
[LANGUAGE: Python]
with pandas
learning 🐼🐼 as I go...
4
u/Dullstar Dec 02 '23
[LANGUAGE: D]
https://github.com/Dullstar/Advent_Of_Code/blob/main/D/source/year2023/day02.d
Had to spend some time trying to figure out how to make fold cooperate with my Game struct since the examples just used arrays of numbers, but I eventually figured out I could just explicitly tell it what types to use and then explicitly feed in 0 as the initial value.
I was quite pleased with the speed just because it's a lot better than last year, though probably that's just a combination of faster computer + faster language.
4
u/kaa-the-wise Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Python] one-liner
Love it when the second part is shorter than the first one!
https://github.com/kaathewise/aoc2023/blob/main/2.py
EDIT: with improvements from /u/4HbQ
4
u/Mammoth_Spray_3451 Dec 02 '23
[LANGUAGE: Swift]
GitHub
Today i decided to go for some structs and enums. The solution itself was very easy, the parsing was the more annoying work.
4
u/aurele Dec 02 '23
[LANGUAGE: Rust]
https://gist.github.com/samueltardieu/2a9a051af44b0867062b446182aa71a0
4
u/cetttbycettt Dec 02 '23 edited Dec 02 '23
[LANGUAGE: R]
Easy solution using look ahead regex and the fact that for both parts only the maximum of red, green, blue per game is needed.
github
4
u/greycat70 Dec 02 '23 edited Dec 02 '23
[LANGUAGE: tcl]
Part 1, part 2.
A much-needed return to normalcy after day 1's fiasco, eh? When reading the part 1 example, I knew eventually the separation between groups (semicolons) would matter, but for part 1, it did not. For part 1, I simply extracted each "number color" items using a regex.
For part 2, of course that doesn't work, so I did the obvious "split by semicolons, then split by commas" thing. Both parts were quite simple once the parsing was done, as one would expect from such an early puzzle.
Edit: Actually, there's no reason the regex approach won't work in part 2. Looking at it again, there is no need to know whether "4 blue" and "3 green" are in the same draw, or two different draws. So my part 2 could be simplified, but I'll leave it as is.
4
u/hughcornwallis Dec 02 '23 edited Dec 02 '23
[LANGUAGE: PYTHON] one-liners
4
u/ultranarcolepsy Dec 02 '23
[LANGUAGE: Dyalog APL]
Simplified my code a bit after realizing I didn't really need to split on
;and then,; I can just consider3 blue, 4 red; 1 red, 2 green, 6 blue; 2 greenas a flat list of 6 things.4
u/Gravitar64 Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Python]
Part 1&2, 15 sloc, runtime 1ms
List comprehension on steroids
4
u/allak Dec 02 '23
[LANGUAGE: Perl]
Now both part1 and part 2:
4
u/Per48edjes Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Haskell]
Day 2: Part 1, Part 2
TL;DR: Seeing FP's merits come to the fore; getting a hang of this mental model (at the risk of jinxing myself).
Much, much smoother today. AoC feels like a perfect way to gain comfort with parsing in Haskell...as every puzzle needs to be parsed! Once everything compiled (after fiddling with types), it did indeed "just work". Slowly, but surely, the learnings from CIS 194 earlier this year are paying dividends.
Looking forward to seeing other (read: likely better!) Haskell solutions.
I am (pre-emptively) dreading the future dynamic programming problems because I still don't have the mental model for translating the functional expressiveness down to what's going on under the hood. Could switch to Python, but I'd still like to push the boundaries (edge of my abilities, dare I say?!) re: my Haskell-fu. Plus, I'm sure there'll be some Day far before DP is required that will put me in my place. :sweat_smile:
4
u/atgreen Dec 02 '23
[LANGUAGE: Common Lisp]
I made heavy use of parseq today: https://github.com/atgreen/advent-of-code-2023/blob/main/02.lisp
4
u/RickM1 Dec 02 '23
[LANGUAGE: Excel]
https://github.com/RickeshMistry95/AdventOfCode_2023_Excel
4
u/Nzxtime Dec 02 '23
[LANGUAGE: Java]
GitHub
4
u/Pagie7 Dec 02 '23
[LANGUAGE: R]
GitHub
Not the most concise but I love me some pivoting
4
u/AlexTelon Dec 02 '23 edited Dec 02 '23
[LANGUAGE: python]
python+GPT4 for parsing
O(n) time and cost complexity on my end.
It costs ~0.5$ to run.
Key section:
4
u/bubinha Dec 02 '23
[Language: Scala]
4
u/keithstellyes Dec 02 '23
[LANGUAGE: Common LISP]
Feeling a bit better today, was able to do Common LISP for both parts
p1.lispp2.lispshared.lisp4
u/Cerberus02052003 Dec 02 '23
[Language: C#]
[paste](https://pastebin.com/sckjvrjQ)
4
u/aleks31414 Dec 02 '23 edited Dec 03 '23
[LANGUAGE: rust]
Puzzle 1
Puzzle 2
4
u/horsecontainer Dec 02 '23
[Language: Python]
I had this in a separate post but then deleted and came here instead when I saw the megathread. Sorry for anyone who saw it twice.
4
u/robertkingnz Dec 02 '23 edited Dec 03 '23
[LANGUAGE: Rust]
Advent of Code Rust Solution 🚀 | 5-Min Video
⚠️ ⚠️ ⚠️ The following code is OVER-ENGINEERED ⚠️ ⚠️ ⚠️
it uses the new LET-ELSE statements, Results, Enums etc.
Code Snipppet
4
u/0xd34db347 Dec 03 '23
[Language: Rust]
I had a quick and dirty solution but as I'm trying to get familiar with rust I thought I'd give it a bit of effort. Not sure how idiomatic it is but its functional.
4
u/thousandsongs Dec 03 '23
[Language: awk] [Language: bash] [Allez Cuisine!]
What do unix aficianados use to slice and dice in the kitchen? Awk and sed indeed!
When doing my regular solution in Haskell, I realized that the semicolons don't matter - and each game can be reduced down to 3 (r,g,b) values. Armed with that simplification, the solution in awk becomes a breeze:
4
u/manhuntos Dec 19 '23
[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 / 136.37ms / 133.52ms
1
u/zjxy Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Rust] Full code
3
u/MarcusTL12 Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Julia], no leaderboard, but got 860/537 with this code. Now on to implement in LEG64 assembly.
Edits: It keeps trying to put the final end outside the code block...
3
u/RinkAttendant6 Dec 02 '23 edited Dec 02 '23
[LANGUAGE: JavaScript]
4221/1680594/1141 (edit: was looking at the numbers for current completed when originally posted)Made a lot of assumptions here, such as Game ID = line number, that each subset of cubes in a game did not contain duplicate colour cubes, and that there would be no extraneous characters other than whitespaces. No regex used as I did not deem it necessary for the job. String.prototype.endsWith sure came in handy today!
3
u/Sharparam Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Ruby]
(GitHub link)
Took longer than day 1 due to not grasping all the text at first.
3
u/alexbaguette1 Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Python]
sol1:
sol2:
3
u/xRyann_ Dec 02 '23
[Language: Python]
3
u/SpiceThisUp Dec 02 '23
[LANGUAGE: Swift]
https://github.com/FieryFlames/AdventOfCode/blob/2023/Sources/Day02.swift
I went heavy on splitting, but it got the job done :p
3
u/voidhawk42 Dec 02 '23 edited Dec 02 '23
[LANGUAGE: ngn/k]
Video walkthrough. No regexes, just splitting on spaces.
3
u/rizzu26 Dec 02 '23
[LANGUAGE: Swift]
Fairly easy both parts - just took some time to actually parse the input into the way I want.
Github Day 2
3
u/a3th3rus Dec 02 '23
[LANGUAGE: Elixir]
3
u/jeis93 Dec 02 '23
[LANGUAGE: TypeScript]
Fairly happy with my solutions, although I find it a bit messy to look at. Let me know what y'all think! Happy hacking!
TypeScript x Bun Solutions for Day 2 (Parts 1 & 2)
3
u/sreyas_sreelal Dec 02 '23
[Language: Rust]
Day 2 Solution
3
u/synack Dec 02 '23
[LANGUAGE: Ada]
Part 1
Part 2
3
u/ProjectEdenGG Dec 02 '23
[LANGUAGE: TypeScript]
GitHub
Pretty straightforward, learned about the logical or assignment operator ||= with this one (but ended up not using it)
3
u/ImpossibleSav Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Python]
Both parts of Day 2 solved in a single line of Python (
q[2]has the input file):I'm trying to get as many days as I can in only a single line! You can follow along on my GitHub if you'd like :)
3
u/JamesProclaims Dec 02 '23
[LANGUAGE: Typescript]
Super simple approach in the end, stoked with this. Good fun!
Day 2 Part 2 Solution
3
u/radulfr2 Dec 02 '23
[LANGUAGE: Python3]
Just some nested loops. I liked this task. Paste.
3
u/Lispwizard Dec 02 '23
[LANGUAGE: EmacsLisp]
3
u/JWinslow23 Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Python]
paste
This was thankfully easier than day 1. 😅
EDIT: Changed
functools.reduceandoperator.multomath.prod. Somehow I forgot that existed.3
u/POGtastic Dec 02 '23 edited Dec 02 '23
[LANGUAGE: F#]
F#'s
Maptools are absolutely wretched, and I had to roll a bunch of stuff that I take for granted in Python / Haskell. Boo, Microsoft. Boo.3
u/chkas Dec 02 '23 edited Dec 01 '24
[Language: Easylang]
Solution
3
u/silxikys Dec 02 '23
[LANGUAGE: Haskell]
Solution
Standard parsing exercise
3
u/raevnos Dec 02 '23
[LANGUAGE: Common Lisp]
Paste
First time using regular expressions in CL.
3
u/indienick Dec 02 '23
[LANGUAGE: Zig]
Github
3
u/ManaTee1103 Dec 02 '23 edited Dec 02 '23
[Language: Python]
3
u/NPException Dec 02 '23
[LANGUAGE: Clojure]
Solution
This was a nice and straightforward one.
3
u/Cyclotheme Dec 02 '23
[Language: QuickBASIC]
Github link
3
u/34rthw0rm Dec 02 '23
[LANGUAGE: tcl]
part 1
part 2
3
u/pdxbuckets Dec 02 '23
[LANGUAGE: Kotlin]
Wow, huge dropoff. Guess I'll post my code. Fairly standard. Only took the maximums of each color for each game. Made part 2 particularly trivial:
fun part2(): Int = games.sumOf { it.values.reduce(Int::times) }3
u/sim642 Dec 02 '23
[LANGUAGE: Scala]
On GitHub.
Everything was easily done by defining a case class for the (red, green, blue) triple with a couple of operations.
3
u/HAEC_EST_SPARTA Dec 02 '23
[LANGUAGE: Ruby]
Solution on sourcehut
Nothing particularly groundbreaking in today's solution, but I'm pretty pleased with its conciseness: being able to write the parser and solutions to both parts in single expressions is always quite satisfying.
3
u/theSpider_x Dec 02 '23
[Language: C]
This was easier than day 1 imo
https://github.com/fuzesmarcell/aoc/blob/main/2023/day_02.c
3
u/pontus53 Dec 02 '23
[Language: Rust]
Used
nomfor parsing.https://gist.github.com/ponbac/abde10b07a1a96886dd8b1e188eb4374
3
u/Different-Ease-6583 Dec 02 '23
[LANGUAGE: Python]
3
u/[deleted] Dec 02 '23
[LANGUAGE: Python]
Could refactor to use
getattrto be less repetitive, but it's late and just wanted the answer 😅3
u/p88h Dec 02 '23 edited Dec 19 '23
[LANGUAGE: Mojo] vs [LANGUAGE: Python]
https://github.com/p88h/aoc2023/blob/main/day01.mojo
https://github.com/p88h/aoc2023/blob/main/day01.py
Benchmark results:
The parallelized code wasn't any faster than regular one when using more threads, probably due to something about Mojo that I don't understand :)
Either way, PyPy wins the parsing game.
3
u/Fyver42 Dec 02 '23
[LANGUAGE: assembly]
https://github.com/fyvonnet/AdventOfCode-2023-Assembly/blob/main/day02.asm
3
u/WilkoTom Dec 02 '23 edited Dec 02 '23
[Language: Rust] [Allez Cuisine!]
For your delectation: a humble Crab Greek Salad consisting of chopped tomatoes, cucumbers, Feta and of course Crab (sorry Ferris!)
(Oh, you just want the crab? It's here)
3
u/Cloudan29 Dec 02 '23
[LANGUAGE: Python 3]
There's probably some slick way to just put everything together into one 3xn array and do both parts without having to parse the input both times (as I did with my J solution), but this works and it's how I did it when I first solved it (because I always do Python first at midnight)
3
u/6f937f00-3166-11e4-8 Dec 02 '23
[LANGUAGE: Ruby]
3
u/encse Dec 02 '23
[LANGUAGE: C#]
Made it quite readable in my opinion, still in 40 lines.
https://github.com/encse/adventofcode/blob/master/2023/Day02/Solution.cs
3
u/FlockOnFire Dec 02 '23 edited Dec 07 '23
[LANGUAGE: Elixir]
https://github.com/janssen-io/AdventOfCode/blob/main/Elixir/lib/2023/02.ex
Probably not the fastest to scan and filter three times, but still more than fast enough for the early problems. :) And it made part 2 easy to implement as well.
3
u/-WorstWizard- Dec 02 '23
[LANGUAGE: Rust]
3
u/blacai Dec 02 '23
[LANGUAGE: F#]
There we go, some input parsing. This year I'm trying to avoid using "obscure" arrays to store information and use types, makes it easier to know what are the things interacting.
Link Year 2023 Day 02
3
u/WhyWouldYouDoThatQQ Dec 02 '23
[LANGUAGE: Golang]
Much simpler than yesterday.
Day2 Golang
3
u/Perohmtoir Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Excel]
When I looked at Part 2 I realized that I had already solved it.
The following function takes 2 arguments: a string input and a color (either "Game", "red", "green", or "blue"). it returns the maximum number of the relevant color in a game. If the color argument is "Game", it returns the game ID.
Put the input in column A, starting from A2. Put "Game", "red", "green" & "blue" in B1,C1,D1 & E1 respectively. Put the function in B2 & drag it down and right. At this point all required numeric data should be available and the rest of the exercice is trivial as far as Excel is concerned.
The hard part was to design the parser. If we extract it from the function above, you can see how that works in the function below (though it cannot be dragged down).
3
u/[deleted] Dec 02 '23
[LANGUAGE: awk] Part 1:
3
u/NeilNjae Dec 02 '23
[Language: Haskell]
A day where a declarative approach paid off. Write down some definitions from the problem description, define a monoid for combining revelations in a game, and you get something pretty clear, I think.
Full writeup on my blog, and code on Gitlab.
3
u/odnoletkov Dec 02 '23 edited Dec 02 '23
[LANGUAGE: jq] github
3
u/[deleted] Dec 02 '23
[LANGUAGE: Python]
Much simpler than day1 imo,
3
u/[deleted] Dec 02 '23
[LANGUAGE: Forth]
Today's challenge was a perfect match for Forth (not that Forth is ever not a perfect choice ;) ). We can define
Game,red,,blue;,green, etc. as words, turning the puzzle input into the code for our program!Here's how part 1 looks:
Part 2 was possible in the same way. You can check it out in my repo.
3
u/mnocenti Dec 02 '23
[LANGUAGE: Rust]
Solution on GitHub
3
u/purcell Dec 02 '23
[LANGUAGE: uiua]
Or view in Uiua Pad.
3
u/jotac13 Dec 02 '23
[LANGUAGE: rust]
As always, any criticism and suggestions are welcome!
https://github.com/joao-conde/advents-of-code/blob/master/2023/src/bin/day02.rs
3
u/tymscar Dec 02 '23
[LANGUAGE: Rust]
Day two of doing aoc in Rust, and while it still takes me much longer than with Python or JS, I enjoy writing it quite a bit. Spent ages however trying to debug why my Regex wasnt having a to_string() on it, and it was because I was using the binary version of the Regex package.
Part1 and Part2
3
u/chicagocode Dec 02 '23
[LANGUAGE: kotlin]
All of the work for me was in the parser, which I used `split`, `substringBefore` and `substringAfter` to build rather than regular expressions.
My code can be found on GitHub, I've written this up on my blog, and here are my 2023 Solutions so far. This is my seventh year of blogging about Advent of Code but I'm probably going to have to some of these after the fact, as I've just started a new job and have a few personal commitments in December that will cause delays.
3
u/dec0nstruct0r Dec 02 '23 edited Dec 04 '23
[LANGUAGE: R]
Not very memory efficient but some fun with a 3D array:
https://gitlab.com/Yario/aoc_2023/-/blob/master/AoC_02.R
3
u/juanplopes Dec 02 '23
[LANGUAGE: Python]
Both parts in 14 lines:
https://github.com/juanplopes/advent-of-code-2023/blob/main/day02.py
3
u/bo-tato Dec 02 '23
[LANGUAGE: Common Lisp]
https://github.com/bo-tato/advent-of-code-2023/blob/main/day2/day2.lisp
Part 2 was solved almost entirely with a fun use of the
loopmacro:3
u/Anna-cz Dec 02 '23
[LANGUAGE: Python]
Hey,
the second day in easy and very readable (I hope) Python with lots of comments that might help other non-programmers like me :)
Part 1
Part 2
3
u/rp152k Dec 02 '23 edited Dec 03 '23
[LANGUAGE: Common-Lisp]
3
u/rv5742 Dec 02 '23 edited Dec 03 '23
[LANGUAGE: Java]
Edit: Full solution:
3
u/frhel Dec 02 '23
[LANGUAGE: PHP]
https://github.com/frhel/AdventOfCode2023-PHP/blob/main/src/Solutions/Day2.php
3
u/Steinrikur Dec 02 '23 edited Dec 02 '23
Bash (grep)
Not golfed at all. Part 2 splits each line into multiple lines, and I use "Game" as separator.
3
u/Desthiny Dec 02 '23 edited Dec 02 '23
[LANGUAGE: Rust]
https://gist.github.com/AloizioMacedo/b686c6603d46b4b0e37cf34063e823c8
I set up a mdbook to document the thought process and also help people who are learning the language: https://aloiziomacedo.github.io/aoc2023/day_2.html : )
The full repo is here: https://github.com/aloiziomacedo/aoc2023
3
u/ummicantthinkof1 Dec 02 '23
[Language: python]
Trying to do these in one line where I can
Part 1
Part 2
3
u/MuffinHydra Dec 02 '23
[LANGUAGE: GO]
https://github.com/muffinhydra/Advent-of-Code-2023-GO
first time writing GO, style advice welcome!
3
u/jpjacobs_ Dec 02 '23
[LANGUAGE: J]
Parsing was most work, getting the input into boxed arrays of numbers-colour pairs. The logic itself is pretty straightforward: add limits, and sum per colour. Any game that has all sums negative is possible. Part 2 is even easier, as it's simply the per-colour minimum of all takes in the game.
3
u/wzkx Dec 02 '23 edited Dec 02 '23
[LANGUAGE: C]
More or less, translation for my Python solution.
01.exe was 42kB, this is 72kB. I guess, it's because of atoi().
3
u/gweicox Dec 02 '23
[LANGUAGE: Python]
I tried to make one-liners )
3
u/Szeweq Dec 02 '23
[LANGUAGE: Rust]
Code: https://github.com/szeweq/aoc2023/blob/master/src/bin/02.rs
Not even trying to use Vec, it is still very fast.
3
u/ValiantCookie Dec 02 '23
[LANGUAGE: Kotlin]
solution
So I had another much messier solve last night at release, but I'm really pleased with my second implementation today. At first I had a terrible amount of splitting the strings and iteration over the different games & sets. But after thinking about it more, pulling out the numbers via a lookahead regex is much better. With this function to parse the input, finding which games were invalid and what power each game had was simple :D
3
u/miptalan Dec 02 '23 edited Dec 02 '23
[Language: kdb+/q]
3
u/The-Average-Coder Dec 02 '23
[LANGUAGE: Python]
Found this one a brilliant example for beginners as to why knowing RegEx can be so useful, saves so much string manipulation mess.
https://github.com/The-Average-Coder/advent-of-code-2023/blob/main/Day%202/main.py
3
u/TheJReesW Dec 02 '23
[LANGUAGE: Python]
My solution
Something very interesting happens here. My code works, but only because the input I received had one of each color in every game. A friend got an input with a singular game that didn't contain blue cubes, and after testing my solution did not solve his input correctly.
It has to do with multiplying the powers with 0 if one of the colors isn't present. That's what I do but that is incorrect it seems. But seeing as my input didn't contain that scenario it didn't affect me and I solved it anyway.
3
u/stinkytoe42 Dec 02 '23
[LANGUAGE: Rust]
Day 2
(I seem to be having the opposite reaction to everyone. My day 1 solution seemed easier. Here's my day 1 post for comparison: Day 1)
3
u/penguinencounter Dec 02 '23
[LANGUAGE: Python + Factorio]
Image of the machine
Factorio's Combinators can be used to perform mathematical operations in-game. Unfortunately, I couldn't get directly loading the input working within Factorio, so there's a helper Python script that generates a blueprint containing your input in a way that the machine can understand.
Instructions, mod list, and source code on GitHub
3
u/dylan_mojo Dec 02 '23
[LANGUAGE: Awk]
Day 2
3
u/rjray Dec 02 '23
[LANGUAGE: Clojure]
GitHub
As is (too) often the case, I spent more time on getting the parsing of the data going than I did on the actual puzzles. I also parameterized the red/green/blue values out of an expectation that part 2 would be some perturbation of them (I was wrong). Once parsing was done, part 1 and part 2 both went pretty quickly. After the facepalm of day 1 part 2, this was a nice break.
(I also didn't start day 2 until about 10:40AM EST, due to being sick af last night when it unlocked in my timezone (MST).)
3
u/therouterguy Dec 02 '23
[LANGUAGE: Python]
https://gitlab.com/NetTinkerer/aoc02023/-/blob/main/day2.py?ref_type=heads Nothing special probably could use some list comprehension but this is more readable and it get the job done.
3
u/sojumaster Dec 02 '23
[LANGUAGE: PowerShell]
I thought of a better solution for part A, after Part B was done. I will go back and clean up the code. Today was much easier than Day 1.
Part 2
3
u/LuplexMusic Dec 02 '23
[LANGUAGE: SQL]
This was hard, I struggled a lot with parsing the strings and the resulting array data structures. It worked out in the end.
3
u/donald-ball Dec 02 '23
[LANGUAGE: babashka]
Part 1
Part 2
3
u/aptacode Dec 02 '23
[LANGUAGE: BASH]
Part 1
Part 2
3
u/ChillerOctopode Dec 02 '23 edited Dec 02 '23
[LANGUAGE: python]
I'm a beginner. Tried to do this year to see if i am able but i'm having difficult trying to get values from strings. Just completed part 1 and it seems a little bit messy to me.
https://github.com/SemKitten/Advent-of-Code-2023/blob/main/day2part1.py
3
u/[deleted] Dec 02 '23
[LANGUAGE: JavaScript]
today was a lot easier than day 1, tried to make my code as compact as I could but I don't typically write my code to be small so it was most definitely a different challenge. Super fun day regardless, excited for tomorrow :3
paste
3
u/biggy-smith Dec 02 '23
[Language: C++]
Parsing was tricky for this. Luckily my fudge parsing worked out in part 2!
https://github.com/biggysmith/advent_of_code_2023/blob/master/src/day02/day2.cpp
3
u/Cuyoyo Dec 02 '23
[Language: Javascript]
3
u/eregontp Dec 02 '23
[LANGUAGE: Ruby] Part 1 in 96 bytes:
3
u/Imaginary_Age_4072 Dec 02 '23
[LANGUAGE: Common Lisp]
Day 2
Fairly similar to most of the other solutions. Just parsed all the rolls and then for part one I had a function to test whether all the rolls were less than the given limits, and for part two just a function to calculate the game power.
3
u/bandj_git Dec 02 '23 edited Dec 02 '23
[Language: JavaScript]
Pretty straightforward day, I think the challenge was how to represent the cubes. I initially went with a map, mapping a color key to the count. But after thinking about the operations necessary on the data I quickly switched to an array, because I cared less about associating data to a particular color and more about performing operations across the cubes such as map/filter/reduce.
I ended up compressing the cubes taking the maximum cube count for each color.
This resulted in simple filtering in level one:
and easy power calculating in level two:
Runtimes:
github
3
u/[deleted] Dec 02 '23 edited Dec 02 '23
[Language: Python]
This one was pretty chill and fast. I decided to represent the cubes via array of dictionaries. Here's the code covering both exercises for today:
3
u/mendelmunkis Dec 02 '23
[LANGUAGE: C]
Ugly, but it works
0.482/0.487 ms
3
u/dannywinrow Dec 02 '23
[LANGUAGE: Julia]
(also known as the delightful hog's JowlEar dish)
[Allez Cuisine!]
https://github.com/dannywinrow/adventofcode/blob/main/2023/allez/2.jl
3
u/emceewit Dec 02 '23 edited Dec 03 '23
[LANGUAGE: Haskell]
Making use of monoids
GitHub
3
u/xavdid Dec 03 '23
[LANGUAGE: Python]
Step-by-step explanation: https://advent-of-code.xavd.id/writeups/2023/day/2/
Pretty straightforward today - Python's
re.findalland a regex got me all the pairs of count + color and adefaultdictmade it easy to calculate the min number required. Oddly, this felt more like a day 1 than yesterday did 😅3
u/ren1by Dec 03 '23
[Language: Python]
Part 1 and 2
Still trying to do without importing anything! Found today a bit easier
3
u/Callumpy Dec 03 '23
[Language: Java]
On my repo
Decided to go a bit all out and spent far too much time making it.
3
u/sikief Dec 03 '23
[LANGUAGE: C++]
[PLATFORM: Nintendo DS (Lite)]
Solution - Part 1 and 2, including graphical output
3
u/wzkx Dec 03 '23 edited Dec 03 '23
[LANGUAGE: J]
The data is processed twice, for part 1 and part 2. That is, for prod some optimization could be done :)
3
u/smiliclot Dec 03 '23 edited Dec 03 '23
[Language: Python 3]
3
u/Thomasjevskij Dec 03 '23
[LANGUAGE: Python]
I was quite surprised when it just boiled down to keeping track of three max values in both parts. With some proper parsing of the input, the actual solutions boil down to very tidy sums of list comprehensions.
3
u/blackdev1l Dec 03 '23
[LANGUAGE: Clojure]
I wonder if there are better ways to use the keys such as [:blue :green :red] and iterate over it to create all the structs Git
3
u/Emotional_Cicada_575 Dec 03 '23 edited Dec 04 '23
[LANGUAGE: Haskell]
Feedback is welcome !
CODE
3
u/Derailed_Dash Dec 03 '23 edited Dec 11 '23
[LANGUAGE: Python]
Solution in Jupyter notebook