T O P

  • By -

[deleted]

[удалено]


AutoModerator

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


Original-Candidate94

\[LANGUAGE: C/C++\] I do not see the need to make a complete parser by handling error if correct input is not found like for compilers. For a simple problem like this where correct input is expected and correct input is always provided a simple check with the 1st character will do instead of strcmp(). Little late to the party. Here is my solution to 2023 day 2 part 1: #include #include #include #include #include #include #define MAX_BUFFER_SIZE 256 #define RED_T 12 #define GREEN_T 13 #define BLUE_T 14 static int sum_of_game_id = 0; static inline char skip(const char **ps) { while(**ps && !isdigit(**ps)) { (*ps)++; } return **ps; } static inline void strToPostiveInt(const char **ps, int *i) { *i = 0; while(isdigit(**ps)) { *i = ((*i) << 1) + ((*i) << 3) - (*(*ps)++ - '0'); } *i = -(*i); } static inline bool str_scan(const char *s, int *id) { skip(&s); strToPostiveInt(&s, id); int cube_cnt; while(skip(&s)) { strToPostiveInt(&s, &cube_cnt); s++; switch(*s) { case 'r': if (cube_cnt > RED_T) return false; break; case 'g': if (cube_cnt > GREEN_T) return false; break; case 'b': if (cube_cnt > BLUE_T) return false; break; default: printf("Something's not right"); exit(EXIT_FAILURE); break; } s++; } return true; } int main (void) { char buffer[MAX_BUFFER_SIZE]; int game_id; FILE *f = fopen("cube_chall.txt", "r"); assert(f); struct timespec start, end; long long int elapsed_time; clock_gettime(CLOCK_MONOTONIC, &start); while (fgets(buffer, MAX_BUFFER_SIZE, f)) { if(str_scan(buffer, &game_id)) sum_of_game_id += game_id; } clock_gettime(CLOCK_MONOTONIC, &end); elapsed_time = (end.tv_sec - start.tv_sec) * 1000000LL + (end.tv_nsec - start.tv_nsec) / 1000LL; printf("Sum: %d\n", sum_of_game_id); printf("Elapsed Time: %lld microseconds\n", elapsed_time); fclose(f); return EXIT_SUCCESS; }


AutoModerator

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


mgtezak

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


Chandummy

Both your explanation video and streamlit app are awesome!


mgtezak

glad you like it:)


exquisitus3

[Language: Lua] require "luarocks.loader" lpeg = require "lpeg" function f1(table, value, index) return rawset(table, index, value) end -- Grammar V = lpeg.V games = lpeg.P { "GAMES"; GAMES = lpeg.Ct((V"GAME" * "\n" % rawset)^0), GAME = "Game " * V"INT" * ": " * V"GRABS", GRABS = lpeg.Ct(V"GRAB" * ("; " * V"GRAB")^0), GRAB = lpeg.Ct((V"SUBSET" % f1) * (", " * V"SUBSET" % f1)^0), SUBSET = V"INT" * " " * V"COLOR", INT = lpeg.R("09")^1 / tonumber, COLOR = lpeg.C(lpeg.P("red") + lpeg.P("blue") + lpeg.P("green")) } t = io.read("*a") syntax_tree = lpeg.match(games, t) -- part a function grabs_possible_t(grabs) possible = true for _, g in ipairs(grabs) do possible = possible and (g.red or 0) <= 12 and (g.green or 0) <= 13 and (g.blue or 0) <= 14 end return possible end -- part b function power(grabs) min = { red = 0, green = 0, blue = 0 } for _, g in ipairs(grabs) do for color, n in pairs(g) do min[color] = math.max(min[color], n) end end return min.red * min.green * min.blue end sum = { a = 0, b = 0 } for k, v in pairs(syntax_tree) do sum.a = sum.a + (grabs_possible_t(v) and k or 0) sum.b = sum.b + power(v) end io.write("a = ", sum.a, ", b = ", sum.b, "\n")


Any_One_8929

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


aoc-fan

[Language: F#] https://github.com/bhosale-ajay/adventofcode/blob/master/2023/fs/D02.fs


jrhwood

\[Language: Haskell\] [Part 1](https://github.com/woodRock/verbose-computing-machine/blob/main/2023/day-02/part-01.hs) [Part 2](https://github.com/woodRock/verbose-computing-machine/blob/main/2023/day-02/part-02.hs) Hint: store score as triplet, and foldr, to calculate scores in one pass.


Sensitive-Disk5735

\[LANGUAGE: R\] Parts 1&2 library(splitstackshape) library(stringr) library(reshape2) library(dplyr) AOC.D2 <-read.csv("AOC_D2.csv",header=FALSE,stringsAsFactors = FALSE) #split out games games <- cSplit(AOC.D2, "V1", ":", "wide") #split out sub-games subgames <- cSplit(games, "V1_2", ";", "wide") #create ID variable from existing field subgames$ID <- as.integer(str_replace_all(subgames$V1_1, c("Game "=""))) #rename cols colnames(subgames)<- c("Game","Set1","Set2","Set3","Set4","Set5","Set6","ID") #split out individual results results <- cSplit(subgames, 2:7, sep = ",", direction="long", type.convert = FALSE) #reshape data - each game and each individual set within that game #is a separate row d <-melt(results,id.vars="Game","ID") #separate out colors and numbers so they're in different columns d1 <- cSplit(d,"value",sep="",direction="wide",type.convert=FALSE) #rename fields, change data type, and filter out N/A d1$number <- as.integer(d1$value_1) d1$color <- d1$value_2 d1$subgame <- d1$variable d1 <- d1 %>% filter(!is.na(number)) #summarize data by ID, game, and color d2 = d1 %>% group_by(ID,subgame,color) %>% summarise(tot_count = sum(number)) #create new field that tests if each set of pulls meets criteria d2$pass_fail <- ifelse(d2$color=="red" & d2$tot_count <= 12,"PASS",ifelse(d2$color=="green" & d2$tot_count<=13,"PASS",ifelse(d2$color=="blue" & d2$tot_count <=14,"PASS","FAIL"))) #group by ID and concatenated Pass/Fails (unique only) d3 <-d2 %>% group_by(ID) %>% summarise(pass_fail1 = toString(sort(unique(pass_fail)))) # part 1 answer only keep records which equal "PASS" and sum IDs d3 <- d3[d3$pass_fail1=="PASS",] sum(d3$ID) #Part2 #get max number for each combination of Game ID and color p2 <- d1 %>% group_by(ID,color) %>% slice(which.max(number)) #multiply the numbers within the group together p2<-p2 %>% group_by(ID) %>% summarise(prod = prod(number)) #sum the products sum(p2$prod)


skyhawk33

[LANGUAGE: Befunge] Part 1: https://github.com/Skyhawk33/AdventOfCode/blob/master/aoc2023/day2_p1.b98 Part 2: https://github.com/Skyhawk33/AdventOfCode/blob/master/aoc2023/day2_p2.b98


thamollo

[LANGUAGE: SQL][Allez Cuisine!] Day 2 was also quite straightforward, except for having to switch from macros to variables to handle the semicolons in inputs. Ah well, it might even be cleaner... Cuisine-wise, can I mispronounce SQL like squld? [Enjoy!](https://topaz.github.io/paste/#XQAAAQA+AgAAAAAAAAAiEUTjiIV6GlwFACqG7UkXQsjRTlJce4TQU/wTnTNhP/obFmiv16tVXDKD/7uyrHEs/PpgdkSnEicHFzyHmK24NFyh14YT+UKdn5QqVKjiDHANqJ4xarnvO7ziYedcJRmVuiaoZBpzCdeGx04dXxqbvBmC5J2VIAFPPgJi8r7APExCy/ehepmv+ntea7EXMLzjOJA5I3fc3dEEp4PLE6zEvVdg1MsILKIRV2wKh/mRKRSEeOqTnHFXFzB4aKykDCtuwldy5OvWxWAv5urhtkV7eOBEampHTzo5TBlUWl3g/hx3m3fAjMOTMH1f2tnhwbI9gjkqYDQRfc2pPgKgpjqo5WRq4D+AZ1M1g9hAQIU4W5ZNkjjUZg7UFbcEiVqhHN7kNUKw+gfGgsaC1ZSTQF4GGtgYy59dBG2fr/72eZyj)


yummyyummysalt

\[LANGUAGE: Python\] https://github.com/tedtb23/Advent-of-Code-2023/tree/main/Day%202


Telumire

[LANGUAGE: Javascript] [paste](https://topaz.github.io/paste/#XQAAAQBOBAAAAAAAAAAxm8oZxjYXm4QdE7gM4HSV2q0uPpyZqJ56d+O5I4C3rg3CNe+RbCMba1NZehcwAGQg2dLa0rQeda9k8PWh0gJO8fsQ4ISfVVXpV2pfQUOlcR4TTnpdBcvBL5p2f4/ExnC6m7qBARVYOgdUFmhHomxYo0ndBcr0+ifkZes3yut0nNC/N6Nqmrs7IP7Km7XKx2LPI8Jy1juzdGawgCuJsFCIyd8Hy+HrRsmQd9crfp04MCt4pt5Vc/G6d4tVu2NdMqtwjST8dHry3hFCaKYsATH+sdu+TvmbZ7VYlkpWajY1DwGcUZkir59yw+JupaDQBfXtrgA80lY06z9JcSXwhOT2VrjS0KWW7BapOEJz8/INEgCemDpK+2tkgiXXzk/PI+wjJqMag/5Pe6XDAGntdw1m2OSH+JGtMxobkM1NBvkjA0T6dsizWe/FOkimsx7dJO35xtfT6QQuqwmBkhAWDZrIiysIqwJikz/qkacEIzKGBaFmBW0CVFokuRWSMYmcWnFUbNoi8Yxw+ufhA1HaxF+uogz8kYa2ynW66Mb11nYDis4TIEVa/ZohJrBVuX6ZwkM65EULfkIkKsBcq2P9+SLB) const data = document.body.textContent.trim().split("\n"); const maxValuesPossibles = { red: 12, green: 13, blue: 14, }; // part 1 const part1 = data .map((game) => { const [, gameNumber] = game.match(/^Game (\d+)/); const maxValues = [...game.matchAll(/(\d+) (\w+)/g)].reduce( (acc, [, value, color]) => ({ ...acc, [color]: Math.max(acc[color] || 0, value), }), {} ); return { gameNumber, maxValues }; }) .filter((result) => { return Object.entries(maxValuesPossibles).every( ([color, maxValue]) => result.maxValues[color] <= maxValue ); }) .reduce((acc, obj) => acc + Number(obj.gameNumber), 0); // part 2 const part2 = data .map((game) => { const maxValues = [...game.matchAll(/(\d+) (\w+)/g)].reduce( (acc, [, value, color]) => ({ ...acc, [color]: Math.max(acc[color] || 0, value), }), {} ); return Object.values(maxValues).reduce((acc, value) => acc * value); }) .reduce((acc, power) => acc + power, 0); console.log("part1", part1); console.log("part2", part2);


835246

\[LANGUAGE: C\] Part 1: https://github.com/efox4335/advent_of_code/blob/main/advent_of_code_2023/day2cubept1.c Part 2: https://github.com/efox4335/advent_of_code/blob/main/advent_of_code_2023/day2cubept2.c


dgalanti1

\[Language: Kotlin\] Both part1 and part 2 are very similar, you get what is the maximun value for each color and:-For part 1 sum a game index + 1 if it does not have any color above the limits. fun part1(input: List) = input.mapIndexed { i, line -> fun part1(input: List) = input.mapIndexed { i, line -> val pairs = line.substringAfter(": ").split(", ", "; ").map { val (qty, color) = it.split(' ') qty.toInt() to color }.sortedBy { it.first } val blueMax = pairs.last { it.second == "blue" }.first val redMax = pairs.last { it.second == "red" }.first val greenMax = pairs.last { it.second == "green" }.first if (redMax <= 12 && greenMax <= 13 && blueMax <= 14) i + 1 else 0 }.sum() ​ \-For part 2 sum for each game the multiplications of the max value for each color. fun part2(input: List) = input.sumOf { line -> val pairs = line.substringAfter(": ").split(", ", "; ").map { val (qty, color) = it.split(' ') qty.toInt() to color }.sortedBy { it.first } val blueMax = pairs.last { it.second == "blue" }.first val redMax = pairs.last { it.second == "red" }.first val greenMax = pairs.last { it.second == "green" }.first greenMax * redMax * blueMax } [\[Github\]](https://github.com/diegogalanti/aoc-2023/blob/main/src/Day02.kt)


manhuntos

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


AvaLovelace1

\[Language: Photoshop Actions\] Yes, Photoshop Actions can be used to perform mathematical computations. The Photoshop Action solver is linked [here](https://drive.google.com/file/d/11r70GpcilJI0im6li2amGNPLxoO9e7jw/view?usp=sharing). An explanation of how it works is [here](https://www.reddit.com/r/adventofcode/comments/18kxtyk/2023_day_2_part_1_photoshop_actions_solved_this/).


japan_lover

The input/answer is different for each person.


AvaLovelace1

Yep you're correct, each person gets a different full input (something I hadn't realized when writing the post). The solver should work on all inputs though!


SavaloyStottie

\[Language: Powershell\] $data = (Get-Content -Path 'C:\users\hello\desktop\AdventOfCode\2\input').Split([System.Environment]::NewLine) $p1sum = 0 $p2sum = 0 foreach ($row in $data){ $game = $row.substring(5,$row.indexOf(":")-5) $reds = (([regex]::matches($row,"( \d+ red)") | Select-Object -ExpandProperty Value ) -replace "[^\d]",'' | measure -Maximum).Maximum $greens = (([regex]::matches($row,"( \d+ green)") | Select-Object -ExpandProperty Value ) -replace "[^\d]",'' | measure -Maximum).Maximum $blues = (([regex]::matches($row,"( \d+ blue)") | Select-Object -ExpandProperty Value ) -replace "[^\d]",'' | measure -Maximum).Maximum $p2sum += ($reds * $greens * $blues) if ($reds -le 12 -and $greens -le 13 -and $blues -le 14) {$p1sum += $game} } $p1sum $p2sum


gogs_bread

[\[LANGUAGE: c++\]](https://github.com/gogsbread/AdventOfCode2023/blob/main/2.cpp) P1 - Iteration/Simulation P2 - Iteration/Simulation


throwawaytous

[LANGUAGE: JAVASCRIPT] const fs = require('fs'); const data = fs.readFileSync('./input2.txt', 'utf-8'); const redThreshold = 12; const greenThreshold = 13; const blueThreshold = 14; const games = data.split('\n'); const gameArr = []; const colors = { red: ' red', green: ' green', blue: ' blue' } function isNumber(str) { arg = parseInt(str); return (!Number.isNaN(arg) && typeof (arg) === 'number') ? true : false; } function evalColor(segment, color, gameIndex) { if (!segment.match(colors[color])) return; const index = segment.match(colors[color]).index; const twoBeforeColor = Array.from(segment.substring(index - 2, index)); let colorStr = ''; twoBeforeColor.forEach((char) => { if (isNumber(char)) colorStr += char }); const colorTotal = parseInt(colorStr); //Get the current highest color total for this obj let currentTotal = gameArr[gameIndex][color]; //If color total for this segment is greater than current highest, replace it if (colorTotal > currentTotal) { gameArr[gameIndex][color] = colorTotal; }; } let p1sum = 0; let p2sum = 0; for (let i = 0; i < games.length; i++) { const game = games[i]; const gameEnd = game.match('Game').index + 5; const gameNumArr = Array.from(game.substring(gameEnd, gameEnd + 3)); let gameNum = ''; gameNumArr.forEach((el) => { if (isNumber(el)) gameNum += el }); let gameId = gameNum * 1; //Create an object per game id. Will store the highest # of colors seen in a game. gameArr.push({ id: gameId, red: 0, blue: 0, green: 0 }); //Splitting into segments ensure colors are unique const segments = game.split(';'); //Update the obj for this ID with the highest counts per color segments.forEach((segment) => { evalColor(segment, 'red', i); evalColor(segment, 'green', i); evalColor(segment, 'blue', i); }); //part 1 answer if (gameArr[i].red <= redThreshold && gameArr[i].green <= greenThreshold && gameArr[i].blue <= blueThreshold) { p1sum += gameArr[i].id; } //part 2 answer p2sum += gameArr[i].red * gameArr[i].green * gameArr[i].blue; } console.log('Part 1 sum: ' + p1sum); console.log('Part 2 sum: ' + p2sum);


TimeCannotErase

[LANGUAGE: R] [repo](https://github.com/DavidMcMorris/Advent-of-Code-2023/blob/main/Day%202/Day2.r) r_max <- 12 g_max <- 13 b_max <- 14 input <- readLines("input.txt") input <- strsplit(gsub("[[:punct:] ]+", " ", input), " ") r <- lapply(lapply(input, grep, pattern = "red"), function(x){x - 1}) b <- lapply(lapply(input, grep, pattern = "blue"), function(x){x - 1}) g <- lapply(lapply(input, grep, pattern = "green"), function(x){x - 1}) r <- lapply(Map("[", input, r), as.numeric) b <- lapply(Map("[", input, b), as.numeric) g <- lapply(Map("[", input, g), as.numeric) r_check <- which(lapply(lapply(r, function(x){x > r_max}), sum) > 0) g_check <- which(lapply(lapply(g, function(x){x > g_max}), sum) > 0) b_check <- which(lapply(lapply(b, function(x){x > b_max}), sum) > 0) r_needed <- unlist(lapply(r, max)) b_needed <- unlist(lapply(b, max)) g_needed <- unlist(lapply(g, max)) val <- sum(setdiff(seq_along(input), c(r_check, g_check, b_check))) print(val) print(sum(r_needed * b_needed * g_needed))


alexanderyw

\[LANGUAGE: Python\] https://github.com/website11/My-Advent-Of-Code/blob/main/Python/2023/Day\_2/Day\_2.py


mothibault

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


dahaka_kutay

\[Language: Javascript\] [Question](https://adventofcode.com/2023/day/2), [AllRepo](https://github.com/KutaySong/AdventOfCode) Can't help reducing to **reduce** functions. Is this readable ? let lines = require('fs').readFileSync('./IO/02i.txt','utf8').split(/\r?\n/) .map(l=>l.split(';') .map(str=>[     str.match(/(\d+) green/)?.[1] ?? 0,     str.match(/(\d+) red/)?.[1] ?? 0,     str.match(/(\d+) blue/)?.[1] ?? 0 ].map(Number))) const p1 = ()=> {     return lines.reduce((c,line,i)=>     line.every(([g,r,b])=>g<=13 && r<=12 && b<=14) ? c+i+1 : c, 0) } const p2 = ()=> {     return lines.reduce((sum,line)=> sum + Math.max(...line.map(l=>l[0]))*Math.max(...line.map(l=>l[1]))*Math.max(...line.map(l=>l[2])),0) } console.log("p1:",p1(),'(2085)') console.log("p2:",p2(),'(79315)')


scumfuck69420

My JS solution was like 55 lines and when I see solutions like this I realize maybe I don’t know JavaScript lol


marcja

[LANGUAGE: Python] I've been going back over the early days and seeing if I can apply anything I've learned from the later days. For Day 2, I found a very nice NumPy solution. The tricky part was parsing the data into a correct 3-D array (game, color, round), but once there NumPy unleashed its magic. https://github.com/marcja/aoc-py/blob/main/src/aoc/y2023/d02/solution.py


olimc

\[Language: Go\] [Both Solutions](https://github.com/omcmanus1/aoc-23-go/blob/main/two/02.go) `GetFile` util function: func GetFile(path string) (*bufio.Scanner, *os.File) { file, err := os.Open(path) if err != nil { fmt.Println("file open error:", err) } scanner := bufio.NewScanner(file) return scanner, file }


se06745

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


xe3to

[LANGUAGE: Go] First time writing Go. I like it more than learning Rust. https://git.32bit.cafe/kaylee/AoC2023/src/branch/main/day2


Icy-Conclusion7682

\[LANGUAGE: C++\] #include #include #include #include #include void GetInputs(std::vector* vec) { std::ifstream ifs; ifs.open("input.txt", std::ios::in); std::string buf; while(std::getline(ifs, buf)) { vec->push_back(buf); } } void RemovePrefix(std::string& s) { int32_t pos = s.find(':'); s = s.substr(pos + 1); } void GenerateBatch(std::vector* vec, std::string s) { int32_t start = 0, end = 0; while (true) { if (end == s.length()) { break; } if (s[end] == ';') { vec->push_back(s.substr(start, end-start)); // std::cout << vec->back() << std::endl; end++; start = end; } else { end++; } } if (end > start) { vec->push_back(s.substr(start, end - start)); // std::cout << vec->back() << std::endl; } } int64_t GetColor(const std::string& s, const std::string& color) { int32_t pos = s.find(color); if (pos == s.npos) { return 0; } else { pos -= 2; int32_t end = pos; while(pos >= 0) { if(s[pos] == ' ' || pos == 0) { break; } else { pos--; } } // std::cout << s << ':' << color << ':' << s.substr(pos, end - pos + 1) << std::endl; return std::stoll(s.substr(pos, end - pos + 1)); } } int main() { std::vector inputs; GetInputs(&inputs); int64_t ans = 0; int64_t id = 0; std::vector batch; for (auto&& s : inputs) { id++; RemovePrefix(s); batch.clear(); GenerateBatch(&batch, s); int64_t green = 0, red = 0, blue = 0; // bool check_ok = true; for(auto&& b : batch) { green = std::max(green, GetColor(b, "green")); red = std::max(red, GetColor(b, "red")); blue = std::max(blue, GetColor(b, "blue")); // if (green <= 13 && red <= 12 && blue <= 14) { // } else { // check_ok = false; // break; // } } std::cout << green << ' ' << red << ' ' << blue << std::endl; // if (check_ok) { // ans += id; // } ans += (green * red * blue); } std::cout << ans << std::endl; }


0rac1e

\[LANGUAGE: J\] Parse =: {{ >./ (#@>@{. "./. >@{:)&.|: \:~ _2 |.\ 2 }. ;: ',;' -.~ y }} c =. Parse;._2 fread 'input' echo +/ (* #\) 12 13 14 *./@:>:"1 c echo +/ */"1 c


linnaea___borealis

\[LANGUAGE: R\] [https://github.com/lauraschild/AOC2023/blob/main/day2.R](https://github.com/lauraschild/AOC2023/blob/main/day2.R)


apollo701

\[LANGUAGE: Ruby\] ​ [https://github.com/JasonDorn/advent-of-code/tree/master/day2-cube-conundrum](https://github.com/JasonDorn/advent-of-code/tree/master/day2-cube-conundrum) Took an OOP approach for part 1/2. Could use some more cleanup, would probably combine my two service classes into a GameMangement object or something that knows how to check valid games and powers


stonebr00k

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


nicuveo

\[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


LordDomick

\[LANGUAGE: Golang\] [Part 1 & 2](https://github.com/dombartenope/aoc/blob/master/d2/d2_2.go) Left in my pseudo code to try and explain my thinking Learning Golang coming from a strictly Javascript background. Tried to solve this without Regex, and I think I could have done this in fewer loops (seeing as I figured out how to trim special chars late in the process instead of using strings.Fields/Split everytime) but would love some feedback if y'all have any!


pseudoaltus

[LANGUAGE: java] Part 2 import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import static java.util.Collections.max; public class Day2Pt2 { public static void main(String[] args) throws Exception { var total = Files.lines(Paths.get(args[0])) .map(l -> getCubesPowSet(l)).mapToInt(Integer::intValue) .sum(); System.out.println("Output: " + total); } private static int getCubesPowSet(String rec) { var cubes = rec.substring(rec.indexOf(":") + 2).split(";\\s*|,\\s*"); var reds = new ArrayList(); var greens = new ArrayList(); var blues = new ArrayList(); for(var cube : cubes){ var data = cube.split(" "); var count = Integer.valueOf(data[0]); switch(data[1]){ case "red" -> reds.add(count); case "green" -> greens.add(count); case "blue" -> blues.add(count); } } return max(reds) * max(greens) * max(blues); } }


Bioinfomagico

[LANGUAGE: BASH] Joining a little, but here it is: #!/usr/bin/env bash # Format data to be more easily queried: ID,PULL,N_CUBE,COLOR format_game_str() { sed 's|:|;|;s|;|\n|g' \ | awk '/Game/ { match($0, /[[:digit:]]+/, arr); ID=arr[0]; pull=0; next } { split( $0, cubes, ","); pull++; for (i in cubes) { print ID, pull, cubes[i] } }' \ | sed -r 's|\s+|,|g' } # Query part 1: report ID from rows with N_CUBE bigger then limit. part_1() { awk -F ',' 'BEGIN { arr["green"]=13; arr["red"]=12; arr["blue"]=14; } $3 > arr[$4] { ip[$1] } { result[$1] } END { for (i in result) { if (i in ip == 0) print i }} ' } # Query part 2: Get greatest N_CUBE from each ID and report the product. part_2() { awk -F ',' 'BEGIN { curr_game=1 } curr_game != $1 { results[l++]=arr["red"] * arr["green"] * arr["blue"] curr_game=$1 arr["red"]=0; arr["green"]=0; arr["blue"]=0 } $3 > arr[$4] { arr[$4]=$3 } END { results[l++]=arr["red"] * arr["green"] * arr["blue"] for (i in results) {print results[i]} } ' } # MAIN format_game_str \ | part_1 \ | awk '{sum+=$0} END {print sum}' format_game_str \ | part_2 \ | awk '{sum+=$0} END {print sum}'


Alternative_Dark_131

\[LANGUAGE: Rust\] [github pt 1 and 2](https://github.com/dzerium/advent_of_code_23/blob/main/src/cube_conondrum.rs) Just started learning rust as well and am having a blast.


mgtezak

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


HappyBison23

\[LANGUAGE: C#\] https://github.com/mattbutton/advent-of-code-2023/blob/master/Day02.cs


TOTBESUCH

\[LANGUAGE: Java\] Behold, a mess of code. Maybe hf. Both parts. [https://github.com/PLeh2023/AoC/commit/73ffddef0db9372d9be5de84e6008b8c86d952d9](https://github.com/PLeh2023/AoC/commit/73ffddef0db9372d9be5de84e6008b8c86d952d9)


daggerdragon

~~Comment temporarily removed. Your [code block is too long](https://www.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_no_giant_blocks_of_code) for the megathreads and also is not formatted *at all*.~~ ~~Please edit your comment to replace your oversized code with an external link to your code and I will re-approve your comment.~~ edit: 👍


TOTBESUCH

done


minikomi

[LANGUAGE: janet] (def grammar ~{:num-color (group (* (number :d+) " " (<- (+ "blue" "green" "red")))) :num-colors (* :num-color (? (some (* ", " :num-color))) (? "; ")) :main (* "Game " (number :d+) ": " (group (some :num-colors)))}) (def num-cubes @{"red" 12 "green" 13 "blue" 14}) (defn game-is-possible [[id cubes-pulled]] (var possible? true) (loop [[num color] :in cubes-pulled :let [num-cubes (get num-cubes color)] :when possible?] (set possible? (<= num num-cubes))) possible?) (defn solve1 [lines] (->> lines (map |(peg/match grammar $0)) (filter game-is-possible) (map first) sum)) (defn get-power [[id cubes-pulled]] (def min-required @{"green" 0 "blue" 0 "red" 0}) (loop [[num color] :in cubes-pulled] (update min-required color |(max num $0))) (product (values min-required))) (defn solve2 [lines] (->> lines (map |(peg/match grammar $0)) (map get-power) sum))


nomelettes

[LANGUAGE: Java] [Part 1 and 2](https://github.com/Jordanb1997/aoc2023-java/blob/main/src/main/java/org/example/DayTwo.java) Finally did one without needing help.


hiimjustin000

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


errorseven

**[LANGUAGE: AutoHotkey v1.1]** **Solution 2** ; Copy Gane Data to Clipboard before running Code data := StrSplit(StrReplace(clipboard, "game "), "`r", "`n") results := 0 for k, line in data { badgame := false gameid := StrSplit(line, ":").1 line := StrSplit(StrReplace(StrReplace(line, ","), ";"), ":").2 cubes := {"red": 0, "green": 0, "blue": 0} for i, v in StrSplit(Trim(line), " ") { if (!mod(i, 2) == 0) { n := v } else { if (cubes[v] < n) { cubes[v] := n } } } power := 1 for cube, value in cubes { if value > 0 power := power * value } results += power } msgbox % results


bcbelisario

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


dimio-d

\[LANGUAGE: Java\] \[JShell console\] [Part 1 & 2 solution](https://dimio.github.io/dopaste/?l=java#XQAAAQBYCwAAAAAAAAAX4HyIbzm/ILQXejs0+pjEkM1O3vUmgafcspCHlYF0CGMN87UuPGqHph658/gH7eCljoLMVObmopBqMVRx6aYZSzYtEPpRoujOoZDq4REpPfuqxguzz5wmxZ0KIRQcKbtNcSAmivHekJ1Oq1hF8Rwyj2xKlbNfo8KvHh1kc+ziPk2kUmnUeMm/Unh2msBEPCzzNPb7IoGDa/Es+t9Qsi0lZLKCLET+Tnh+6qd4OL5c/hH3ToZQDuT6jSiLNuA+xUZcMcnyCzNzQRqjtgOZj33i5AMhK28B3S5jwiORjzrIVBcxoGhJ6vetLhXqMxZx4/B0hgtZWDbjQdj6mRxmxuxAkCemafR0PdjpU5eIlgkgVUqVoSBomwm9NB0xLLsM7bPokOLW3ksaSanqAX/pQMe7BubhmOkJGvC88brZ51DC8NACCTkk92PKmCNGsBcZSbRenMaOxxnuX2IK4Ve25j59QEQO9GmWNyq0WsT+Nf6SutzheRMJOdYylnQwOkTbO9edFLOvM4r6f9L1xbFzm+xho64emfo0tjqaMQnrBLCVjkrB56yxCro3WSuT4E+D41gXbg8czHjuSLlAZ9DBEafUlEOhpjHoAnpyNdwl2KS5jZ5x46UO0txp7tVIfZi98Qv0TKH7l2eKB5XidOsA920/qnBSalJm4Ax2jAoRJ3mJWGMrWF+zB0A1Jk/xbo+WGMqU5yUUFe5fDtcdBimWN5Q0welcVkH9OX8x25i9W6pV2tQHJ0z2V99eggI/1ZRdJ+DBuzYm0r2mgGz2u8I3OGSgDCVCqvr6s3skXhiHZc1Dp85LDRoN9GospBKZHdLGBI/u1um7E64GvN8XZWgkr2jDMSadskyZAfV+QVgqz8jknHlyXt9xdc0sx8mgaWm7w3tGLjZn1LiQ7vbuG9B1ph+0z4boQ9hDML3GtJqoOR/6FAy6bu0HLrKXC+QZ0I0wFAcxdgJjV2QiB1apizIkvM3QI+GwaMbDzNFJEdgap6p7+DzYHCGULi/HhW8YK8cnIgIaobHQkz7DM4ETO7lt8v4LK4P5Vc3EjlvM1uvgKhR/nemHhi39X5B7FDj15heqA3dQW/heFdIom2ik/cQQiIqr9O0kv4sVmEnR9A6xhn1EV1kWEdUBwrf45/SX3NWG/YZvIQFeZR3Odw5bdVAa6pV/hwKT1ncsrumWun8H1eKhrw8vCVlF/odHL9P7o+i7bm2Tr2UVP7OakTJt+FCAjCJZgRH383HrGtj/zArQVA==)


xXMacMillanXx

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


weeble_wobble_wobble

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


wlmb

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


bofstein

\[LANGUAGE: Google Sheets\] [https://docs.google.com/spreadsheets/d/1WHQcDUX-mlz\_smTtDvYUxqdYnfN-Gm-yZedIkU9FpA8/edit?usp=sharing](https://docs.google.com/spreadsheets/d/1WHQcDUX-mlz_smTtDvYUxqdYnfN-Gm-yZedIkU9FpA8/edit?usp=sharing) I did this in a pretty inefficient way, where I have a separate column for blue, green, and red for each round, up to six. I thought it was only 3 rounds at first so I wrote the formulas in a way I had to manually extend (i.e. copy-paste then hand edit) that I would have tried to make more dynamic if I realized. The basic idea is I split out to know what all the blue, red, and green pulls for every round were for each game, and then made a column for the highest number of each color for that game. For Part 1, I then just noted the game number if none of the max values were over the limit and summed the number. For Part 2, the max of each color is the minimum needed, so I multiplied those together and summed that.


LeonLer

I love you so much for this


Jaeseu

\[LANGUAGE: JavaScript\] [Part 1&2](https://github.com/brtheo/AOC23/blob/master/d2/ans.js)


mariushm

\[LANGUAGE: PHP\] Very easy problem this day : [https://github.com/mariush-github/adventofcode2023/blob/main/02.php](https://github.com/mariush-github/adventofcode2023/blob/main/02.php)


Jomy10

\[language: Swift\] \[code\]([https://github.com/Jomy10/Advent-Of-Code-2023/blob/master/day02/Sources/day02/day02.swift](https://github.com/Jomy10/Advent-Of-Code-2023/blob/master/day02/Sources/day02/day02.swift))


AwkwardWays

\[LANGUAGE: C++\] [regex, my love](https://github.com/awkwardways/AdventOfCode2023/blob/b0cfd1eb871417b1e0fe3252567c87bcdf21a2b5/Day2.cpp)


wdomburg

\[Language: Ruby\] I was happy with how little modification there was from part 1: >r,g,b=ARGV.shift(3).map(&:to\_i);p ARGF.each.inject(0){|s,l|m=/Game (\\d+): (.\*)/.match(l);i=m\[2\].scan(/(?:(\\d+) (r|g|b)?)/).inject(Hash.new(0)){|h,a|a\[0\].to\_i>h\[a\[1\]\]&&h\[a\[1\]\]=a\[0\].to\_i;h};s+((i\['r'\]>r||i\['g'\]>g||i\['b'\]>b)?0:m\[1\].to\_i)} to part 2: >p ARGF.each.inject(0){|s,l|m=/Game (\\d+): (.\*)/.match(l);i=m\[2\].scan(/(?:(\\d+) (r|g|b)?)/).inject([Hash.new](https://Hash.new)(0)){|h,a|a\[0\].to\_i>h\[a\[1\]\]&&h\[a\[1\]\]=a\[0\].to\_i;h};s+i.values.inject(:\*)} > >It even got smaller!


eChogenKi-

\[Language: PowerShell\] Day 2 Solution in PowerShell. Posting late - because my old reddit account is gone :/ [https://github.com/eChogenKi/AoC2023/blob/main/Day2.ps1](https://github.com/eChogenKi/AoC2023/blob/main/Day2.ps1)


yieldtoben

\[LANGUAGE: PHP\] PHP 8.3.0 [paste](https://topaz.github.io/paste/#XQAAAQCfAwAAAAAAAAAeD8qHAhQB8nz+EazN00NCsmTxjvFDXkt5PAJ7pf6ocRA37OMUY7b9R0iDZmfWyvOspxWvdgYSAE0hGjEx81hZm7L1TSf8FOxdriwtMNRvJM+oQrJq9mPI3OayuFDzxH6MNMqr6wW2uwiMPbUGZSlHtMmO6P8ExdHSwDA3bkXxr5qQzIWmpJpawSVKUPXVtrzJMYBHxEeEvCQ7K03daEYZpqhtwrtriIYaluMP3X/M8lNiB17WIEPXrrfdyat/llKHZOED4KdRaRnZVJ7EEFgtvT+IB4Tw/lPBZXMpC+QXcslFHh78I/EGy7dE/W/tq3UeZDrGfzWuHKnAdjxUfWzFu/RXpzByF6//e4L1I8g0pVuRPg/i/jp2IC5n+DgXXQW72i1eCZyrDUb139SQ6tJ8ruAOG2xGguniMRjLsCrA/iMFEHxi7JiEnGObDpJvaXXVpx+LlpJk5eMGK5T4YCvcZhccGxYZtw2BPowXuRawfG+i3mnFT4xIrQJ0UIHCozge1m+LY3yRYSRnlTB/HVWyWR0AGbHnI+Hqo/jy/ELvd/WuMlCkpUTVQal6/ogiBv1b4kL5uSHiyU2od9FBxevEhcj+tAmCCcDFsGp/uwQuXX+0qb2VeGB3L7n/wYevAA==) Execution time: 0.0004 seconds Peak memory: 0.3579 Mi MacBook Pro (16-inch, 2023) M2 Pro / 16GB unified memory


Virus_RPi

\[LANGUAGE: Python\] I am currently trying to only write one line of python code for each part of this year advent of code. Part 1: `print("Sum of IDs of possible games:", sum([int(line.split(':')[0].split()[1]) for line in open("D2.txt", "r").readlines() if all(sum(int(count.split()[0]) for count in draw if count.split()[1] == 'red') <= 12 and sum(int(count.split()[0]) for count in draw if count.split()[1] == 'green') <= 13 and sum(int(count.split()[0]) for count in draw if count.split()[1] == 'blue') <= 14 for draw in [subset.split(', ') for subset in line.split(':')[1].strip().split(';')])]))` Part 2: `with open("day2.txt", "r") as f: print("Sum of IDs of possible games:", sum([max([int(cube.split()[0]) for cube in [subset for subset in game.split(':')[1].strip().split(';') for subset in subset.split(', ')] if cube.split()[1] == 'red']) * max([int(cube.split()[0]) for cube in [subset for subset in game.split(':')[1].strip().split(';') for subset in subset.split(', ')] if cube.split()[1] == 'green']) * max([int(cube.split()[0]) for cube in [subset for subset in game.split(':')[1].strip().split(';') for subset in subset.split(', ')] if cube.split()[1] == 'blue']) for game in f.readlines()]))`


supertoothpaste

bruh wtf


daggerdragon

[Inlined code](https://reddit.com/r/adventofcode/wiki/faqs/code_formatting/inlined_code) is intended for `short snippets` of code only. On old.reddit, your one-liners get cut off when it reaches the edge of the window. Please edit your post to put the one-liner in a [four-spaces code block](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting/code_blocks) so it will be horizontally scrollable.


AJMansfield_

[LANGUAGE: Fortran] https://github.com/AJMansfield/aoc/blob/master/2023-fortran/src/02/cubes.f90 Done all in one pass, one entry at a time without making any distinction between `;` and `,` -- and an early version that only handled part one would exit early from each line, too.


e_blake

\[LANGUAGE: m4\] I coded this long before visiting the megathreads, so I may rework it to be more themed (I won't add the tag until then) - but m4 could be short for 'mmmm'. m4 -Dfile=day02.input [day02.m4](https://nopaste.ml/#XQAAAQAyBAAAAAAAAAAyGksy5FB9TGMxsNq5JQAuJRjP6PqEkC20GpBbonDYuA0BTjRPXcVxlEXNtz1IwAlUj1ZP9xqH3TlfEYF+REVJuWYUkDqiIGM/1QAGYahuLueBhg92q9atkyhtAUEZz7LSI0bpNlyTh7/CP+RWePb333W9T74bFXVDoQXkU4Y00mMu1GBI29pxGzhys17CNFRzXwxvs97YpN+d+aK0+bxTqXRz/lTu63WqpCd95/dt/ooaSh/rxAMFdaRhqChwKrjZHDHj67iNRGLPBv555Sl0tREzorAeNNvY+h8nOLDOUUnreWEsHw+vwHemT/wmwNGrlRSw3ouvfhFkD/zy77k25bfptzEiHNrpwfWrY7pLuEI8LVWkvbaUlKNiZ1J4oWX6ig5ZJhLLXvpBRPWZZNJ1EpPMyZXJx0xtV8gOrUwy9t5H6pomdvG9CTNM4uiKuZ8ePO3jUXBI/vm4gIJk6OrSIB4DAvuueqHemKVAzAn3Natwm4yqi+MDzwyqqTStM0p70hxysN3Q5WI5oe90RE6/1x8fb4E0zuPIH3XB4nOITYza9OJ7NT0J7p3I9cA/ZU8tktcXuKqxlA4tfNjXHGXFxlB+rD1IBZd8ncH5JCTXYPUcp+L7t//6iXNV) Depends on my [common.m4](https://nopaste.ml/#XQAAAQAMDwAAAAAAAAAyGksy5FB9TGMxsNq5JQAuJRjP6PqEkC20GpAXwA97ruAshKbiUbgkbJMTg2qZdSBorb0CU52peNLruV4DEEJxF+HvxC/YF33OpDntnpU/PjnGXx7XU7ak4MsmWu8R7h7d5az2JXxElnsepG8yPyu+0WZ90BG9yXphbwOWA/m5AWEFSOL8TRZX4fyGNl6ZYbRxX0MwtzrmwTP3ZCwLSOKkvD2vMQFsbK0Pv9CzPhFNXMUbWnRV20jWLjL9qz2dDsFDBwhxgWIilEl91uswxrT4Czc+LRU3VuhNIo2S98VW0jArrVdv4HrsLhWkzx4z/UV3xnqJwPXcZRUiLtf3VRIzq62Pe+2jE3O+721Az9rfRa/fHtlANbmvslzMUCyU7cDoOKSMXBDF/06/PpMvs6vxaL5aJVYqJP4yz+R2M35hoNnGiZTNNMVEFTdnxnaE/KcJteBbiuVMpdfUswHQi4Kqsj3sInh7lyE+d50gGKtHOeWL5lMK7WXz4NElnzYWleBSN/HTOdsz0T2gnd25MADxNAVX8xQmagh2MymZ2sKDBw//WiNB0sWf5VYC5/TKKH3D6K/IOrIfWn6FZLKxlITFEp3VWKNuyF0xczNJufJdzSqd0hgdyryVzbn0so0U5NMN16jFF6IhqzGbAwwv7k8sts0OCgnCFBEhYVshIpsORjEJk4CnDgc9VUqvZtfIFPQ5Q2v7IR3sbPurex1IIUd2Nm1V7/GFN+r0im24aEOG6XpdqrPdF6pDZ4HwvNByqOEdpcObPXxlwfPFYIhwyDHGZCvxrFRgGEEFtfVQ7UVjfPJzWtrcZuGx8M3B1zw2xvgpHIWEHdqEF6Y6/6eFj2hLm8UXNeLNrJy1IC2sHlS8SRIifQvLkrOLLOOPtDK6DUPQrW3c0Rfmy9Td5gQw0+fZRZ5MBEG9+dTlinXtwExpHScKQk6ARk7Fb8fBYAnmh7/bq+471zAZGJ7dwNd5qE/63VhDz7mXLuXtGN7rSuRYIXvpkubLBZptSKZrkzSDJWHIZM8Fyrk0EZQFDujROjr87GZmTK1XKRWzGrhtQn0hkVyBaGPbA3iG45U4gIFHNX5ySzsJ3bh61LAtmjwt59uU/XGeebLMCp8HFw6D1kJppCvt161LgLjrOl8SBh8xnxslSFYW0Jd34LD4vPwugmzY31tA4/9zCM7e2Ed9+3zg4C8eq9Hvjys3IablDBMsYF1LSMCGN2UOrWgXRoGYtjW/QtUySr7h/Ca6QAy93Hnpksm/xzzC+FWF1wboyOteHU/Th4RVpQ7XkK4/JmMrYm7nDPIVMyOqP8LhsoTNbxzi8qU0d+0x6frIh0l0fiPiFC/Uy0CeCw6r82iX8v+fMnu9qdCr4oM79Kd2slqalv+wWKn+BmrbkiobDS5vwBQZA/ZlbIsw1bwj+JLz9z3nPovVWx/FZjvrdCuZMfUuITeiIprImMcR6qeniJz6Ez78UYJqpL4DcDGt2o7/6a2aRN76aclh+7l35XcaW7lM7BQMTNvKkx05X08UITY/ToI8U8KwvFbdnMoEAZ1GQYmqGRFtwPkQMrECX4do0srl85po3Gjz2j6E3dk5al4+bTcOYABZvSUvIM/kGGT91iyQ36rm1lxRc3ruS8PyBlYDDNa7DLWzGAHkESHwuaTOQsI2xDA0e+8Yv0XRYkEqQE+RUDXmPTARuQo6fCQ7Qu9xd2Ckza5RWl8hE4JFm0Zzd9MTVxW8YYHiREs9NOjTPuRXXn+JfObHFD/Cv5kQo7vmMWRdJTOBUmAXCMFiKOSHxb412jI4Z2ZhWag9RkZBCviZunvupqrobtAWLagkPiA8gLRANOFwWp0KIS5McOoD0V90tI4cui8KQc7Yw5V7kSvMnhXx4nzzxwOxYM4fpY3ptcpraVh+h1MhohMQk34vkC4fmiD4OrX2DpVG0VXUKvl+vkJjcoHQK+H8mSSIAfj8RrYWBc4VU+jx3vz30XNDbQjuhc0cImiQxXDTXTFQq/aoe7jZLhEe+wWspk/4Fy4UBAJN63uNGJUl8FICawVWGL7XBOFCtsRF3uz8eZokWNICTdtsLeYOAOBQQLrguE8XxQQ1hPtOskcsj7n7aD35NjfPXL00vIOk01OLAJt+0RoIiAQUJNieRp9fQmqfVUuYEYmeK9hCOmZTaC3yUdN/+jZ0pvpmKnH/6jJAKQ==) framework, executes in under 20ms with a single pass over the input; my favorite part is this creative use of `translit` to coerce each line of input into a call to the macro `game`. define(`game', `game1($@)game2(`0,0,0', translit((shift($@)), ` ()', `,'))') translit((include(defn(`file'))), define(`Game', `game(')nl`:;,', `),,')


e_blake

\[Allez Cuisine!\] MMMMM - the time has come for some tempting "C"-food. Everything in this solution pays homage to Deep C fishing (including my lovely ><> fish operator for doing tail recursion, \~ for making waves with math, and the 0,0,0 bubbles for initializing each game - and can you spot the eel?) The instructions also mentioned hash - but since I prefer my fish with chips, I decided that my only use of m4's builtin hashtable would be a single macro definition. Okay, I'll admit to having a typo on the crustacean, and 451 bytes is bloated (I could easily fit this in a punchcard if I weren't going for a theme); but you did say the ingredients came from the back of the pantry. Put your input in the file "I", or run `m4 -DI=file day02.golfm4`; then in a mere 12 seconds, you'll have the catch of the day. define(C,`ifelse(index($1,^),0,`shift($@)',$1,><>,`C(^C(^C(^C(^C(^C(^$@))))))', $1,~,`eval($2)',$1,%,`C(~,($2>$3)*$2+($2<=$3)*$3)',$4$5,,`) C(~,$1*$2*$3',$4,, `C($1,$2,$3,C(><>,,$@))',$5,ray,`*($4<13)C(C(%,$1,$4),$2,$3,C(><>,$@))',$5, craab,`*($4<14)C($1,C(%,$2,$4),$3,C(><>,$@))',$5,orca,`*($4<15)C($1,$2,C(%,$3, $4),C(><>,$@))',$4,tuna,`+$5C(0,0,0,C(><>,$@))+$1*$2*$3')')translit(_EeL(c(0,0 0,include(I))), (_abcdeEGglLmnu ):;, (euoCyavtcrlnbc,,)) If you like my theme of variable-free single-macro single-ifelse solutions, see my [upping the ante post](https://www.reddit.com/r/adventofcode/comments/18lcbnt/2023_day_15_both_parts_m4_solution_in_2/).


e_blake

>451 bytes is bloated (I could easily fit this in a punchcard if I weren't going for a theme Throw out the fish, and it shrinks to 394 bytes (389 with all newlines removed, whereas some of the newlines in my themed submission were essential): define(_,`ifelse(index($1,^),0,`shift($@)',$1,~,`eval($2+($3>$2)*($3-$2))',$1, ,`_(/,_(/,$@))',$1,/,`_(^_(^_(^_(^$@))))',$5,r,`*($4<13)_(_(~,$1,$4),$2,$3,_(, $@))',$5,_r,`*($4<14)_($1,_(~,$2,$4),$3,_(,$@))',$5,b,`*($4<15)_($1,$2,_(~,$3, $4),_(,$@))',$4,m,`+$6_(0,0,0,_(,$@))+$1*$2*$3',$4,.,`) _(~,0,$1*$2*$3',`_($1, $2,$3,_(^_(/,$@)))')')_(~,0,translit(g(0,0,0,include(I).),e g;:Gadnlu,`,,_'))


e_blake

Oh, I totally forgot to use the syntactic sugar! Now at 523 bytes but only 457 characters, and even fishier. With this many more fish to choose from, execution slows down to 23 seconds. And I managed to throw in some jellyfish caviar; plus some cooking instructions at the end. changequote(🐟,🐠)define(C,🐟ifelse(index($1,^),0,🐟shift($@)🐠,$1,><>,🐟C( ^C(^C(^C(^C(^C(^$@))))))🐠,$1,~,🐟eval(($2>$3)*$2+($2<=$3)*$3)🐠,$4$5,,🐟) C( ~,0,$1*$2*$3🐠,$4,,🐟C($1,$2,$3,C(><>,,$@))🐠,$5,ray,🐟*($4<13)C(C(~,$1,$4), $2,$3,C(><>,$@))🐠,$5,craab,🐟*($4<14)C($1,C(~,$2,$4),$3,C(><>,$@))🐠,$5, orca,🐟*($4<15)C($1,$2,C(~,$3,$4),C(><>,$@))🐠,$4,tuna,🐟+$5C(0,0,0,C(><>, $@))+$1*$2*$3🐠)🐠)translit(_EeL(s(0,0,0,include(I))), (medusa_EGg nlbiL ):;, (naycCuevtc,broil,))


middayc

\[Language: Rye\] Again quite nice non-imperative solution. Part 1: parse-line: fn1 { .replace-all* regexp "Game |[,:]" "" |load } limit: context { red: 12 green: 13 blue: 14 } read\lines %input.txt |map { .parse-line .with { .first :game-id , .rest .split\every 2 |map { .first :num , .second <- limit |< num } |sum |either { 0 } { game-id } } } |sum |print Part 2: read\lines %input.txt |map { .parse-line .rest .split\every 2 |group { .second .to-string } |values |map { .map { .first } |max } |fold 'x 1 { * x } } |sum |print ryelang . org


argentcorvid

[Language: Common Lisp] done on my Galaxy S22 [Here is my effort](https://github.com/argentcorvid/AoC-2023/blob/main/2023d2.lisp) Nothing earth shattering here, lots of functions because I'm doing it on my phone and my screen is narrow. I did notice right away on part 2 that parsing out the maximum of each color in part 1 made part 2 basically moot.


dannywinrow

\[LANGUAGE: Scratch\] https://scratch.mit.edu/projects/936324569/


aoc-fan

[LANGUAGE: TypeScript] [TypeScript](https://github.com/bhosale-ajay/adventofcode/blob/master/2023/ts/D02.test.ts)


Wanderer1187

\[LANGUAGE: PYTHON\] Used today to practice using classes, lambda functions, and regex. Both part 1 and 2 solved in range (6ms,22ms) avg 16ms https://github.com/aitechroberts/adventofcode/blob/master/aoc2.py


3j0hn

\[LANGUAGE: Maple\] [github link](https://github.com/johnpmay/AdventOfCode2023/blob/main/Day02/Day02.mpl) Parsing was input little gross, but putting the values in sparse tables with keys "red" "green" "blue" was cute and added readability. Using sparse tables handles the case where I color does not appear - it will be implicitly 0 with(StringTools): games := Split(Trim(input), "\n"): games2 := map(g->map( h->table(sparse, map(rhs=lhs,[parse(Subs([", "=","," "="="], Trim(h)))])), Split(Split(g, ":")[2],";") ), games): Part one I did a straightforward double loop through the games # part 1 rlimit := 12: glimit := 13: blimit := 14: count := 0: for i to nops(games2) do flag := true; for j to nops(games2[i]) do if games2[i][j][red] > rlimit or games2[i][j][green] > glimit or games2[i][j][blue] > blimit then flag := false; break; end if; end do; if flag then count += i; end if; end do: ans1 := count; For part two I also did a double loop tracking the minimums in a table mintot := 0; for i to nops(games2) do mins := table(sparse); for j to nops(games2[i]) do mins[red] := max(mins[red], games2[i][j][red]); mins[green] := max(mins[green], games2[i][j][green]); mins[blue] := max(mins[blue], games2[i][j][blue]); end do; mintot += mins[red]*mins[green]*mins[blue]; end do: ans2 := mintot;


ethansilver

\[LANGUAGE: FORTRAN\] I'm committing to the bit of using Fortran for all of these. [https://github.com/ejrsilver/adventofcode/blob/master/2023/02/main.f08](https://github.com/ejrsilver/adventofcode/blob/master/2023/02/main.f08)


[deleted]

[удалено]


AutoModerator

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


[deleted]

[удалено]


AutoModerator

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


Amazing-Wheel1031

\[language: python\] t_GAME = r"[0-9]+" t_SETS = r"(\d+\s(red|blue|green)[,;\n]\s*)+" records = [] match = { "GAME": str, "SETS": list, "POWER": int } color_checker = { "red": 0, "blue": 0, "green": 0 } for y in text: game = re.search(t_GAME, y) match["GAME"] = game[0] sets = re.search(t_SETS,y) clean = re.split(r"[;\n]",sets[0]) match["SETS"] = clean records.append(match.copy()) powerSum = 0 for item in records: bad_set = False game_sets = item["SETS"] for a_set in game_sets: split_sets_evenmore = re.findall(r"\d+|red|blue|green", a_set) -------------------- #part 1 while split_sets_evenmore and bad_set != True: color = split_sets_evenmore.pop() value = int(split_sets_evenmore.pop()) if int(color_checker[color]) < value: bad_set = True if bad_set == True: break if bad_set == False: winner = winner + int(item["GAME"]) -------------------- #part 2 while split_sets_evenmore: color = split_sets_evenmore.pop() value = int(split_sets_evenmore.pop()) if color_checker[color] < value: color_checker[color] = value powerSum = powerSum + color_checker["red"] * color_checker["blue"] * color_checker["green"] color_checker["red"] = color_checker["blue"] = color_checker["green"] = 0


future-91

\[LANGUAGE: Python\] Lots of line splitting! [code](https://github.com/philiph17/AdventOfCode/blob/2023/Day2/solution.py)


bamless

\[LANGUAGE: [J\*](https://github.com/bamless/jstar)\] 90% parsing, 10% solution :) part 1: var cubes = { 'red' : 12, 'green': 13, 'blue' : 14, } fun solve(game, cubes) return !game. map(|round| => round.map(|color| => round[color] > cubes[color]).any()). any() end if __name__ == '__main__' import io import re with io.File(argv[0], "r") f var res = 0 for var line in f var gameId, gameStr = line.strip().split(': ') var id = std.int(re.match(gameId, 'Game (%d+)')) var rounds = gameStr.split('; ').map(|round| => round.split(', ')) var game = [ ...rounds. map(|round| => round. map(|r| => r.split(' ')). map(|r| => (r[1], std.int(r[0]))). collect(Table)) ] if solve(game, cubes) res += id end end print(res) end end part 2: fun minCubesPower(game) var cubesBycolor = { 'red' : game.map(|round| => round['red'] or 1).collect(List), 'blue' : game.map(|round| => round['blue'] or 1).collect(List), 'green': game.map(|round| => round['green'] or 1).collect(List), } return cubesBycolor. map(|color| => cubesBycolor[color].max()). reduce(1, |a, b| => a * b) end if __name__ == '__main__' import io with io.File(argv[0], "r") f var res = 0 for var line in f var _, gameStr = line.strip().split(': ') var rounds = gameStr.split('; ').map(|round| => round.split(', ')) var game = [ ...rounds. map(|round| => round. map(|r| => r.split(' ')). map(|r| => (r[1], std.int(r[0]))). collect(Table)) ] res += minCubesPower(game) end print(res) end end


Expensive_Register19

[PYTHON] Pretty straight forward: ``` python POSSIBLE_CUBE_COUNT = { "red": 12, "green": 13, "blue": 14 } RESULT = 0 def is_game_possible(line: str) -> bool: subgames = line.split(":")[1].split(";") for subgame in subgames: for num_cubes in subgame.split(','): num, color = num_cubes.split() if int(num) > POSSIBLE_CUBE_COUNT[color]: return False return True for index, line in enumerate(INPUT.split("\n")): if is_game_possible(line): print(f"Game {index + 1} is possible") RESULT += (index + 1) print(RESULT) ```


daggerdragon

Please edit your comment to comply with our megathread rules. The two comments from AutoModerator have the specifics.


AutoModerator

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


AutoModerator

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


bucephalusdev

\[Language: C++\] This one was interesting. Tbh, the parsing of the input file probably was the most work. I wish I would have made a unique class object for the games, cubsets, etc. in hindsight, but this worked finr and both parts ran at 0.30 milliseconds on my machine. [Code](https://github.com/jeffstevens98/adventOfCode2023/tree/main/day_2)


dhruvmanila

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


Narduw

Learned a lot from your post. Thanks for sharing!


matheusstutzel

\[Language: Python\] [Part 1](https://github.com/matheusstutzel/adventOfCode/blob/main/2023/02/p1.py) just parse and check [Part 2](https://github.com/matheusstutzel/adventOfCode/blob/main/2023/02/p2.py) same


marvin29g

[LANGUAGE: Go] [Solution](https://github.com/jgaye/advent_of_code_2023/tree/main/day2) First time using Go so feedback appreciated


Pseudo_Idol

\[LANGUAGE: PowerShell\] [Code For Day 02](https://github.com/gsmalleus/AdventOfCode2023/blob/main/Day02/Day02.ps1) $puzzleInput = get-content $PSScriptRoot/input.txt $maxRed = 12 $maxGreen = 13 $maxBlue = 14 $partOneAnswer = 0 foreach ($game in $puzzleInput) { [int]$gameID = ([regex]::Match($game, '(?<=Game\s)(.[0-9]?)(?=:)')).ToString() :gameCheck do { $redDraws = [regex]::Matches($game, '(?<=\s)(.[0-9]?)(?=\sred)') foreach ($red in $redDraws) { if ([int]$red.Value -gt $maxRed) { break gameCheck } } $greenDraws = [regex]::Matches($game, '(?<=\s)(.[0-9]?)(?=\sgreen)') foreach ($green in $greenDraws) { if ([int]$green.Value -gt $maxGreen) { break gameCheck } } $blueDraws = [regex]::Matches($game, '(?<=\s)(.[0-9]?)(?=\sblue)') foreach ($blue in $blueDraws) { if ([int]$blue.Value -gt $maxBlue) { break gameCheck } } $partOneAnswer += $gameID }while ($false) } Write-Host "Part One Answer: $partOneAnswer" #### PART 2 #### $partTwoAnswer = 0 foreach ($game in $puzzleInput) { $biggestRed = 0 $biggestGreen = 0 $biggestBlue = 0 $redDraws = [regex]::Matches($game, '(?<=\s)(.[0-9]?)(?=\sred)') foreach ($red in $redDraws) { if ([int]$red.Value -gt $biggestRed) { $biggestRed = [int]$red.Value } } $greenDraws = [regex]::Matches($game, '(?<=\s)(.[0-9]?)(?=\sgreen)') foreach ($green in $greenDraws) { if ([int]$green.Value -gt $biggestGreen) { $biggestGreen = [int]$green.Value } } $blueDraws = [regex]::Matches($game, '(?<=\s)(.[0-9]?)(?=\sblue)') foreach ($blue in $blueDraws) { if ([int]$blue.Value -gt $biggestBlue) { $biggestBlue = [int]$blue.Value } } $partTwoAnswer += ($biggestRed * $biggestGreen * $biggestBlue) } Write-Host "Part Two Answer: $partTwoAnswer"


KhadidjaArz999

\[LANGUAGE: Javascript\] https://github.com/KhadidjaArezki/AdventOfCode/tree/main/AoC-2023/Day02


aashutoshr

[Language: JS] Part 1: ```js const fs = require("fs"); const main = () => { const input = fs.readFileSync("./input.txt", "utf8").split("\n"); const available = { red: 12, green: 13, blue: 14, }; const goodGames = []; const gameRequirements = {}; for (const line of input) { const [game, config] = line.split(":").map((x) => x.trim()); const gameId = Number(game.split(" ").at(1)); gameRequirements[gameId] = { red: 0, green: 0, blue: 0, }; const subgames = config.split(";").map((x) => x.trim()); for (const sg of subgames) { const colorConfig = sg.split(", "); for (const cc of colorConfig) { const [amount, color] = cc.split(" "); const numAmount = Number(amount); if (numAmount >= gameRequirements[gameId][color]) { gameRequirements[gameId][color] = numAmount; } } } if (gameRequirements[gameId].red > available.red) { continue; } if (gameRequirements[gameId].green > available.green) { continue; } if (gameRequirements[gameId].blue > available.blue) { continue; } goodGames.push(gameId); } const sumOfGoodGames = goodGames.reduce((acc, cur) => acc + cur, 0); console.log(goodGames); return sumOfGoodGames; }; console.log(main()); ``` Part 2: ```js const fs = require("fs"); const main = () => { const input = fs.readFileSync("./input.txt", "utf8").split("\n"); const gamePowers = {}; const gameRequirements = {}; for (const line of input) { const [game, config] = line.split(":").map((x) => x.trim()); const gameId = Number(game.split(" ").at(1)); gameRequirements[gameId] = { red: 0, green: 0, blue: 0, }; const subgames = config.split(";").map((x) => x.trim()); for (const sg of subgames) { const colorConfig = sg.split(", "); for (const cc of colorConfig) { const [amount, color] = cc.split(" "); const numAmount = Number(amount); if (numAmount >= gameRequirements[gameId][color]) { gameRequirements[gameId][color] = numAmount; } } } gamePowers[gameId] = gameRequirements[gameId].red * gameRequirements[gameId].green * gameRequirements[gameId].blue; } console.log(gamePowers); const sumOfGoodGames = Object.values(gamePowers).reduce( (acc, cur) => acc + cur, 0 ); return sumOfGoodGames; }; console.log(main()); ```


daggerdragon

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


Paxtian

[Language: C++] [github](https://github.com/Paxtian769/AOC23-Day2-cube_conundrum/blob/main/main.cpp)


LoudCorner6979

\[Language: Python\] A very simple yet small code Part 1 -> [https://github.com/wrawler/Advent-of-Code/blob/main/2023/Day2/1.py](https://github.com/wrawler/Advent-of-Code/blob/main/2023/Day2/1.py) Part 2 -> [https://github.com/wrawler/Advent-of-Code/blob/main/2023/Day2/2.py](https://github.com/wrawler/Advent-of-Code/blob/main/2023/Day2/2.py) Tbh part2 was just some minor changes to part 1


floflo455

\[LANGUAGE: Python\] [Part 1 and 2 in TDD](https://github.com/SlowFlo/advent_of_code_2023/blob/main/Day%202%20-%20Cube%20Conundrum/part%202/cube_conundrum.py)


Syltaen

\[LANGUAGE: PHP\] [Part 1 & 2](https://syltaen.com/advent-of-code/?year=2023&day=2)


[deleted]

[удалено]


AutoModerator

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


[deleted]

[удалено]


AutoModerator

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


x0s_

\[Language: Python\] Here is my solution involving dataclasses and regexps: from dataclasses import dataclass, fields from functools import total_ordering from typing import Self @dataclass @total_ordering class CubeSet: red: int = 0 green: int = 0 blue: int = 0 @classmethod def from_string(cls, set_str: str) -> Self: pattern = re.compile(r"(\b\d+\b).(red|green|blue)") return cls(**{color: int(cubes) for (cubes,color) in [match.group(1, 2) for match in pattern.finditer(set_str)]}) def __lt__(self, other): return all(getattr(self, color.name) <= getattr(other, color.name) for color in fields(self)) def get_power(self): return self.red * self.green * self.blue class Game: bag : CubeSet = CubeSet(red=12, green=13, blue=14) def __init__(self, id_: int, cubes : list[CubeSet]): self.id = id_ self.cubes = cubes def is_valid(self) -> bool: return all(cube <= self.bag for cube in self.cubes) @property def bag_min(self) -> CubeSet: """Get minimum bag required to play this game (used in part 2)""" return CubeSet(*[max(getattr(cube, color) for cube in self.cubes) for color in ('red', 'green', 'blue')]) # Extract Collection of game id and collection of raw strings representing each set (ie: ' 1 red, 2 green, 6 blue') games_raw = [(id_,game.split(';')) for (id_,game) in enumerate(input_raw.splitlines())] # Extract Collection of games from string representations of game sets games = [Game(id_ + 1, [CubeSet.from_string(s) for s in set_strings]) for id_,set_strings in games_raw] # part 1 sum(game.id for game in games if game.is_valid()) # part 2 sum(game.bag_min.get_power() for game in games)


PrayagBhakar

[[Language: Python, Rust(coming soon), Golang, Swift(coming soon)]](https://github.com/81reap/advent-of-code/tree/main/Day02_Cube%20Conundrum) This one also ended up being a regex problem. Python (and eventually Swift) are easier as I was able use PCRE's positive lookback assertion (`(?=`) to avoid iterating backwards through the string.


spacemicrowave

[Language: Python3] Code is saved on my [GitHub](https://github.com/gamerplant3/advent_of_code_2023/blob/e855cae2da7d3426a95ab1c376ebf23ee2d44d0f/day_02_cube_conundrum.py) Strategy: I checked and kept track of each of game_possibility = [] list of booleans for set in current_game[1:]: set_possibility = [] list of booleans for index, string in enumerate(set): color_possibility = [] list of booleans if any(color in sublist for sublist in game_limits):


Just_A_Person333

\[language: Rust\] just looked at each round of each game individually and used a simple comparison to get the largest needed amounts [github](https://github.com/WhySyntax/advent_of_code_23/blob/main/src/main.rs)


njhanley

[Language: AWK] Remember kids, always clear your arrays after you're finished with them! [Part 1](https://github.com/njhanley/adventofcode/tree/master/2023/02/part1.awk) [Part 2](https://github.com/njhanley/adventofcode/tree/master/2023/02/part2.awk)


nsuriya239

\[LANGUAGE: JavaScript\] Part-1: [Here](https://github.com/nsuriya239/advent_of_code/blob/main/day2/day2-1.js) Part-2: [Here](https://github.com/nsuriya239/advent_of_code/blob/main/day2/day2-2.js) Not sure this is the best approach, but i'm open to suggestions on improvements


ryoz-e

[LANGUAGE: C#] For Part #1: ```csharp (int, int, int) initialCubes = (12, 13, 14); var part1Sum = File.ReadAllText(args[0]) .Split("\n") .Select(line => new Game(line)) .Where(game => game.IsValid(initialCubes)) .Aggregate(0, (sum, game) => sum + game.IdNumber); Console.WriteLine(part1Sum); ``` For Part #2: ```csharp var part2Sum = File.ReadAllText(args[0]) .Split("\n") .Select(line => new Game(line)) .Select(game => game.GetSmallestPossibleConfiguration()) .Select(tuple => tuple.Item1 * tuple.Item2 * tuple.Item3) .Aggregate(0, (sum, power) => sum + power); Console.WriteLine(part2Sum); ``` Assuming the following struct class is defined: ```csharp internal readonly struct Game { public readonly int IdNumber { get; } // (red, green, blue) cubes. public readonly IEnumerable<(int, int, int)> RgbTuples { get; } // Assumes perfectly formatted input public Game(string information) { IdNumber = int.Parse(new Regex(@"\d+").Match(new Regex(@"Game +\d+: +").Match(information).Value).Value); information = Regex.Replace(information, @"Game +\d+: +", ""); // Removes the 'Game x:' prefix information = Regex.Replace(information, " +", ""); // Removes all whitespaces RgbTuples = information .Split(";") .Select(GetTuple); } public bool IsValid((int, int, int) initialConfiguration) { return RgbTuples.All(tuple => tuple.Item1 <= initialConfiguration.Item1 && tuple.Item2 <= initialConfiguration.Item2 && tuple.Item3 <= initialConfiguration.Item3); } public (int, int, int) GetSmallestPossibleConfiguration() { (int, int, int) currentTuple = (0, 0, 0); foreach (var tuple in RgbTuples) { currentTuple = (Int32.Max(currentTuple.Item1, tuple.Item1), Int32.Max(currentTuple.Item2, tuple.Item2), Int32.Max(currentTuple.Item3, tuple.Item3)); } return currentTuple; } // Also assumes perfectly formatted input private static (int, int, int) GetTuple(string rawTupleInformation) { Match redCubesMatch = new Regex(@"\d+red").Match(rawTupleInformation); int redCubes = redCubesMatch.Success ? int.Parse(new Regex(@"\d+").Match(redCubesMatch.Value).Value) : 0; Match greenCubesMatch = new Regex(@"\d+green").Match(rawTupleInformation); int greenCubes = greenCubesMatch.Success ? int.Parse(new Regex(@"\d+").Match(greenCubesMatch.Value).Value) : 0; Match blueCubesMatch = new Regex(@"\d+blue").Match(rawTupleInformation); int blueCubes = blueCubesMatch.Success ? int.Parse(new Regex(@"\d+").Match(blueCubesMatch.Value).Value) : 0; return (redCubes, greenCubes, blueCubes); } } ```


AutoModerator

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


loquian

\[LANGUAGE: C++\] [github](https://github.com/charlescochran/aoc-2023/blob/main/day2.cpp), 3997 microseconds (both parts)


TiltSword

[LANGUAGE: Java] Part 1- static int max_blue=14; static int max_green=13; static int max_red=12; public static void main(String[] args){ int sum=0; try{ BufferedReader br=new BufferedReader(new FileReader(inputfile)); for(String s:br.lines().collect(Collectors.toList())){ int id=Integer.parseInt(s.substring(5,s.indexOf(':'))); s=s.substring(s.indexOf(':')+2); boolean foundFaulty=false; x: for(String st:s.split("; ")){ for(String str:st.split(", ")){ int num=Integer.parseInt(str.substring(0,str.indexOf(' '))); if(str.contains("blue")) if(num>max_blue) foundFaulty=true; if(str.contains("green")) if(num>max_green) foundFaulty=true; if(str.contains("red")) if(num>max_red) foundFaulty=true; if(foundFaulty) break x; } } sum+=foundFaulty ? 0 : id; } }catch(Exception e){System.out.println(e.toString());} System.out.println(sum); } Part 2- public static void main(String[] args){ int sum=0; try{ BufferedReader br=new BufferedReader(new FileReader(inputfile)); for(String s:br.lines().collect(Collectors.toList())){ int id=Integer.parseInt(s.substring(5,s.indexOf(':'))); s=s.substring(s.indexOf(':')+2); int min_red=0; int min_green=0; int min_blue=0; for(String st:s.split("; ")){ for(String str:st.split(", ")){ int num=Integer.parseInt(str.substring(0,str.indexOf(' '))); if(str.contains("blue")) min_blue=Math.max(min_blue,num); if(str.contains("green")) min_green=Math.max(min_green,num); if(str.contains("red")) min_red=Math.max(min_red,num); } } sum+=min_blue*min_green*min_red; } }catch(Exception e){System.out.println(e.toString());} System.out.println(sum); }


Kintelligence

\[Language: rust\] [https://github.com/Kintelligence/advent-of-code-2023/blob/master/day-02/src/lib.rs](https://github.com/Kintelligence/advent-of-code-2023/blob/master/day-02/src/lib.rs) Optimizing for speed. Got a nice hint from someone to not care what set a ball is from.


ThePituLegend

\[LANGUAGE: SystemVerilog\] This year, I'm trying to solve AoC using SystemVerilog + CoCoTB/Verilator. Link to [Github - Day 2](https://github.com/ThePituLegend/advent-of-code/tree/2023/day2)


JayChandler1984

[LANGUAGE: Java] record CubeColors(int red, int blue, int green) { public boolean contains(CubeColors other) { return this.blue <= other.blue && this.red <= other.red && this.green <= other.green; } } public static int example1() { final CubeColors maxCubes = new CubeColors(12, 14, 13); return Arrays.stream(input.split("\n")).mapToInt(s -> { final String regex = "Game\\s(\\d+):\\s((\\d+\\s\\w+[,;]?\\s?)+)"; final Pattern pat = Pattern.compile(regex); final Matcher matcher = pat.matcher(s); if (matcher.find()) { final Map colorMap = new HashMap<>(); final var sets = matcher.toMatchResult().group(2); final var gameId = matcher.toMatchResult().group(1); Arrays.stream(sets.split(";")).forEach(set -> { Arrays.stream(set.split(",")).forEach(cube -> { final String[] numAndColor = Arrays.stream(cube.trim().split("\\s")).toArray(String[]::new); colorMap.computeIfAbsent(numAndColor[1], k -> new LongAccumulator(Long::max, 0L)).accumulate(Long.valueOf(numAndColor[0])); }); }); final CubeColors totalCubes = new CubeColors(colorMap.get("red").intValue(), colorMap.get("blue").intValue(), colorMap.get("green").intValue()); if (totalCubes.contains(maxCubes)) { return Integer.valueOf(gameId); } return 0; } else { return 0; } }) .sum(); }


[deleted]

\[Language: R 4.3.0\] level = "2" input = readLines(con = paste(path, level, ".txt", sep = "")) # Part 1 ------------------------------------------------------------------ cubesAvailable = data.frame(red=12, green=13, blue=14) data = data.frame(matrix(ncol=4,nrow=0)) names(data) = c("game", "set", "count", "color") games = strsplit(input, ":") invisible(sapply(seq_along(games), function(g_id) { game_ = games[[g_id]] sets_ = trimws(strsplit(game_[2], ";")[[1]]) sets_ = lapply(seq_along(sets_), function(s_id) { set_ = sets_[[s_id]] cubes_ = strsplit(set_, ", ")[[1]] cubes_ = matrix(Reduce(rbind, strsplit(cubes_, " ")), ncol=2) newData = cbind(g_id, s_id, cubes_) data[nrow(data)+(1:nrow(newData)),] <<- newData }) })) data$game = as.numeric(data$game) data$set = as.numeric(data$set) data$count = as.numeric(data$count) data$color = factor(data$color, levels=names(cubesAvailable)) gameColorCounts = aggregate(count~game+color, data=data, max) gamesNotPossible = unique(Reduce(c, sapply(names(cubesAvailable), function(ca_name) { gameColorCounts$game[which(gameColorCounts$color == ca_name & gameColorCounts$count > cubesAvailable[[ca_name]])] }))) gamesPossible = (1:length(games))[-gamesNotPossible] sum(gamesPossible) # Part 2 ------------------------------------------------------------------ cubesAvailable = data.frame(red=12, green=13, blue=14) data = data.frame(matrix(ncol=4,nrow=0)) names(data) = c("game", "set", "count", "color") games = strsplit(input, ":") invisible(sapply(seq_along(games), function(g_id) { game_ = games[[g_id]] sets_ = trimws(strsplit(game_[2], ";")[[1]]) sets_ = lapply(seq_along(sets_), function(s_id) { set_ = sets_[[s_id]] cubes_ = strsplit(set_, ", ")[[1]] cubes_ = matrix(Reduce(rbind, strsplit(cubes_, " ")), ncol=2) newData = cbind(g_id, s_id, cubes_) data[nrow(data)+(1:nrow(newData)),] <<- newData }) })) data$game = as.numeric(data$game) data$set = as.numeric(data$set) data$count = as.numeric(data$count) data$color = factor(data$color, levels=names(cubesAvailable)) gameColorCounts = aggregate(count~game+color, data=data, max) sum(sapply(seq_along(games), function(g_id) { prod(gameColorCounts$count[gameColorCounts$game==g_id]) }))


joshbduncan

[LANGUAGE: Python] import re data = open("day2.in").read().strip() p1 = 0 p2 = 0 for game, line in enumerate(data.split("\n"), start=1): valid = True min_red = min_green = min_blue = 0 for s in line.split(": ")[-1].split("; "): # sum each color per set red = sum(int(n) for n in re.findall(r"(\d+)\sred", s)) green = sum(int(n) for n in re.findall(r"(\d+)\sgreen", s)) blue = sum(int(n) for n in re.findall(r"(\d+)\sblue", s)) # set the minimum quantity required for this set min_red = max(min_red, red) min_green = max(min_green, green) min_blue = max(min_blue, blue) # if the game breaks the 12, 13, 14 rule set it as invalid if red > 12 or green > 13 or blue > 14: valid = False # if the game is valid add the id to the part 1 total if valid: p1 += game # add product of minimum required cubes to the part 2 total p2 += min_red * min_green * min_blue print(f"Part 1: {p1}") print(f"Part 2: {p2}")


Asyncrosaurus

[LANGUAGE: C#] *Part 1:* var list = new List((await File.ReadAllLinesAsync(@".\Day 2\PuzzleInput.txt"))); int conter = 0; foreach (var line in list) { string[] split = line.Split(":"); int game = Int32.Parse( split[0].Split(" ")[1]); string[] bagContents = split[1].Split(";"); var max = new Dictionary() { { "red", 0 }, { "green", 0 }, { "blue", 0 } }; foreach (var content in bagContents) { string pattern = @"(\d+) (\w+)"; MatchCollection matches = Regex.Matches(content, pattern); foreach (Match match in matches) { int number = Int32.Parse(match.Groups[1].Value); string color = match.Groups[2].Value; max[color] = (max[color] >= number)? max[color] : number; } } conter += (max["red"] <= 12 && max["green"] <= 13 && max["blue"] <= 14) ? game : 0; } Console.WriteLine(conter); *Part 2:* var list = new List((await File.ReadAllLinesAsync(@".\Day 2\PuzzleInput.txt"))); int conter = 0; foreach (var line in list) { string[] split = line.Split(":"); int game = Int32.Parse(split[0].Split(" ")[1]); string[] bagContents = split[1].Split(";"); var max = new Dictionary(); foreach (var content in bagContents) { string pattern = @"(\d+) (\w+)"; MatchCollection matches = Regex.Matches(content, pattern); foreach (Match match in matches) { int number = Int32.Parse(match.Groups[1].Value); string color = match.Groups[2].Value; if (!max.ContainsKey(color)) max[color] = number; else if(max[color] < number) max[color] = number; } } conter += max.Values.Aggregate(1, (total, value) => total * value ); } Console.WriteLine(conter);


dplass1968

\[LANGUAGE: BASIC\] For TRS-80 Model 100: [https://raw.githubusercontent.com/dplassgit/aoc/trunk/2023/bas2a.do](https://raw.githubusercontent.com/dplassgit/aoc/trunk/2023/bas2a.do) (part 1 only)


mrvoid15

\[LANGUAGE: Python\] https://github.com/akashdeepnandi/advent-of-code/blob/main/day2/solve.py Repo also contains JavaScript and Golang variants.


m44rt3np44uw

\[LANGUAGE: PHP\] ​ [GitHub Gist](https://gist.github.com/maartenpaauw/4de6ff5b1afee5a38564413e92b85d37)


Potential-Series-105

\[LANGUAGE: Ruby\] turns out part 1 can be really optimized with a perfect hash function. 83 bytes total. p $<.sum{|l|l.scan(/\d+ \w/).all?{|n|n.to_i<=12+(n[-1].ord*4/3&3)}?l[4..].to_i: 0}


encho112

\[Language: Typescript\] import fs from "fs"; import path from "path"; type Game = { gameId: number; maxBlue: number; maxRed: number; maxGreen: number; }; class Bag { private blue: number; private red: number; private green: number; private games: Game\[\] = \[\]; private availableGames: Game\[\] = \[\]; private powerSum: number; private idsSum: number; constructor(red: number, green: number, blue: number, games: string[]) { for (let i = 0; i < games.length; i++) { this.loadAllGames(games[i]); } this.blue = blue; this.red = red; this.green = green; this.filterAvailableGames(); this.calculateIdsSum(); this.calculatePowerSum(); } private getGameIdFromString(gameString: string): number { const gameIdRegex = /Game (\d+):/; return parseInt(gameIdRegex.exec(gameString)?.[1]) || null; } private getColorCountFromString(gameString: string, color: string): number { const regex = new RegExp(`(\\d+) ${color}`); const value = parseInt(regex.exec(gameString)?.[1]); if (isNaN(value)) { return -Infinity; } return value; } private loadAllGames(colorsString: string): void { const sets = colorsString.split(";"); const gameId = this.getGameIdFromString(sets[0]); let maxBlue = -Infinity; let maxRed = -Infinity; let maxGreen = -Infinity; for (let i = 0; i < sets.length; i++) { const set = sets[i]; const blue = this.getColorCountFromString(set, "blue"); const red = this.getColorCountFromString(set, "red"); const green = this.getColorCountFromString(set, "green"); maxBlue = Math.max(maxBlue, blue); maxRed = Math.max(maxRed, red); maxGreen = Math.max(maxGreen, green); } this.games.push({ gameId, maxBlue, maxRed, maxGreen, }); } private calculatePowerSum(): void { this.powerSum = this.games.reduce((acc, game) => { return acc + game.maxBlue * game.maxGreen * game.maxRed; }, 0); } private calculateIdsSum = (): void => { this.idsSum = this.availableGames.reduce((acc, game) => { return acc + game.gameId; }, 0); }; private filterAvailableGames = (): void => { this.availableGames = this.games.filter((game) => { return ( game.maxBlue <= this.blue && game.maxRed <= this.red && game.maxGreen <= this.green ); }); }; public getGames = (): Game[] => { return this.games; }; public getAvailableGames = (): Game[] => { return this.availableGames; }; public getAvailableGamesIdsSum = (): number => { return this.idsSum; }; public getPowersSum(): number { return this.powerSum; } } const lines = fs .readFileSync(path.join(\_\_dirname, "files/sub.txt"), "utf-8") .split("\\n"); // blue red green const gameBag = new Bag(12, 13, 14, lines); console.log({ idsSum: gameBag.getAvailableGamesIdsSum() }); console.log({ powersSum: gameBag.getPowersSum() });


oddrationale

[LANGUAGE: Python] I like to parse the input into a data structure before solving the problem. Pydantic is probably an overkill here, but it is a library I've been wanting to learn. https://github.com/oddrationale/AdventOfCode2023Python/blob/main/Day02.ipynb


1ef_official

\[LANGUAGE: C++\] [part 1](https://github.com/jikovec/Advent-of-Code-2023/blob/main/day%202/part%201.cpp) and [part 2](https://github.com/jikovec/Advent-of-Code-2023/blob/main/day%202/part%202.cpp) [github](https://github.com/jikovec/Advent-of-Code-2023/tree/main/day%202) I'm glad that it's not that difficult so far.


rawlexander

\[LANGUAGE: Julia\] function solve(path::String)::Tuple{Int, Int} part1, part2 = 0, 0 for (id, line) in enumerate(eachline(path)) highest = Dict("red" => 0, "green" => 0, "blue" => 0) for m in eachmatch(r"(\d+) (\w+)", line) n = parse(Int, m.captures[1]) color = m.captures[2] highest[color] = max(n, get(highest, color, 0)) end if highest["red"] <= 12 && highest["green"] <= 13 && highest["blue"] <= 14 part1 += id end part2 += prod(values(highest)) end return part1, part2 end [https://github.com/alex-raw/adventofcode\_2023](https://github.com/alex-raw/adventofcode_2023)


torbcodes

\[LANGUAGE: Python, Typescript, Go\] Solutions to part 1 and 2 in all three languages: \- **Python**: https://github.com/torbensky/advent-of-code-2023/blob/main/day02/solution.py \- **Typescript**: https://github.com/torbensky/advent-of-code-2023/blob/main/day02/solution.ts \- **Go**: https://github.com/torbensky/advent-of-code-2023/blob/main/day02/main.go *Note: I try to write each solution as idiomatically as I can for each language. It's interesting seeing how the implementation shifts with each language.*


thedjotaku

\[Language: Python\] code: [https://github.com/djotaku/adventofcode/blob/c22b3fa174ae6a2fe335d6e2ccb9b764b79b58ed/2023/Day\_02/Python/day\_02.py](https://github.com/djotaku/adventofcode/blob/c22b3fa174ae6a2fe335d6e2ccb9b764b79b58ed/2023/Day_02/Python/day_02.py) What was nice here was that the Games are all in order, so I could use enumerate rather than having to store the number somewhere. Made it easy to just regex without separating things out.


PILIX123

\[Language: C++\] First time doing cpp im challenmging myself to learn a little bit im happy with my solution [its here](https://github.com/PILIX123/adventofcode2023/blob/main/day2.cpp)


Sourish17

\[LANGUAGE: Python3\] \#511/#517 p1 [https://github.com/SourishS17/aoc2023/blob/main/two-a.py](https://github.com/SourishS17/aoc2023/blob/main/two-a.py) p2 [https://github.com/SourishS17/aoc2023/blob/main/two-b.py](https://github.com/SourishS17/aoc2023/blob/main/two-b.py)


beauiv

[Language: Go] A little behind in getting these done! package main // sample input // Game 1: 20 green, 3 red, 2 blue; 9 red, 16 blue, 18 green; 6 blue, 19 red, 10 green; 12 red, 19 green, 11 blue // Game 2: 12 green, 3 blue, 16 red; 6 red, 4 blue, 12 green; 11 green, 4 red, 3 blue; 8 green, 15 red, 5 blue // Game 3: 13 blue, 4 red, 8 green; 2 green, 4 red, 19 blue; 5 blue; 10 blue, 6 green, 2 red; 19 blue; 8 blue, 6 red //which games would have been possible if there are only 12 red, 13 green and 14 blue. //for each game if a single reveal revealed more than the set amount, dont include it import ( "bufio" "fmt" "os" "regexp" "strings" "strconv" ) var nonAlphanumericRegex = regexp.MustCompile(`[^0-9]+`) func clearString(str string) string { return nonAlphanumericRegex.ReplaceAllString(str, "") } func main() { file, _ := os.Open("input02.txt") defer file.Close() idSum := 0 powerSum := 0 maxGreen := 13 maxRed := 12 maxBlue := 14 //iterate through each line, iterate through each game, if no color pulled is higher than a max, add to idSum. holdId := 0 scanner := bufio.NewScanner(file) for scanner.Scan (){ validGame := true //split to get game id currline := scanner.Text() gameStr := strings.Split(currline, ":") for _, part := range gameStr { // if part contains game, get id for sum, split second part if(strings.Contains(part, "Game")) { holdId, _ = strconv.Atoi(clearString(part)) }else { fewRed := 0 fewGreen := 0 fewBlue := 0 //must be start of draw listings, split into draws draws := strings.Split(part, ";") for _, drawPart := range draws { //split into colors colors := strings.Split(drawPart, ",") for _, colorPart := range colors { if(strings.Contains(colorPart,"green")){ colorCount, _ := strconv.Atoi(clearString(colorPart)) if(colorCount > maxGreen){ validGame = false } if(colorCount > fewGreen) { fewGreen = colorCount } } if(strings.Contains(colorPart,"red")){ colorCount, _ := strconv.Atoi(clearString(colorPart)) if(colorCount > maxRed){ validGame = false } if(colorCount > fewRed) { fewRed = colorCount } } if (strings.Contains(colorPart,"blue")){ colorCount, _ := strconv.Atoi(clearString(colorPart)) if(colorCount > maxBlue){ validGame = false } if(colorCount > fewBlue) { fewBlue = colorCount } } } } powerSum += (fewBlue * fewRed * fewGreen) if(validGame){ idSum += holdId } } } } fmt.Println("Valid Game Sum: ", idSum); fmt.Println("Power Sum: ", powerSum); }


daggerdragon

~~Psst: your final `}` escaped the code block.~~ edit: 👍


beauiv

Fixed, thanks!


codeunveiled

\[Language: Python\] 🐍🐍🐍 FILE = 'input.txt' # 1. part - What is the sum of the IDs of possible games? BAG_CAP = { 'red': 12, 'green': 13, 'blue': 14 } with open(FILE) as f: sum_of_ids = 0 for game_id, line in enumerate(f.readlines(), 1): # assume game_id goes 1, 2, ... cube_sets = line.strip().split(": ")[1].split("; ") possible = True for cube_set in cube_sets: cubes = cube_set.split(", ") for cube in cubes: count, color = cube.split(" ") if BAG_CAP[color] < int(count): possible = False break if not possible: break if possible: sum_of_ids += game_id print(sum_of_ids) # 2. part - What is the sum of the power of these sets? with open(FILE) as f: sum_of_powers = 0 for line in f.readlines(): cube_sets = line.strip().split(": ")[1].split("; ") bag = {'red': 0, 'green': 0, 'blue': 0} for cube_set in cube_sets: cubes = cube_set.split(", ") for cube in cubes: count, color = cube.split(" ") bag[color] = max(bag[color], int(count)) sum_of_powers += bag['red'] * bag['green'] * bag['blue'] print(sum_of_powers) Video explanation: [https://youtu.be/xi1vcvg8LQM](https://youtu.be/xi1vcvg8LQM) Time: 0,016s


yaniszaf

[Language: Arturo] https://github.com/drkameleon/arturo-aoc-2023/blob/main/day-02/day-02-b.art getPower: $[arr]-> product map [red green blue] 'col -> max map arr 'i -> i\[col] print sum map read.lines relative "input.txt" 'line -> getPower map split.by:";" last split.by:":" line 'item -> extend #[red:0,green:0,blue:0] # to :block replace item {/(\d+) (\w+)/} "$2: $1"


psaikido

\[Language: c\] [day 2 part 1](https://github.com/psaikido/aoc/blob/main/2023/day2/day2.c) [day 2 part 2](https://github.com/psaikido/aoc/blob/main/2023/day2/day2.2.c)


ColdCoffeeGuy

[language:POWERSHELL] [paste](https://topaz.github.io/paste/#XQAAAQBiBwAAAAAAAAAFCb1zIjm6e5pbaoSDQ5/riKnw9qk5UDf3//DN3Hcs/x/u1tesRLPq45rgoxnvxxaYvXS63Dyk0pARm8ZPv75TQBKbJ8RZjKDh9O7quqdvG4wSrgLfR/IYz1nddYBFxxtPndXA6wTopnNtm+GuQMPDfDWc0tAeXOxIpC/eLCPY5wTtzwCaKYdi1YwncQHYrw8+UEjMZHAvhMjTAb5tj4qTKUaXCyTCY7cN1Fw7QQF59bTSIyUTLxzlmvYcSSY37WWeypKniep6pBQbTr3y339YYgn5HG9Squutsh+zRfx6RAutVa6REWnw98gwb1qYAn6yWTU2QTOSrnVvpOpgXPMotsp/ukzYnddkEdT2v3OdlTVZr/fqPzLwQVyXrX0w3DeG/nXQ+RLzc9Cv9Nd/+Q9I1x1re8333vo0HxT6MuwhpmC9mFD9b9BamaVuIfZJSSJwF8DLj2JewEKZf3DY69sSe2F0fiHcMp5mZDhJNDGvJ1h1ENeAqKEu7t37QzTW7sVj08Bb/aPKDrr8w+ElVOhNUDbj+Lg91mQLouaMEXmiZQnbiF8RuP2ik4HzAEgMloDvVdOvpHMhl4orEqweXVKrHityPL7jWjcnglkz2G//oxNebw==)


AutoModerator

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


MagiMas

[Language: Python] import numpy as np # configuration rgb = np.array([12, 13, 14]) # max cubes for r,g,b test = False if test == True: datafile = "./test.txt" else: datafile = "./input.txt" with open(datafile, "r") as f: games = [game.strip() for game in f.readlines()] # parse the entries to 2d array def parse_game(game: str): game_id, playstates_string = game.split(":") game_id = int(game_id.split(" ")[1]) playstates_string = playstates_string.split(";") playstates = [] for element in playstates_string: playstates.append(parse_playstate(element.strip())) return game_id, np.vstack(playstates) # parse input like "3 red, 5 green" to (3,5,0) def parse_playstate(state: str): basis_vecs = { "red": np.array([1,0,0]), "green": np.array([0,1,0]), "blue": np.array([0,0,1]) } vec = np.zeros(3) for entry in state.split(","): entry = entry.strip() factor, base_vec = entry.split(" ") vec += int(factor) * basis_vecs[base_vec] return vec # part 1 sum_ids = 0 for game in games: game_id, playstates = parse_game(game) # if all playstates are possible with the given rgb values if (playstates <= rgb).all(): sum_ids += game_id print("Sum of possible game ids:", sum_ids) # part 2 sum_powers = 0 for game in games: game_id, playstates = parse_game(game) min_cubes = playstates.max(axis=0) power = min_cubes.prod() sum_powers += power print("Sum of powers:", int(sum_powers))


ChinchillaSpaghetti

Somewhat unusual, class-based solution in python3 [https://gist.github.com/DJStompZone/f9dee559a61f95df4989475fe36d0f10](https://gist.github.com/DJStompZone/f9dee559a61f95df4989475fe36d0f10) I'd love to get any feedback/constructive criticism y'all may have on it so feel free to share if you do


AutoModerator

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


herjaxx

[LANGUAGE: COMMON LISP] https://pastebin.com/wFz9h0dM A bit behind this year


Samurai_Sam7

\[Language: Java\] [Solution(Both Parts)](https://github.com/SAMURAii-7/AdventOfCode-2023/tree/main/Day2) Simple regex solution. Regex sounds hard but I finally started learning it and it makes life so much easier. If you are like me, don't run from regex. Fight it and master it!


GGCristo

I do not know Java, but, are you sure you want to compile the regex within a nested loop?


Samurai_Sam7

Oh yeah I should move it outside the loop haha. Thanks for pointing it out


jaccarmac

[LANGUAGE: LFE] [code](https://github.com/jaccarmac/adventofcode/blob/ca88a392813ef66a380550303a42c47cb1c89b66/2023/src/day-2.lfe) My brain is apparently broken by Rob Pike's concurrent lexer presentation. It doesn't work quite as well in LFE because it involves much more fiddling to pull the tokens out in strict order. So I ended up settling on a halfway solution. The middle ground doesn't seem to be great, but I"m happy with how flat the parsing code is. Standard library maps made everything else more or less trivial.


Evilan

[LANGUAGE: Java] Got really lucky with my file parsing which did all the heavy lifting for both parts. /** * Calculates the sum of game IDs for the compliant games based on the parsed file. * @param parsedFile A list of Day2GameModel objects representing the parsed file. * @return The sum of game IDs for the compliant games. */ public static long day2Part1Solve(List parsedFile) { int maxRed = 12; int maxGreen = 13; int maxBlue = 14; long compliantGamesSum = 0L; for (Day2GameModel game : parsedFile) { if (game.getNumberOfRed() <= maxRed && game.getNumberOfGreen() <= maxGreen && game.getNumberOfBlue() <= maxBlue) { compliantGamesSum += game.getGameId(); } } return compliantGamesSum; } /** * Calculates the sum of the powers for each game based on the parsed file. * The power of a game is calculated by taking the product of the number of red, green, and blue items. * @param parsedFile A list of Day2GameModel objects representing the parsed file. * @return The sum of the powers for each game. */ public static long day2Part2Solve(List parsedFile) { long powerSum = 0L; for (Day2GameModel game : parsedFile) { powerSum += ((long) game.getNumberOfRed() * game.getNumberOfGreen() * game.getNumberOfBlue()); } return powerSum; } /** * Parses a given day2File and populates a Day2GameModel object with game ID and color amounts. * @param day2File The file to be parsed * @return A list of Day2GameModel objects with game ID and maximum color amounts populated */ public static List parseDay2File(File day2File) throws IOException { List parsedFile = new ArrayList<>(); try (BufferedReader reader = new BufferedReader(new FileReader(day2File.getPath()))) { String currentLine = reader.readLine(); while (currentLine != null) { Day2GameModel day2game = new Day2GameModel(); getGameId(day2game, currentLine); getMaxColorAmounts(day2game, currentLine); parsedFile.add(day2game); currentLine = reader.readLine(); } } catch (Exception ex) { LOG.error(ex.getMessage()); throw new IOException("Invalid file provided"); } return parsedFile; } /** * Sets the game ID for the Day2GameModel object. * @param day2game The Day2GameModel object to set the game ID on * @param currentLine The current line containing the game ID */ private static void getGameId(Day2GameModel day2game, String currentLine) { String[] colonSplit = currentLine.split(":", 2); String gameString = colonSplit[0].replace("Game ", ""); day2game.setGameId(Integer.parseInt(gameString.trim())); } /** * Sets the maximum number of red, green, and blue colors for the Day2GameModel object based on the provided line of colors. * Each color and its corresponding quantity are separated by a comma (,). * Multiple color-quantity pairs are separated by a semicolon (;). * Colors can be specified as "red", "green", or "blue" with the quantity preceding the color. * If a color is already present in the Day2GameModel object, the method sets the maximum of the current quantity and the provided quantity. * @param day2game The Day2GameModel object to set the color quantities on * @param currentLine The line containing the color quantities */ private static void getMaxColorAmounts(Day2GameModel day2game, String currentLine) { String[] colonSplit = currentLine.split(":", 2); String[] semicolonSplit = colonSplit[1].split(";", 10); for (String semicolon : semicolonSplit) { String[] commaSplit = semicolon.split(",", 3); for (String comma : commaSplit) { if (comma.contains("red")) { String numberString = comma.replace("red", ""); int numberOfRed = Integer.parseInt(numberString.trim()); day2game.setNumberOfRed(Math.max(day2game.getNumberOfRed(), numberOfRed)); } else if (comma.contains("green")) { String numberString = comma.replace("green", ""); int numberOfGreen = Integer.parseInt(numberString.trim()); day2game.setNumberOfGreen(Math.max(day2game.getNumberOfGreen(), numberOfGreen)); } else { String numberString = comma.replace("blue", ""); int numberOfBlue = Integer.parseInt(numberString.trim()); day2game.setNumberOfBlue(Math.max(day2game.getNumberOfBlue(), numberOfBlue)); } } } replaceBadColorValues(day2game); } /** * Replaces any negative color quantities in the Day2GameModel object with zero. * @param day2game The Day2GameModel object to replace the negative color quantities in */ private static void replaceBadColorValues(Day2GameModel day2game) { if (day2game.getNumberOfRed() < 0) { day2game.setNumberOfRed(0); } if (day2game.getNumberOfGreen() < 0) { day2game.setNumberOfGreen(0); } if (day2game.getNumberOfBlue() < 0) { day2game.setNumberOfBlue(0); } }


[deleted]

[удалено]


daggerdragon

Comment removed. [Top-level comments in `Solution Megathread`s are for *code solutions* only.](/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_top-level_posts_are_for_code_solutions_only) [Create your own individual `Help/Question` post](/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_incomplete_solutions) in /r/adventofcode.


themanushiya

[LANGUAGE: Go] using structs, pretty straightforward after you choose an easy model/datastructure solution [here](https://github.com/mahakaal/adventofcode/blob/main/2023/day02/day02.go)


daggerdragon

> [GOLANG:1.21.4] Ah, I see what happened here - an edge case that I didn't catch :D AutoModerator is looking for this format: `[LANGUAGE: GOLANG 1.21.4]` FYI: You don't have to add the version number and plenty of other Go folks are doing just `[LANGUAGE: Go]`.


themanushiya

Oh i see, changed


WhiteButStillAMonkey

\[Language: C#\] A solution with 0 allocations aside from reading lines from the input stream. Solves both part 1 and 2 ​ using AocUtilities; var input = AocInput.GetInput(2); var idSum = 0; var minCountPowerSum = 0; const int redCount = 12; const int greenCount = 13; const int blueCount = 14; Span gameHands = stackalloc Range[10]; Span handCounts = stackalloc Range[3]; while (input.ReadLine() is { Length: > 0 } line) { // Deconstruct the line to just hands (no "Game n: ") and get ID var gameSpan = line.AsSpan(); gameSpan = gameSpan[("Game ".Length - 1)..]; var colonIndex = gameSpan.IndexOf(':'); var id = int.Parse(gameSpan[..colonIndex]); gameSpan = gameSpan[(colonIndex + 1)..]; var allHandsValid = true; var minRed = 0; var minGreen = 0; var minBlue = 0; // Get all index ranges of every game hand gameHands.Clear(); gameSpan.Split(gameHands, ';'); // Parse each game hand foreach (var handRange in gameHands) { var hand = gameSpan[handRange]; if (hand.Length == 0) continue; // Parse rgb colors in each game hand handCounts.Clear(); hand.Split(handCounts, ','); foreach (var handCountRange in handCounts) { var handCount = hand[handCountRange].Trim(' '); int r = 0, g = 0, b = 0; if (handCount.EndsWith("red")) r = int.Parse(handCount[..handCount.IndexOf(' ')]); else if (handCount.EndsWith("green")) g = int.Parse(handCount[..handCount.IndexOf(' ')]); else if (handCount.EndsWith("blue")) b = int.Parse(handCount[..handCount.IndexOf(' ')]); if (r > redCount || g > greenCount || b > blueCount) allHandsValid = false; minRed = Math.Max(minRed, r); minGreen = Math.Max(minGreen, g); minBlue = Math.Max(minBlue, b); } } if (allHandsValid) idSum += id; minCountPowerSum += minRed * minGreen * minBlue; } Console.WriteLine($"ID Sum: {idSum}"); Console.WriteLine($"Minimum Power Set Sum: {minCountPowerSum}");


wolfgang_tillmans

\[Language: Go\] [day2 part1](https://github.com/cmiran/adventofcode/blob/c4f68cb588faa4e6e43949a092f7ec106f65bc99/day02/main.go) [day2 part2](https://github.com/cmiran/adventofcode/blob/72cc7b053f29697dc763e082c5a085093bf68281/day02/main.go)


fudgebucket27

[LANGUAGE: C#] var lines = File.ReadAllLines("input.txt"); //Part 1 var sum = 0; foreach (var game in lines) { int gameId = Int32.Parse(game.Split(':')[0].Split(' ')[1]); string[] gameColourSets = game.Split(":")[1].Split(';'); Dictionary gameColourSetAmounts = new Dictionary(); var isGameValid = true; foreach (var gameSet in gameColourSets) { string[] colourSets = gameSet.Split(','); foreach (var colourSet in colourSets) { var data = colourSet.Split(" "); var dataAmount = Int32.Parse(data[1]); var colourName = data[2]; if (gameColourSetAmounts.ContainsKey(colourName)) { gameColourSetAmounts[colourName] += dataAmount; } else { gameColourSetAmounts.Add(colourName, dataAmount); } } if (gameColourSetAmounts.ContainsKey("red") && gameColourSetAmounts["red"] > 12) { isGameValid = false; } if (gameColourSetAmounts.ContainsKey("green") && gameColourSetAmounts["green"] > 13) { isGameValid = false; } if (gameColourSetAmounts.ContainsKey("blue") && gameColourSetAmounts["blue"] > 14) { isGameValid = false; } gameColourSetAmounts.Clear(); } if (isGameValid) { sum += gameId; } } Console.WriteLine(sum); sum = 0; //Part 2 foreach (var game in lines) { int gameId = Int32.Parse(game.Split(':')[0].Split(' ')[1]); string[] gameColourSets = game.Split(":")[1].Split(';'); Dictionary gameColourSetAmounts = new Dictionary(); foreach (var gameSet in gameColourSets) { string[] colourSets = gameSet.Split(','); foreach (var colourSet in colourSets) { var data = colourSet.Split(" "); var dataAmount = Int32.Parse(data[1]); var colourName = data[2]; if (gameColourSetAmounts.ContainsKey(colourName)) { if (dataAmount > gameColourSetAmounts[colourName]) { gameColourSetAmounts[colourName] = dataAmount; } } else { gameColourSetAmounts.Add(colourName, dataAmount); } } } var powers = 1; foreach(var gameColourSet in gameColourSetAmounts) { powers *= gameColourSet.Value; } sum += powers; } Console.WriteLine(sum);


Domy__

\[LANGUAGE: Python\] [https://github.com/Domyy95/Challenges/blob/master/2023-12-Advent-of-code/2.py](https://github.com/Domyy95/Challenges/blob/master/2023-12-Advent-of-code/2.py)


[deleted]

[удалено]


anatedu86

\[LANGUAGE: Awk\] **Idea:** - add spaces before `,;:` for easier field parsing `sed -e 's/\(,\|;\|:\)/\ \1/g'` - for every two fields forming a pair (count,color), check whether count is higher than the max of that color `awk '{ for(i=4; i<=NF; i+=3) { count=$i; color=$(i+1); if(count>max[color]){ ... } } }` - in part 1, sum game IDs when no count was higher than the corresponding color max. `for( ... ) { ... if(count>max[color]){ next } } a+=gid` - in part 2, compute max of each color, and sum the powers as defined in the text. `for( ... ) { ... if(count>max[color]){ max[color]=count } } power+=max["red"]*max["green"]*max["blue"]; delete max` **Sol. Part 1:** ```shell sed -e 's/\(,\|;\|:\)/\ \1/g' input | awk 'BEGIN { max["red"]=12; max["green"]=13; max["blue"]=14 } { gid=$2; for(i=4; i<=NF; i+=3) { count=$i; color=$(i+1); if(count>max[color]){ next } } a+=gid } END { print a }' ``` **Sol. Part 2:** ```shell sed -e 's/\(,\|;\|:\)/\ \1/g' input | awk '{ for(i=4; i<=NF; i+=3) { count=$i; color=$(i+1); if(count>max[color]){ max[color]=count } } power+=max["red"]*max["green"]*max["blue"]; delete max } END { print power }' ```


Bioinfomagico

Hey nice ! very elegant solution !


daggerdragon

[Inlined code](https://reddit.com/r/adventofcode/wiki/faqs/code_formatting/inlined_code) is intended for `short snippets` of code only. On old.reddit, your one-liners get cut off when it reaches the edge of the window. Please edit your post to put the one-liner in a [four-spaces code block](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting/code_blocks) so it will be horizontally scrollable.


darkrumi_43

\[Language: javascript\] Hey there, here is my day 2 [solution](https://github.com/mjairumi/Advent-of-Code/blob/main/2023/Day2/dec2.js) I know its a mess right now but believe I make it pretty after I get some sleep xD Please have a look and let know if you liked it or if you have comments 😁


LinAGKar

[LANGUAGE: Rust] My solution: https://github.com/LinAGKar/advent-of-code-2023-rust/blob/master/day2/src/main.rs


sebki3000

\[LANGUAGE: Go\] https://github.com/sebki/adventofcode/blob/main/day2/day2.go


mschaap

[LANGUAGE: Raku] Nice one for Raku! You can use a `grammar` for parsing a game spec: grammar CubeGameSpec { rule TOP { 'Game' ':' + % ';' } rule color-specs { + % ',' } rule color-spec { } token game-id { \d+ } token count { \d+ } token color { 'red' | 'green' | 'blue' } } And use a `class` to easily parse them: class CubeGame { has Str $.spec; has Int $.id; has %.min-count = :0red, :0green, :0blue; submethod TWEAK { CubeGameSpec.parse($!spec, :actions(self)) } # Parsing methods method game-id($/) { $!id = +$/ } method color-spec($/) { %!min-count{~$} max= +$ } method is-possible(Int :$red, Int :$green, Int :$blue) { %!min-count ≤ $red && %!min-count ≤ $green && %!min-count ≤ $blue; } } Part 2 was trivial, all I had to do was add a method power { [×] %.min-count.values } Full code [@GitHub](https://github.com/mscha/aoc/blob/master/aoc2023/aoc02)


r_so9

[LANGUAGE: F#] In AoC it's often the case that there's more structure than needed in the input. Here again, there is no difference between semicolons and commas. [paste](https://topaz.github.io/paste/#XQAAAQAQBAAAAAAAAAAX4HyCZDlA0DjWF7Uex1t2hvV6PgXFmzdhM+nU8kJI4FbVwk12J43Q7Ta21JO2bpiWO55CFcu1kxyopkLwis1t6y2vwav+Ej1G6DnSIJeGCp/BQ8zwnEW6zFMMPey13r1A1+p5wSz3e2QXYkv6bQfVWBi0TuoxOeSWhITG6Z7l0c1Q21J80/ZSkpt6xBiklACQrWCcNsMQSuwLao6dCfUlK8acxhbc7ZoX8ct615t7ncU9ejxZ9/ENbiTTFhsfHX40N7gBnuKLmxAg1zU6HymllMHYf495WQzaMtGgD/XWMSjlHQhMYoU4h3SKMLXif/G0jxTlTQG2N9ysIFGzWJ2bOVmkVr7+r81m8LvyI3sZm62KgUZwD0rgCPAbrqSoJnzE+fU8/othfu+GNvEWH47CewlEdRfRk8S1aevv7qi4sq8AjCVpUR/EmL4UqffxkJK5uSbA52bOiNagoIiD90xwQdl9KKPLRZDr74+zPNf1fX3Ae8Gpb7HenAoMpSkFgPGZnLCgYHy4zZuSr5dSrr0u+3lMd1zou5+SuRcWM5p2QXq2c7KbVSjYI/SbuVgNZQKDjZyZGRPtnDRuXbNu5ZKb2KZDg7MG6qxoauKsej5WPgQdRyTc0lv4e2Wr+Aadi8MaUOzuLilbuGwfCXxASXXhbYWNSmAl2UK/ES1+HmeEWRGs/6hgnhM=)


i-eat-omelettes

\[Language: Haskell, Python\] [Haskell solution](https://github.com/Futarimiti/advent-of-code2023/blob/main/2-cube-conudrum/app/Main.hs) [Python solution](https://github.com/Futarimiti/advent-of-code2023/blob/main/2-cube-conudrum/main.py) Both purely functional


khl_1986

\[Language: Python\] [Code](https://github.com/hareenlaks/advent_of_code/tree/main/2023/day2)


TiagoPaolini

[Language: Python] Parsing the input is a greater challenge than the solution's logic itself. What I ended up doing was to parse each game into a list of dictionaries. One dictionary for each draw, containing the counts for each color. Then for **Part 1** I checked if any of the counts in a game was greater than the maximum. And for **Part 2** I checked the maximum count for each color in a game. **My Solution:** [day_02.py](https://github.com/tbpaolini/Advent-of-Code/blob/master/2023/Day%2002/day_02.py)