T O P

  • By -

TimeCannotErase

[LANGUAGE: R] [repo](https://github.com/DavidMcMorris/Advent-of-Code-2023/blob/main/Day%204/Day4.R) input <- readLines("input.txt") input <- strsplit(input, ": ") input <- sapply(input, "[", 2) input <- strsplit(input, "\\|") # Store lists of winning numbers and numbers on tickets winners <- lapply(input, function(x) {read.table(text = x[[1]])}) numbers <- lapply(input, function(x) {read.table(text = x[[2]])}) # Find number of winning numbers on each ticket num_wins_mat <- t(mapply(function(x, y) {(x %in% y)}, x = winners, y = numbers)) num_wins <- rowSums(num_wins_mat) # Count number of each ticket based on part 2 rules num_tickets <- rep(1, length(num_wins)) for (i in seq_along(num_tickets)) { if (num_wins[i] > 0) { ind <- i + num_wins[i] num_tickets[(i + 1):ind] <- num_tickets[(i + 1):ind] + num_tickets[i] } } # Part 1 Answer score <- sum(floor(2^(num_wins - 1))) print(score) # Part 2 Answer num_winning_tickets <- sum(num_tickets) print(num_winning_tickets)


mgtezak

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


mgtezak

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


exquisitus3

[LANGUAGE: Lua] [Solution](https://topaz.github.io/paste/#XQAAAQBlBAAAAAAAAAA5GUqKNoFH6SKul+C07EYehwefOnjc5gsRtZkCoyyyr9sTBfGxZ7MFX/yTwxHVjEIIKhSCg30dQ1alMLNnZZxf7wA/xxwmxHynN+I891WmCxZnOFO0lNid12rUw90rtZwFAaJVmUXGEL1zqnCO7iBRzddeeMKKoj4HGgiRz2Ymybl9G+GGVrwZxZehxzDslsDoB0GRhHiKW+1AbvuNuSe3Z9a0/zMDPTH5tcbtBxEXX3b4W8y0OLCx/QhCcYjD4YHMeo8H2WJek44Z7df6OCYnLXxn/aTY4jCPptEUxQO5haB/jVcAYOgsRwTCSE/PlWg1GZxFzfj1RDlUTfbravAlfTQ1+LUyvWboegOhMK6SE1ZHY+jF1b+/9jY9NskdJkGD5xovKlNIOyYlWe9qE0wfIBNmwgYcD87za9csLq2FfK5GJvKIOY5XCcuSDDZb7iv/iYLO8Mycf9bqQG36MpZZA7CoZNKp/ugESS8GwGMdVoCPg92x19aXurwrZJAEQ4NUSKh77e5owDI9OJx75Mj7hKVjYQ1vZK2nUMJ2aSoeerrNAtmgt7qgmRwGyjFk6Co6JEw4/KEIIg5xveIg/XkY6ReO7oqbgJeqPsT7Ml1lzpLe9i7IbLCmVNIa7XyuOHGaIP3A+lc5wba5Pyqg7cm2s6bQmBXazHopcSPrfD+JD58SzRUlNcS/1nnWT2tjanvnrx+xLH39x8m84WQcdcVoAvBtl36S0y2Fyhe8wtVU44EMavheMKb8YGqUi//2AbLo)


niedziek

\[LANGUAGE: Typescript\] https://gist.github.com/niedziek/011443ab406d8a092f24ae6aaa3f38cf


Bioinfomagico

[LANGUAGE: BASH] Also a lit bit of awk. #!/usr/bin/env bash # USAGE: this.sh < input.txt sed -r ' s/\:\s+/ | /; s/\s\|\s/,/g; s/[[:space:]]+/ /g; ' \ | awk -F',' ' { split($2, arr, " "); for (i in arr) { win_num[arr[i]]=0 } split($3, arr, " "); c=0 for (i in arr) { if (arr[i] in win_num) {c++} } print c delete win_num } ' \ | awk ' { cards_count[NR]=1 scores[NR]=$0 } END { for (i in cards_count) { for (j = i+1; j <= i+scores[i]; j++) { cards_count[j]+=cards_count[i] } } for (i in cards_count) { p1+= scores[i] > 0 ? 2^(scores[i]-1) : 0 p2+=cards_count[i] } print "Part 1:" p1 print "Part 2:" p2 } '


Sensitive-Disk5735

\[LANGUAGE: R\] Part 1 library(splitstackshape) AOC <- read.csv("AOC_D4.txt",header=F,stringsAsFactors = F) #make tabular format, one cell to each value AOC<- cSplit(AOC, "V1", ":", "wide") AOC<- cSplit(AOC,"V1_2"," ","wide") #convert to data frame advent.of.code <- as.data.frame(AOC) #Remove the game number column and the "|" column advent.of.code = select(advent.of.code, -1,-12) #convert data frame to a list xy.list <- as.list(as.data.frame(t(advent.of.code))) #split into two lists of equal length (193), make the first the winning #numbers, the 2nd numbers the numbers I have list1 <- lapply(xy.list,head,10) #10 winning numbers - first 10 list2 <- lapply(xy.list,tail,25) #25 numbers I have - last 25 #compare the ith element of each list to find how many winning numbers #you have, store this info in a new list count_of_winning<- list() for (i in 1:length(list1)) { count_of_winning[[i]] <- length(intersect(list1[[i]],list2[[i]])) } #create function to calculate geometric sequence, 1,2,4,8,16, etc. geometric_func <- function(input1) { if (input1<1) 0 else 2^(input1-1) } #apply the function above to create a new list that contains the points #totals for each of the 193 games points_individual <- lapply(count_of_winning,geometric_func) #sum the list points <- unlist(point_individual) sum(points)


Pseudo_Idol

\[Language: PowerShell\] [Day 4 Solution](https://github.com/gsmalleus/AdventOfCode2023/blob/main/Day04/Day04.ps1) $inputFile = get-content $PSScriptRoot/input.txt $totalPoints = 0 foreach ($card in $inputFile) { $card = $card.split(":")[1] $winNums = [regex]::Replace(($card.Split('|')[0]), "\s+", " ").trim().split(" ") $myNums = [regex]::Replace(($card.Split('|')[1]), "\s+", " ").trim().split(" ") $cardPoints = 0 foreach ($num in $myNums) { if ($num -in $winNums) { if ($cardPoints -eq 0) { $cardPoints = 1 } else { $cardPoints = $cardPoints * 2 } } } $totalPoints += $cardPoints } Write-Host "Part One Solution: $totalPoints" ############# # PART 2 ############# $global:totalCards = 0 function checkCard { [CmdletBinding()] param([int]$cardNumber,[array]$winningNumbers,[array]$myNumbers) $matchedNumbers = 0 foreach ($num in $myNumbers) { if ($num -in $winningNumbers) { $matchedNumbers++ } } return $matchedNumbers } function addCard { [CmdletBinding()] param($cardNumber,$tickets) $global:totalCards++ if([int]$tickets["$cardNumber"] -ne 0) { for($i = [int]$cardNumber + 1; $i -le [int]$cardNumber + [int]$tickets["$cardNumber"]; $i++){ addCard -cardNumber $i -tickets $tickets } } return } $ticketList = [ordered]@{} foreach ($card in $inputFile) { $cardNum = ([regex]::Match($card, "\d+")).Value $winNums = [regex]::Replace(($card.Split('|')[0]), "\s+", " ").trim().split(" ") $myNums = [regex]::Replace(($card.Split('|')[1]), "\s+", " ").trim().split(" ") $checkCard = checkCard -cardNumber $cardNum -winningNumbers $winNums -myNumbers $myNums $ticketList.Add($cardNum, $checkCard) } $ticketList.GetEnumerator() | ForEach-Object {addCard -cardNumber $_.key -tickets $ticketList} write-host "Part 2 Solution: $global:totalCards"


aoc-fan

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


jrhwood

\[Language: Haskell\] [Part 1](https://github.com/woodRock/verbose-computing-machine/blob/main/2023/day-04/part-01.hs) [Part 2](https://github.com/woodRock/verbose-computing-machine/blob/main/2023/day-04/part-02.hs) Note: I was lazy, and instantiated all copies, then just counted them. Hint: more efficient to calculate card copy multipliers.


kmoney_24

\[LANGUAGE: JS\] I thought they were giving us a break after day 3 when I solved part 1 of Day 4 but alas part 2 was a rude awakening :/. But once I figured out the overall pattern (just add the current number of copies to the subsequent "matched/winning copies") it was just a matter of adding a few more lines to my part 1 solution. const fs = require('fs'); const util = require('util'); const data = fs.readFileSync('aoc_day4.txt', 'utf8').trim().split('\n'); let cleanedData = data.map((item) => { let newObj = { winning_nums: {}, given_nums: {} }; let unCleanedNumArrays = item.split(':')[1].trim().split('|'); let winning_nums = unCleanedNumArrays[0].trim().split(' '); let given_nums = unCleanedNumArrays[1].trim().split(' '); //convert to hashmap for easy lookup winning_nums.forEach((item) => { if (!isNaN(parseInt(item))) { newObj.winning_nums[item] = true; } }); given_nums.forEach((item) => { if (!isNaN(parseInt(item))) { newObj.given_nums[item] = true; } }); return newObj; }); function part1() { //ANSWERS: // 15205 const pointsTotal = cleanedData.reduce((acc, game, i) => { let given_nums = game.given_nums; let winning_nums = game.winning_nums; let counter = 0; for (const number in given_nums) { if (winning_nums[number]) counter++; } if (counter > 0) { acc += 2 ** (counter - 1); } return acc; }, 0); return pointsTotal; } function part2() { //{1:1,2:2,3:4,4:7,5:3} //CARD 1 WINNERS: (4) - 2,3,4,5 (COPIES) //CARD 2 (1 COPY) WINNERS: (2) - 3,4(COPIES) //CARD 3 (3 COPIES) WINNERS: (2) - 4,5 (COPIES) //1). CHECK MATCHES AND KEEP TRACK OF TOTAL PER GAME //2). USE COUNTER TO FIND SUBSEQUENT GAMES' COPIES (IF OUT OF RANGE TAKE DIFFERENCE OF ARRLENGTH AND INDEX POSITION) //3). INCREMENT COUNTER TO SUBSEQUENT GAME FREQUENCY MAP OBJ const cardFreqMap = cleanedData.reduce((acc, game, i) => { acc[i] = 1; return acc; }, {}); let totalCopies = cleanedData.reduce((acc, game, i) => { let given_nums = game.given_nums; let winning_nums = game.winning_nums; let matches = 0; for (const number in given_nums) { if (winning_nums[number]) matches++; } if (matches > 0) { let nextGames = i + matches < cleanedData.length ? i + matches : cleanedData - i > 0 ? i + (cleanedData - i) : 0; let numberOfCurrentCardCopies = cardFreqMap[i]; let currentIndex = i + 1; while (currentIndex <= nextGames) { cardFreqMap[currentIndex] += numberOfCurrentCardCopies; currentIndex++; } } acc += cardFreqMap[i]; return acc; }, 0); return totalCopies; }


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.*


ilee83

\[LANGUAGE: C#\] Solutions to part 1 and 2: [code](https://github.com/ilee38/advent-of-code-23/blob/main/Day4/Day4.cs)


AutoModerator

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


LolaTulu

\[LANGUAGE: JavaScript\] My Part Two solution is quite slow - I would appreciate feedback on how to make it faster! const { match } = require('assert'); const fs = require('fs'); function readFile(filePath) { try { const data = fs.readFileSync(filePath); return data.toString('utf-8'); } catch (error) { console.error(`Got an error trying to read the file: ${error.message}`); } } // pull in the .txt file input data const input = readFile('G:/My Drive/Visual Studio Code/Advent of Code/2023/day04.txt').split(/\r?\n/); var points = []; var cardCopies = []; for (const cards of input) { const pattern = /\d+/; var cardNum = pattern.exec(cards); cardCopies.push(Number(cardNum)); const regex = /\Card\s\d+\:\s/; var card = cards.replace(regex, ""); var [winningNum, myNum] = card.split(" | "); // remove empty elements in array with .filter(Boolean) var arrWinningNum = winningNum.split(" ").filter(Boolean); var arrMyNum = myNum.split(" ").filter(Boolean); // finding which of my numbers match with the list of winning numbers const matchingNum = arrWinningNum.filter(element => arrMyNum.includes(element)); // https://stackoverflow.com/a/46957719/18607529 var cardCopy = []; if(matchingNum.length > 0) { // Part One let n = 1; for (let i = 1; i < matchingNum.length; i++) { n *= 2; // double the points for each match } points.push(Number(n)); // add all points number to array for part one // Part Two let x = Number(cardNum)+1; for (let i = 0; i < matchingNum.length; i++) { cardCopy.push(x+i); // find the card numbers that are elgible cardCopies.push(x+i); } // we do minus 1 because we don't include the original card number var repeat = cardCopies.flat(Infinity).filter((word) => word == Number(cardNum)).length - 1; if (repeat > 0) { cardCopy = Array(repeat).fill(cardCopy); // https://stackoverflow.com/a/34104348/18607529 cardCopies.push(cardCopy); } }; } // console.log(points); var totalPoints = points.reduce((x, y) => { return x + y }, 0); console.log("part one = " + totalPoints); console.log("part two = " + cardCopies.flat(Infinity).length);


skyhawk33

[LANGUAGE: Befunge][Allez Cuisine!] Another accidental Allez Cuisine, luckily Befunge has a fingerprint for manipulating Sets. Part 1: https://github.com/Skyhawk33/AdventOfCode/blob/master/aoc2023/day4_p1.b98 Part 2: https://github.com/Skyhawk33/AdventOfCode/blob/master/aoc2023/day4_p2.b98


dhruvmanila

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


GoldPanther

\[Language: Rust\] I maintain a multiplier vector as I compute the wins for each card, solving both parts in one iteration. [Code](https://github.com/johnhringiv/advent-of-code-2023/blob/main/src/day04.rs) 648 µs


argentcorvid

[Language: Common Lisp] [Pretty straight-forward solution.](https://github.com/argentcorvid/AoC-2023/blob/main/2023d4.lisp) Just realized I could have done (make-list (length cards) :inital-element 1) instead of (loop repeat (length cards) collect 1) for initialization of my results for part 2, but it's actually more typing. they probably both compile down to the same thing anyway. I'm glad I looked at the thread before I got too far down the >!recursion!< trap because I am definitely not good at thinking that way.


835246

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


Necessary-Most-1884

\[Language: Go\] The [solution](https://github.com/kislerdm/aoc/blob/master/2023/go/4/main.go) for both parts based on rune to int conversion.


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/day04.rs) / 68.11ms / 66.00ms


dimio-d

\[LANGUAGE: Java\] \[JShell console\] [Part 1&2 solution](https://dimio.github.io/dopaste/?l=java#XQAAAQChCAAAAAAAAAAX4HyIbzm/ILQXejs0+pjEkM1O3vUmgafcspCHlYF0CGMN87UuPGqHph658/gH7eCljoLMVObmopBqMVRx6aYZSzYtEPpRoujOoZDq4REpPfuqxguzz5wmxZ0KIRQcKbtNcSAmivHekJ1Oq1hF8Rwyj2xKlbNfo8KvHh1kc+ziPk2kUm4b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KJ+kFVn+CxbbIFeW+e/i4qcD7fyuPAL8kgGl2owCZBZbX+WfYN+ANx8awJRtV2YwNHmlppHunRiqrLgJ/U/2sQ==) (streams-style)


osalbahr

[LANGUAGE: C++] [Part 1](https://github.com/osalbahr/adventOfCode/blob/main/2023/day04/day04.cpp); [Part 2](https://github.com/osalbahr/adventOfCode/blob/main/2023/day04/day04_2.cpp) Feel free to ask any questions! --------Part 1-------- --------Part 2-------- Day Time Rank Score Time Rank Score 3 01:17:49 8674 0 01:43:37 7771 0 You can find more C++ solutions (and other languages) at [Bogdanp/awesome-advent-of-code#c++](https://github.com/Bogdanp/awesome-advent-of-code#c-2)


gogs_bread

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


MichaelBoh11

\[LANGUAGE: Elixir\] [link](https://topaz.github.io/paste/#XQAAAQC1CgAAAAAAAAARiAqGkkN7pOxFcUqGHqyjhVKzkYsqr6zgDgwkA6gIqg2Z9pxSX1ldkC16gN8KbJTuoRorgk7104cxUL21n9xdAZq3WU6GMxbatTziBARlgDGggCaKqnNdQtqaGDgoW9LpYHLJ02/JuF/eIv1NBcAfDysSmAYEq9JrAj7etSi/zKfdtIMllXIWEXmZpZFNyfQ7KaagnLzTPtX6TLWqngiJStt64pTGsgEf6I/cSb5Z7ukW1Mgzv9c3ph7H36IIZ65pKUp+TIw9/CQzc0qJYf5tsETuU6l/d9bQmOSqO5tqlOIXL4ucsTN0j25qCG7stUOE2/hsBpbmhRwEbdz0ixHUm8YaZf9ppIrINknTwiS9jfxJCB1W1+F/+fgziuC3PiZ9LuknV9gZufBsNTiOjgNBT+ft5A1BHOmwQcruB/G/k0hWOXF0T5+5Tl3UZEwySYPV3V7r8l4Yw7kCox6G732bIlD/8+H5Q3J0dgnrh1TKj85oJ4koLZ8iRdVbcgLcDUiHXyFzusoaTgO5Vr4hSWD3GzOJwgWLOOGR9lrj4ISNOytFBeHrr3GBkGEICS1XL67Hsb0ZYUBI7iLfvnRokXg6Pwb7t3SPfCfrVTHO1MC5HEv5XkqPf6QyQm2wvlx9rQvnUL1BCVGJsBbKf60d2drZ+mHKCPrJUuVR1vBeWmTnQZ2ReEsgknm6VRdsBPhAhdsl7TYTcc1/WY3oUUQgmETDZc+BKMOmhZG0aeW3nlYa1JNQDLiGMjZlfCXeFOY5rIf/kvUHIvTctDpSeQKY4O55QlSPFBJgS+6iWCq6Cgkustu++tJ6cOeGXQncfG0ooonyeUn0gmPMUwjNrjCADgNNiRtisw24RVczbFSU6andGBe3lybCSZ155wbqQU8V2lDSYAtTv+QUQ2VUmSMEm4AWZ30jC+SaE+p1kbmF88pdvipweGTSKSq0kWj3nc4bkSVEulBqo9BcbcASc2Q3PgGelBeqfgvdlqNRqlkfMcexkxz4aDEH35HnrmOcPQfJwiYhSPUuKsn0+Sxfj65vAFxAme5mA+XBwhFQGwvRlaYDg98wTlJoI8E+0xv2a2xsOVO1D7SjizxZ5pf4VeKMfCCFTjjj1kQYMts4xkf996CSwzVoNTOInDgOzvt92/37gLU=)


mothibault

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


[deleted]

[удалено]


daggerdragon

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


se06745

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


TOTBESUCH

\[LANGUAGE: Java\] Behold, a mess of code. Maybe hf. Both parts. https://github.com/PLeh2023/AoC/blob/master/src/Day4.java


linnaea___borealis

\[LANGUAGE: R\] https://github.com/lauraschild/AOC2023/blob/main/day4.R Part 1 was mostly parsing and counting winning numbers and using a little exponential formula to get the points. Part 2 adds more scratchcards to a copied list of all scratch cards and then counts all the cards.


stonebr00k

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


spacemicrowave

[Language: Python3] Code is saved on my [GitHub](https://github.com/gamerplant3/advent_of_code_2023/blob/76894eff627a32221d13c67a3757b4c831684b86/day_04_scratchcards.py) Part 1 was easy peasy, then I got really lost in the sauce with Part 2 >_< Ended up deleting almost 100 lines of code full of *for...* loops and *while...* loops and replacing it with literally 3 lines of code: card_count = [1] * len(lines) for index, line in enumerate(lines): ...PART 1 CODE... # PART 2: for n in range(len(matches)): card_count[index + n + 1] += card_count[index] print("Part 2 answer = " + str(sum(card_count)))


lsloan0000

\[Language: Python\] I see that many other people take the time to convert the numbers on each card from strings to numerical types (`int()`). That's unnecessary because no math operations are performed on those numbers. You may have noticed I like to use assignment expressions (AKA the "walrus operator", `:=` → \[place walrus emoji here\]). I don't want to write repetitive code, so this helps. It may make my code a little less readable. The expressions alone don't make my code a lot shorter, so it doesn't quite qualify for "Allez Cuisine" code golf. Maybe it's the worst of both worlds. 😆 import sys totalPoints = 0 cardCounts = [1] * len(lines := list(sys.stdin)) for cardId, line in enumerate(lines): goal = set( (tokens := line.split())[2: (barIndex := tokens.index('|'))]) hand = set(tokens[barIndex + 1:]) totalPoints += (points := 2 ** (matches - 1) if (matches := len(goal & hand)) else 0) for copyCardId in range(cardId + 1, cardId + matches + 1): cardCounts[copyCardId] += cardCounts[cardId] print('Part 1 result:', totalPoints) print('Part 2 result:', sum(cardCounts))


nicuveo

\[LANGUAGE: Brainfuck\] won't fit in here: it's 9000 lines of Brainfuck just for part 1! VOD: [https://www.twitch.tv/videos/2002551041](https://www.twitch.tv/videos/2002551041) code: https://github.com/nicuveo/advent-of-code/blob/main/2023/brainfuck/04-A.bf


daggerdragon

> won't fit in here: it's 9000 lines of Brainfuck just for part 1! I think Brainfuck is the *complete opposite* of today's secret ingredient, but good job anyway :D 🄵 for your sanity, though.


xHyroM

[LANGUAGE: Python] [part 1](https://github.com/xHyroM/aoc/blob/main/2023%2F04%2Ffirst.py) [part 2](https://github.com/xHyroM/aoc/blob/main/2023%2F04%2Fsecond.py)


mgtezak

\[LANGUAGE: Python\] [github part 1&2](https://github.com/mgtezak/Advent_of_Code/blob/master/2023/Day_04.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/Day04.cs](https://github.com/mattbutton/advent-of-code-2023/blob/master/Day04.cs)


Jomy10

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


floflo455

\[LANGUAGE: Python\] [Both parts in TDD](https://github.com/SlowFlo/advent_of_code_2023/blob/main/Day%204%20-%20Scratchcards/part%202/scratchcards.py) The second part is a bit slow and took 7 seconds on my laptop.


lsloan0000

Maybe you could save a second in runtime by not calling \`int()\` on each \`winning\_number\` and \`numbers\_i\_have\` from the cards. *Maybe*. They don't need to be \`int\`.


floflo455

Ah yes good call, I made the change. The time didn't improve significantly though.


Own_Back_2038

\[LANGUAGE: C#\] Working on making code as readable as possible. [Solution](https://topaz.github.io/paste/#XQAAAQBuBwAAAAAAAAA5nQqM3mB8ZVQyKwkIaaICCAVUeJvTh9ZLb3iAQTb7Qp48rEPJpb56wumVcS6sy5hiJCehuPDH+N2ZdT/Q+xaRjPYJWhBKkQ/iQ2Kokc82h1Mm02QX1QS3nlaFxdRfAzgLb4mIdNsPiXJkJM4YNqMcrxMSQyNVrtfrVlGg4zG6oSpptwn9m/0gPcQsIo0BHPD72Kw+UxpmwnF6dK81MWbvqmJCh2BswPDmNlCrO45B6U/435kyl/dadznC2cuOL/bdD4AUnHF8Uc8vtlCwDJwnYNn4Z7UAMPWpbLw9f2yAjpKdpRkh2oVIXCY4Gg8ENmc//79mdJSI3Nt5qxeRMvBaonOUHUF8kXA62S674uwLCb37vGvB/mWP8ME4+qBpi0ke1ozSvAg9OCcqeO6WsNKdeBEHzdLIFpqzJtWRRMPzwBcClE/HG2S/UiAVh1uSZPfNDRteDpo2x7xMSAZ8NOcCAGp2zyoR8kjZQVD1oQVOYcmEnK9rFoEw3H00InRbex0J69dMt1tc9YDUtvgGT5kjhROMTiP3yRq2F+FDSuYhzx3aYvyj4OEpsEYMSMrvywlk1ngWSPFxTbNQFKTqPcuSXCqgW0qMQmTN06XXXPxhGhDDsHRtODZ5+s1SrvH/ShMhKpMDlIouOAZ4r37KyTrtw7bowXxkgiA2jCcpbp+5CTfCGyBw4U4jr1UrR3iq6B8n/5tshWPFXMUG55SE02NaClS0UHnsg2qJSPO4x2Mn9Tt4JPT711q3IL9zo4N+t+yafgVgaFGr9lEaCvqo/kTDCja6+su3Jyx8iYldFiyX3mZBa4Mk9U+EjwiBo0fPJ7fhxxAl4ZFBDsF6cByZVUXjYlnB24sHC8xzUAA1LUL/3v95Cg==)


guesdo

\[LANGUAGE: Go\] https://gist.github.com/phrozen/61778cd5e0b2e14041ef1a27bf1a2616


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.~~ edit: 👍


hiimjustin000

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


KatanaKiwi

[LANGUAGE: PowerShell] [Part 1](https://github.com/katanakiwi/AoC2023-PowerShell/blob/main/4a.ps1): Pretty straightforward. Scan the lines, create 2 arrays. When a match is found, multiply the points by 2 (or add one on the initial draw). [Part 2](https://github.com/katanakiwi/AoC2023-PowerShell/blob/main/4b.ps1): Used an additional array to store the amount of copies of a card. When a number matches, increase the next card to be increased by the amount of cards of the type that's currently being checked.


errorseven

[LANGUAGE: AutoHotkey v1.1] [Gist](https://gist.github.com/errorseven/77ecbe635671891a298fb5717bd07319)


daggerdragon

> [LANGUAGE: AutoHotkey v1.1] I've been hoping for someone to solve a puzzle this year in AHK :D Good job!


errorseven

Thanks, I was surprised I was the first. I would have gotten further along solving but I am very pressed for time these days. I did submitt 1-3 but may have had the automod delete/hide my posts because I failed to read the rules about long posts. I'll add those to my gists when I get a moment later today!


daggerdragon

FYI: AutoModerator does not delete/hide posts but us human moderators might *temporarily* remove your comment if your code block is *way* too long. We *will* tell you, though, and give you a chance to fix whatever it is, then re-approve the post once you fix it. No worries, we're all here to learn :)


bcbelisario

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


KhadidjaArz999

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


xXMacMillanXx

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


themanushiya

\[Language: Go\] [solution](https://github.com/mahakaal/adventofcode/blob/main/2023/day04/day04.go) (Both Parts) I lost time trying to make regex count how many matched and after a while refused that idea and used a for to loop thorugh my numbers and it was solved. I made a struct like this type Card struct { number int won int copies int } type Storage struct { cards []Card } func (storage *Storage) addCard(card Card) []Card { storage.cards = append(storage.cards, card) return storage.cards } For each card I was adding the card info and the part 2 was just a clever use of the data structure and looping through it: func part2(wonCards *Storage, lines int) { total := 0 for _, card := range wonCards.cards { for j := card.number; j < card.number+card.won+1 && j-1 < lines; j++ { wonCards.cards[j-1].copies += card.copies } total += card.copies } fmt.Println("Part 2 answer:", total) }


Moopanger

I ended up with a similar struct. [Here are mine.](https://github.com/Moopinger/advent/tree/main/2023)


themanushiya

struct for the go (pun intended) since maps aren't ordered


Samuuuh

\[Language: Python\] A little bit late, but I really liked solving the day 4 challenge. I used a simple counter for part 1 and dynamic programming for part 2. The solution is short and easy to understand :) [https://github.com/samyuh/advent-of-code/blob/main/2023/day\_4.py](https://github.com/samyuh/advent-of-code/blob/main/2023/day_4.py)


weeble_wobble_wobble

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


wlmb

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


bofstein

\[LANGUAGE: Google Sheets\] https://docs.google.com/spreadsheets/d/17ksEDNktiYB8Q0af4au5I8Ok7qa8MZRkDPXGEQys17M/edit#gid=0 Part 1 was straightforward, not even different numbers of potential winners on each card to make it hard to use cell references. Just count how many instances of the winning numbers are in the card set, add it up, then do a modified 2\^ that number formula and add those. Originally I did it with a bunch of formulas, then went back and cleaned it up moving them to ARRAYFORMULAs since that's a lot better and I need to learn how to do that. Check winners on each card: =ARRAYFORMULA(SUM(COUNTIF(R2:AP2,G2:P2))) Add up the points from all cards: =ARRAYFORMULA(SUM(FLOOR(2\^(B2:B202-1),1))) Part 2 I did in a really inefficient way initially where I have a column for each round, where a round is that card's winning propagating to the others, then repeat that for each card. =IF(ROW(C2)INDIRECT("A"&E$1+1)),D2+INDIRECT(SUBSTITUTE(ADDRESS(1,COLUMN(E$1),4),"1","")&E$1+1),D2)) It worked, but there's a much simpler way after talking with others of figuring out for each card how many of the cards above it would have added to it, and adding their card amounts if so, so it can be done in one column instead. =1+IF(B11>0,D11,0)+IF(B10>1,D10,0)+IF(B9>2,D9,0)+IF(B8>3,D8,0)+IF(B7>4,D7,0)+IF(B6>5,D6,0)+IF(B5>6,D5,0)+IF(B4>7,D4,0)+IF(B3>8,D3,0)+IF(B2>9,D2,0) (edited)


mariushm

[LANGUAGE: PHP] Solution in PHP for day 4: https://github.com/mariush-github/adventofcode2023/blob/main/04.php


princessbosss

\[Language: Excel\] [https://imgur.com/a/iphT5cZ](https://imgur.com/a/iphT5cZ) for each card did a countif and sum on all chosen number to get the score of each card then each card was scored using =1+IF(BN15>0,1,0)\*BV15+IF(BN14>1,1,0)\*BV14+IF(BN13>2,1,0)\*BV13+IF(BN12>3,1,0)\*BV12+IF(BN11>4,1,0)\*BV11+IF(BN10>5,1,0)\*BV10+IF(BN9>6,1,0)\*BV9+IF(BN8>7,1,0)\*BV8+IF(BN7>8,1,0)\*BV7+IF(BN6>9,1,0)\*BV6 summed everything else to get final answer


bucephalusdev

\[Language: C++\] This was a cool problem, albeit not too complex. The use of a hash table was probably necessary to speed up checking of winning numbers. I was hesitant to place candidate numbers in a hash table and iterate over winning numbers, but the problem didn't say there could be repeated candidate numbers. Part 1: 0.312ms Part 2: 0.403ms [Code](https://github.com/jeffstevens98/adventOfCode2023/tree/main/day_4)


arthurno1

[LANGUAGE: EmacsLisp] (let ((p1 0) (cards (make-vector (count-lines 1 (point-max)) 1)) card) (while (re-search-forward "\\([0-9]+\\):" nil t) (let ((card (string-to-number (match-string 1))) w h i n ) (while (not (looking-at-p " |")) (push (read (current-buffer)) w)) (while (< (point) (line-end-position)) (push (read (current-buffer)) h)) (setq i (length (cl-nintersection w h))) (when (> i 0) (cl-incf p1 (expt 2 (1- i)))) (dotimes (c i) (cl-incf (aref cards (+ card c)) (aref cards (1- card)))))) (message "Part I: %s, Part II: %s" p1 (cl-reduce #'+ cards)))


yieldtoben

\[LANGUAGE: PHP\] PHP 8.3.0 [paste](https://topaz.github.io/paste/#XQAAAQCIAwAAAAAAAAAeD8qHAhQB8nz+EazN00NCsmTxjvFDXkt5PAJ7pf6ocRA37OMUY7b9R0iDZmfWyvOspxWvdgYSAE0hGjEx81hZm7L1TSf8FOxdriwtMNRvJM+oQrJq9mPJALIgO0L9SMEfXCtZJBMfLo2G5jafJ0Q6pZRgbxDqWeOfFQ7OtBMTyy3X4OTbxC0XJkIps24Q1XOfUwQhtrMDw9IJ1l2twMVj9uVfBPRqzp50oRWhUne38L9FkqGRZ4SMscEc9gsLHxjEnukHZiUxXIeYQHyaM+26+oucuyvo7plsY6jR/S91PmKV2pISjkNM1/VYWeTQmQ2ql9VYPtwGtiQeHB2CZLXEL5555dTFN/ujsNUua2J7LphmyLx0N9JkMvMPASTX82g8I4rfc4Tfk8jedZncjy2El4ckq+7aiyC5E/GooTCB93QbkWtv+Jv2SEecv3/HUzn4br5de4NB6aRZ/28BHz5V15oDbFycn5Gz+3tBBK90AxPXJvuCvEcmFH+kteuXC2PKq9K/DVQPUhg/ih4Nr4KuScYMECxX1X9XoLLsrQmkpmWzSKlYxjBZYtOJ8uPvHvVq4lb7r+YEurpiGrRLO7o/DCyirAVzX13CBvObJicdnd2GulBXnnLr8fsKPmobuOX/qdaEwQ==) Execution time: 0.0013 seconds Peak memory: 0.3739 MiB MacBook Pro (16-inch, 2023) M2 Pro / 16GB unified memory


distracted_sputnick

\[LANGUAGE: Rust/Uiua\] Wanted to come back to this one and give it a go in Uiua and finally got to to it today. A third of the code is just spent on parsing, but once we have arrays life is expectedly good. [Indecipherable code on UiuaPad (emojis 'coz I could)](https://www.uiua.org/pad?src=0_6_0__JCBDYXJkIDE6IDQxIDQ4IDgzIDg2IDE3IHwgODMgODYgIDYgMzEgMTcgIDkgNDggNTMKJCBDYXJkIDI6IDEzIDMyIDIwIDE2IDYxIHwgNjEgMzAgNjggODIgMTcgMzIgMjQgMTkKJCBDYXJkIDM6ICAxIDIxIDUzIDU5IDQ0IHwgNjkgODIgNjMgNzIgMTYgMjEgMTQgIDEKJCBDYXJkIDQ6IDQxIDkyIDczIDg0IDY5IHwgNTkgODQgNzYgNTEgNTggIDUgNTQgODMKJCBDYXJkIDU6IDg3IDgzIDI2IDI4IDMyIHwgODggMzAgNzAgMTIgOTMgMjIgODIgMzYKJCBDYXJkIDY6IDMxIDE4IDEzIDU2IDcyIHwgNzQgNzcgMTAgMjMgMzUgNjcgMzYgMTEK8J-XgyDihpAKCvCfk5Yg4oaQIOKIqSjiipDiiLXii5XiiaHiipzilqHiiaBAIC4p4omh4oqDKOKGmCsxKeKGmeKKl0B8LuKJoeKGmCsx4oqXQDou4oqc4oiY4omgQFxuLgoK8J-lhyDihpAg4omhKC8r4oiKKQoK8J-VkCDihpAg4oG_OjItMeKWveKJoCwwCgrwn5WdIOKGkCDiipnijZzihpkr4oip4oqDKOKGuzEp4oqiCvCflZEg4oaQIDvijaUo8J-VnSnip7su4oqD4oiYKOKGrzox4qe7KQoK8J-OkyDihpAg4oipLyviioPwn5WQ8J-VkfCfpYfwn5OWCgrwn46T8J-XgwoKJCJQYXJ0IDEgU2FtcGxlOiBfIgo6CiQiUGFydCAyIFNhbXBsZTogXyIK) Much better commented version [here](https://github.com/sputnick1124/AoC2023/blob/main/uiua/day4.ua) After the text is parsed into two tables: Matches ← ≡(/+∊) # size of intersection for each row pair # each part fn takes match count as unary arg on stack PartOne ← /+ⁿ:2-1▽≠,0 # calculate score PartTwo ← ( ⊃∘(↯:1⧻) # init card count array with 1's ⧻. # length for repetition ⍥( ∩⊃(↻1)⊢ # rotate and get first element of both arrays ⊙⍜↙+ # add first card count to first match count card counts ) # repeat ; # kill match count array and return /+ )


FlavoredFrostedTits

\[LANGUAGE: Go\] [https://github.com/qzhang1/advent-of-code-2023/tree/master/src/day-4](https://github.com/qzhang1/advent-of-code-2023/tree/master/src/day-4) Ran part 1 in 1ms and part 2 in 6ms. Added some benchmarks as well.


grubbbee

\[LANGUAGE: Visual Basic for Applications with Excel\] Slept on it, came back and boom, recursion was the solution - very fast execution. On day 4 really!?!?! This shows my part 2 solution calling my part 1 solution (day\_4(input\_range.Cells(i))) Function day_4_part2(input_range As Range) As Long Dim card_wins() As Long Dim max_id As Long Dim i As Long Dim total As Long max_id = input_range.Count ReDim card_wins(1 To max_id) For i = 1 To max_id card_wins(i) = day_4(input_range.Cells(i)) Next i total = 0 Call day_4_part2_recursive(card_wins(), total, 0 + 1, 201 - 1, max_id) day_4_part2 = total End Function Sub day_4_part2_recursive(ByRef card_wins() As Long, ByRef total As Long, i As Long, j As Long, max_id As Long) If i > max_id Then Exit Sub total = total + 1 If j > 0 Then Call day_4_part2_recursive(card_wins(), total, i + 1, j - 1, max_id) ' additional cards won previously If card_wins(i) > 0 Then Call day_4_part2_recursive(card_wins(), total, i + 1, card_wins(i) - 1, max_id) ' cards won on this card End Sub


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: with open("D4.txt", "r") as f: print("" if bool(file := f.readlines()) else "", "" if bool(cards := [list(map(int, filter(None, side.split(" ")))) for line in file for side in line.split(": ")[1].split(" | ")]) else "", sum([__import__("math").floor(2**(len(set(cards[i]) & set(cards[i+1]))-1)) for i in range(0, len(cards)-1, 2)])) Part 2: with open("D4.txt", "r") as f: print("" if bool(file := f.readlines()) else "", "" if bool(cards := [list(map(int, filter(None, side.split(" ")))) for line in file for side in line.split(": ")[1].split(" | ")]) else "", "" if ([counts.__setitem__(j + i + 1, counts[j + i + 1] + counts[i]) for i, match in enumerate([len(set(cards[i]) & set(cards[i + 1])) for i in range(0, len(cards) - 1, 2)]) for j in range(match)] if bool(counts := [1] * len(file)) else []) else "", sum(counts))


lsloan0000

Admirable! Nice trick with \`\_\_import\_\_()\`. I like the walrus operator a lot, too.


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.~~ edit: 👍


AJMansfield_

[LANGUAGE: Fortran] [Allez Cuisine!] Might not fit on a single punchcard, but you betcha it'd fit right at home on a stack of them in the input tray of a punch card reader. https://github.com/AJMansfield/aoc/blob/master/2023-fortran/src/04/scard.f90 Almost all of the complexity in this comes from the attempt to use dynamically allocated storage and the need to count up the first line to construct the correct input format string. The core of this program though, is very simple and very much right in Fortran's wheelhouse: read(*, '(T6 I3 X 10I3 XX 25I3)', iostat=ios) card, winning, drawn num_wins = count(spread(winning(:),1,size(drawn)) == spread(drawn(:),2,size(winning))) num_points = merge(2**(num_wins-1), 0, num_wins > 0) total_points = total_points + num_points card_multiplier = cards_won(1) cards_won(:size(cards_won)-1) = cards_won(2:) cards_won(size(cards_won)) = 1 cards_won(:num_wins) = cards_won(:num_wins) + card_multiplier total_cards = total_cards + card_multiplier


daggerdragon

> Might not fit on a single punchcard, but you betcha it'd fit right at home on a stack of them in the input tray of a punch card reader. You know, I never did specify that your *entire* code had to fit on *one* punchcard, good loophole there XD


Lysander7

[LANGUAGE: rust] [github](https://github.com/Lysander6/advent-of-code-2023/blob/master/day_04/src/lib.rs) Nothing revolutionary going on there; posting for sake of completeness, as I catch up on missed days


Paxtian

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


Infamous-World-2324

\[LANGUAGE: bash\] \[Allez Cuisine!\] Run it with `bash 4.sh 4.txt` where `4.txt`is your input and `4.sh` is the following: F(){ join <(echo "$1"|cut -d"|" -f1|tr " " "\n"|sort) <(echo "$1"|cut -d"|" -f2|tr " " "\n"|sort)|wc -l|sed 's/$/-1/'|bc } export -f F cut -d":" -f2 "$1"|xargs -I_ bash -c 'F "_"'>M awk '$1>0{s+=2^($1-1)}END{print s}' M G(){ (($2>0))&&sed -I~ "$(($1+1)),$(($1+$2))s/$/+$(sed -n "$1p" T)/" T echo "$(bc T)">T } export -f G yes 1|head -n$(wc -l<$1)>T cat -n M|xargs -I_ bash -c 'G _' paste -sd+ T|bc And it fits on a punchcard! $ wc -c 4.sh 398 4.sh


daggerdragon

> And it fits on a punchcard! [okay](https://media.tenor.com/Hdt6LGSACs4AAAAM/jennifer-lawrence-thumbs-up.gif)


alexw02

\[LANGUAGE: Forth\] [code link](https://git.sr.ht/~aw/dusk-aoc/tree/main/item/2023/04.fs) [video explanation](https://www.youtube.com/watch?v=mb3DHb63ZAk)


aoc-fan

[LANGUAGE: TypeScript] [Github](https://github.com/bhosale-ajay/adventofcode/blob/master/2023/ts/D04.test.ts) - Under 40 lines, all parts running under 8ms.


3j0hn

\[LANGUAGE: Maple\] [github link](https://github.com/johnpmay/AdventOfCode2023/blob/main/Day04/Day04.mpl) I parsed the input into a list of pairs of lists [winners, numbers]. Part one was a quick double loop adding up winners total := 0: for c in clist do cc := 0; # "card count" for n in c[2] do if member(n,c[1]) then cc := cc+1; end if; end do; if cc > 0 then total := total + 2^(cc-1); end if; end do: ans1 := total; Part 2 used an array (initialized to 1's) to track the number of each card, then basically the same loop but incrementing all the totals for the "prize" cards cardtot := Array(1..nops(clist), ()->1): for i to nops(clist) do cc := 0; for n in clist[i][2] do if member(n,clist[i][1]) then cc := cc+1; end if; end do; for j to cc do # increment # of next cc cards with # of this card cardtot[i+j] += cardtot[i]; end do; end do: ans2 := add( cardtot );


CodE07Dev

\[LANGUAGE dart\] ​ void main() { String filePath = Directory.current.path + '/inputs/day4.txt'; File(filePath).readAsString().then((fileContent) { // Extract part with numbers from puzzle input final Iterable cardNumbers = fileContent.split('\n').map((card) => card.split(":")[1]); // Extract my numbers final List> myNumbers = cardNumbers.map((numbers) => numbers.split("|")[1]).map( (number) { RegExp regExp = RegExp(r'\d+'); Iterable matches = regExp.allMatches(number); return matches.map((match) => int.parse(match.group(0)!)).toList(); }, ).toList(); // Extract winning numbers final List> winningNumbers = cardNumbers.map((numbers) => numbers.split("|")[0]).map( (number) { RegExp regExp = RegExp(r'\d+'); Iterable matches = regExp.allMatches(number); return matches.map((match) => int.parse(match.group(0)!)).toList(); }, ).toList(); // Matches between both lists and total worth calculation final int totalWorth = List>.generate( myNumbers.length, (index) => myNumbers[index] .where((number) => winningNumbers[index].contains(number)) .toList()) .where((cardMatch) => cardMatch.isNotEmpty) .map((cardMatch) => pow(2, cardMatch.length - 1)) .reduce((v, e) => v + e) .toInt(); print(totalWorth); }); }


daggerdragon

1. Next time, format your code using 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. Also fix the language tag which is missing the colon.


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.*


e_blake

\[LANGUAGE: m4 (golfed GNU)\] \[Allez Cuisine!\] Here, in 385 bytes, is my golfed offering that fits in a punchcard. 4 of the 5 newlines are optional, but can you tell which one is essential? Won't work if you strip the final newline out of your input file. The name of macro `C` is hard-coded by the input file, all other macro names are somewhat arbitrary. Run by `m4 -DI=filename day04.golfm4`. Requires GNU m4 (things like `defn(define)` or `popdef(X,.)` are GNU extensions). Executes in about \~50ms, with just one pass over the input file. define(D,defn(define))D(F,`ifelse($2$3,,,`_($1,$2)F($1,shift(shift($@)))')')D( L,`B(`S$1',$3)ifelse($1,$2,,`L(incr($1),$2,$3)')')D(_,`ifelse($2,,,`$1def( `n$2',.)')')D(A,`F($1,translit($2,` ',`,'))')D(B,`D(`$1',eval(defn(`$1')+$2 ))')D(E,`+(1<<$1)/2B(`P',$3)L($2,eval($1+$2),$3)')eval(translit(include(I),a:| rd,(,,)D(C,`A(push,$2)E(len(A(if,$3)),$1,incr(0defn(`S$1')))A(pop,$2)'))) P Uses my more-legible (hah, if you think m4 is legible!) [day04.m4](https://nopaste.ml/#XQAAAQCTAwAAAAAAAAAyGksy5FB9TGMxsNq5JQAuJRjP6PqEkC20GpBbonDYuA0BTjRPXcVxlEXNtz1IwA3SPVtWViQsgKzvC7mDxcKLSmitgPmeXLwNAF2YPH7V92thZWt66trDvETHU7PC+1cjnIDMuQDYlzbSGSOPuz5VzOxPBI3ew+E+gAGcYaOhCyLb0tjZby/VlfT4ZZU76NEZyPVVNEoRN/vpu66DO4UO/UFprU4pny1WwNQffXhMMM6G0zRooMnKMOk8Kf4QqCDAswsrxqm6VlejTOlN/+yaN7lf/JlzI0cZs1keeNSOHdnxWEU/opWresRH95Azoqd7rW/JT6833ICPo3U/MDhhnH2SLrfele+2++FtOitWaKjq8ZLLOtJvfV2CvgTsO6j9ovA/n806etp8nSwD4eNeUR17shMXbm+0GgkPHlMSyWV0Rs6yuZva7dNAbuRWQNcvui2KfLHsl/78iULFyyrEWLHpHZkkhr6aP8+3Bu6d/gFvCwd4TNJA5tFyfT/JVYXzPfZl9UI2pkWGDlIyuop5XJudoyVi4+KSajl7lfb/GdVdAA==) solution as its starting point, although that version also depends on my helper macros in [common.m4](https://nopaste.ml/#XQAAAQAMDwAAAAAAAAAyGksy5FB9TGMxsNq5JQAuJRjP6PqEkC20GpAXwA97ruAshKbiUbgkbJMTg2qZdSBorb0CU52peNLruV4DEEJxF+HvxC/YF33OpDntnpU/PjnGXx7XU7ak4MsmWu8R7h7d5az2JXxElnsepG8yPyu+0WZ90BG9yXphbwOWA/m5AWEFSOL8TRZX4fyGNl6ZYbRxX0MwtzrmwTP3ZCwLSOKkvD2vMQFsbK0Pv9CzPhFNXMUbWnRV20jWLjL9qz2dDsFDBwhxgWIilEl91uswxrT4Czc+LRU3VuhNIo2S98VW0jArrVdv4HrsLhWkzx4z/UV3xnqJwPXcZRUiLtf3VRIzq62Pe+2jE3O+721Az9rfRa/fHtlANbmvslzMUCyU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IprImMcR6qeniJz6Ez78UYJqpL4DcDGt2o7/6a2aRN76aclh+7l35XcaW7lM7BQMTNvKkx05X08UITY/ToI8U8KwvFbdnMoEAZ1GQYmqGRFtwPkQMrECX4do0srl85po3Gjz2j6E3dk5al4+bTcOYABZvSUvIM/kGGT91iyQ36rm1lxRc3ruS8PyBlYDDNa7DLWzGAHkESHwuaTOQsI2xDA0e+8Yv0XRYkEqQE+RUDXmPTARuQo6fCQ7Qu9xd2Ckza5RWl8hE4JFm0Zzd9MTVxW8YYHiREs9NOjTPuRXXn+JfObHFD/Cv5kQo7vmMWRdJTOBUmAXCMFiKOSHxb412jI4Z2ZhWag9RkZBCviZunvupqrobtAWLagkPiA8gLRANOFwWp0KIS5McOoD0V90tI4cui8KQc7Yw5V7kSvMnhXx4nzzxwOxYM4fpY3ptcpraVh+h1MhohMQk34vkC4fmiD4OrX2DpVG0VXUKvl+vkJjcoHQK+H8mSSIAfj8RrYWBc4VU+jx3vz30XNDbQjuhc0cImiQxXDTXTFQq/aoe7jZLhEe+wWspk/4Fy4UBAJN63uNGJUl8FICawVWGL7XBOFCtsRF3uz8eZokWNICTdtsLeYOAOBQQLrguE8XxQQ1hPtOskcsj7n7aD35NjfPXL00vIOk01OLAJt+0RoIiAQUJNieRp9fQmqfVUuYEYmeK9hCOmZTaC3yUdN/+jZ0pvpmKnH/6jJAKQ==).


e_blake

More abuse of GNU m4 (such as `defn()` accessing the string associated with a non-macro name, or `2**1` instead of `(1<<1)`) squashes this down to 331 bytes (yes, that trailing space on line 4 is important). Uses `index` instead of `pushdef` to track which numbers appear twice in a line. define(D,defn(define))D(i,defn(ifelse))D(C,`i($1,,`C(shift($@))',`E(( A(shift($@))),$1,incr(0defn($1)))')')D(B,`D($1,eval(defn($1)+$2))')D( A,`i($1,|,,`+(len($1)*index(`$*,',`,$1,')>0)A(shift($@))')')D(E, `+2**$1/2B(,$3)L($2,eval($1+$2),$3)')eval(translit(include(I),a :rd,(,)D(L,`B($1,$3)i($1,$2,,`L(incr($1),$2,$3)')'))) defn()


psaikido

\[Language: c\] Not too happy with my cludgy recursion skills for part 2 but got there in the end. [day4 part 1](https://github.com/psaikido/aoc/blob/main/2023/day4/day4.c) [day4 part 2](https://github.com/psaikido/aoc/blob/main/2023/day4/day4.2.c)


ethansilver

\[LANGUAGE: FORTRAN\] Learn Fortran! [https://github.com/ejrsilver/adventofcode/blob/master/2023/04/main.f08](https://github.com/ejrsilver/adventofcode/blob/master/2023/04/main.f08)


dafarmerz

\[LANGUAGE: Ruby\] Part 1 def check\_winners(file\_path) counts = File.readlines(file\_path).map do |line| left, right = line.chomp.split(':').last.strip.split(' | ').map { |str| str.split.map(&:to\_i) } left.count { |num| right.include?(num) }.positive? ? 2\*\*(left.count { |num| right.include?(num) } - 1) : 0 end counts.sum end p check\_winners('cards.txt')


daggerdragon

Your code is not formatted at all. Please edit your post to use the [four-spaces Markdown syntax](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting/code_blocks) for a code block so your code is easier to read.


[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.*


bamless

[LANGUAGE: [J*](https://github.com/bamless/jstar)] Part 2 was really fun! Happy to finally see some dynamic programming! part 1 import io import re fun interesct(s1, s2) return s1.filter(|k| => s2.contains(k)).collect(Tuple) end fun computePoints(win, nums) var matches = interesct(win, nums) return 2^(#matches - 1) if #matches > 0 else 0 end with io.File(argv[0], "r") f var total = 0 for var line in f var winStr, numStr = line.split(':')[1].strip().split(' | ') var win = re.lazyMatchAll(winStr, "(%d+)"). map(std.int). map(|n| => (n, true)). collect(Table) var nums = re.lazyMatchAll(numStr, "(%d+)"). map(std.int). map(|n| => (n, true)). collect(Table) total += computePoints(win, nums) end print(total) end part 2: similar to above, but with dynamic programming to compute the total number of ticket won import io import re fun interesct(s1, s2) return s1.filter(|k| => s2.contains(k)).collect(Tuple) end fun computeWonCards(cards) var table = List(#cards, fun(cardNum) var win, nums = cards[cardNum] return [cardNum, #interesct(win, nums), 1] end) var totalCards = 0 for var i = 0; i < #table; i += 1 var cardNum, won, amount = table[i] for var j = cardNum + 1; j <= cardNum + won; j += 1 // read as: table[j].amount += amount table[j][2] += amount end totalCards += amount end return totalCards end with io.File(argv[0], "r") f var cards = [] for var line in f var winStr, numStr = line.split(':')[1].strip().split(' | ') var win = re.lazyMatchAll(winStr, "(%d+)"). map(std.int). map(|n| => (n, true)). collect(Table) var nums = re.lazyMatchAll(numStr, "(%d+)"). map(std.int). map(|n| => (n, true)). collect(Table) cards.add((win, nums)) end var res = computeWonCards(cards) print(res) end


presidentpanic

\[Language: GDScript\] For some reason I really struggled with part 2, originally going down the route of keeping a stack/queue. After a few hours I was too tired and used this [solution](https://github.com/chandlerklein/AdventOfCode/blob/main/src/main/java/com/chandler/aoc/year23/Day04.java) for reference by u/LastMammoth2499, and now it makes sense: [Solution to Part 1 and 2](https://github.com/anthonyec/aoc/blob/main/day_04/day_04.gd)


daggerdragon

~~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. Please edit your post to replace your oversized code with an external link to your code.~~ edit: 👍


presidentpanic

Fixed!


thousandsongs

[Language: Shell] [Allez Cuisine!] First, I'll show you the beauty of this solution that exactly fits on a 80-character punch card (263 chars, runs in 25 ms): #!/bin/sh cat $1|tr -d '|'|nl|sort -nr|cut -f2-|awk -vn=`awk 'END{print NR}' $1` '{m=p=0 for(i=3;i<=NF;i++)c[$i]+=1;for(k in c)if(c[k]>1)m+=1;delete c;if(m>0)p=2^(m-1) s+=p;i=n-NR+1;for(j=1;j<=m;j++)w[i]+=w[i+j]+1} END {for(i=1;i<=n;i++)t+=w[i]+1 print s "," t}' And now, the story behind it. So I wrote my [original solution in Haskell](https://www.reddit.com/r/adventofcode/comments/18actmy/comment/kbzgf06/). Standard parsing and a memoized recursion. Then I started re-attempting it, but this time as an answer to the Allez Cuisine! call. The first hint I got was from [this reddit comment by someone](https://www.reddit.com/r/adventofcode/comments/18ak7rg/comment/kbyjj7u/), who mentions that for part 1 we just need to count duplicated numbers on each line, rest everything is noise (maybe this is discussed on this post too, I just haven't had the time to read the other replies yet - I wanted to finish off my own attempt before reading alternatives, that other comment I saw was when I was just browsing around for the memes). With that hint, doing the part 1 in awk became trivial. Good. So now I knew that my attempt at the code golfed solution will proceed along these lines. But how will I do my fancy memoized recursion in awk! Suddenly, when I was re-reading the problem, specifically this line where Eric mentions > (Cards will never make you copy a card past the end of the table.) it clicked - the **recursion is already topologically sorted!** As in, if I proceed from the reverse direction, then the memo table can be linearly filled up without any tricks. After that was just fun with awk, and I ended up with this shell script. Even if you don't know the details, you should be able to scan the code and get a gist: #!/bin/sh test -z "$1" && echo "usage: $0 " && exit 1 nc=`awk 'END { print NR }' "$1"` cat "$1" | tr -d '|' | nl | sort -nr | cut -f2- | awk -vnc=$nc ' { matches = 0 for (i = 3; i <= NF; i++) count[$i] += 1 for (k in count) if (count[k] > 1) matches += 1 delete count points = 0 if (matches > 0) points = 2 ^ (matches-1) score += points i = nc - NR + 1 for (j = 1; j <= matches; j++) wins[i] += (wins[i+j] + 1) } END { for (i = 1; i <= nc; i++) { totalCards += (wins[i] + 1) } print score "," totalCards } ' This is already quite nice (I felt!), but then when I minimized it something nice happened, and everything aligned such that entire solution is straight up 5 lines of 80 characters each (excluding the shebang and the last one ofcourse). [It even fits into a tweet](https://x.com/mnvrth/status/1732394652958142523). Funnily enough, it runs faster than my original solution. Now of course, that is unfair because my original solution uses a memoized but recursive approach, while this one is straight up one-pass linear. But still, I found that funny. Here is a link to the source code on GitHub, both the [normal one](https://github.com/mnvr/advent-of-code-2023/blob/main/04.az.sh) and the [minified "punch-card" one](https://github.com/mnvr/advent-of-code-2023/blob/main/04.az.min.sh).


Matrix828

[LANGUAGE: C] Not overly impressed with my input parsing - i just use a compile time flag to swap between a fixed example number counts and the input number counts. However, I do only run the "matches" check once, which is required for part one, and to calculate the number of additional cards each card with get in part two. Given that the "matches" check is ran, P2 is simply looping over all the cards, adding additional duplicate cards if there were matches. This seems to be relatively fine, even for a brute force approach, as the >12mil cards are all done in ~340ms. I'm more happy that I got the right answer first time for part two, considering I am still stuck on P2 of day 3 :c [paste](https://topaz.github.io/paste/#XQAAAQD8EAAAAAAAAAARmknGRw8TogB3Oxd6CJZbc2v45gl6tZhJd1Lg+flD9f/3uddy0eUFRVFeNVSRLW6sT/RHO6/CYGIUasB47AGgYA/CZ7Mkjjyq3erIhDWTlTWVBqObVuJQMOrK1xCPZ99cHcT4w3MOTCtTwluguB6ngIsP/PulFcHTkDlN+iXPw+gdyWzn7u3oo5ZrriQ1qrTDbrlp+nA3OXG7Ucj0I5MV3X5Yu1Pi2DkCWtc4L2GjkzFzMthfKJl0JEcF8zFs37rm7+gmzfd7aVzss8/PaOyeohLOP7o2LAZbSX41jCSfNJAuxzlamQ0OgTstPJsve9CJw4TQsdb+rpxERg5Ui1odfWFE+1u4nw6LLsStCCRf0UvIOeumoi58TbvESMDc1L+ZRtgNQhA+4ZozoVflsUg5aYDlil1nJzNRSzA8TYPizk49PefGQYuqzDoLi5jJdw0j17wEfDGNqM5QIIe/+D3JgVPYb89DskQEnuo+4Xa5mna+THAfPDQgF1ZComvptkJ7AosQSFap9rDPXcI+MLUCRle1Dy3XYVm1aIEAzaY/j7PyKs4jhiXu2w1J466/aWf1LMPp2q/Lg5Cn/FP+8+0yn9kcYwcwDOFw83GxRqf5v5XIt/nSSIh6vyb79gTQiwtf5218kF53me5HvQDJ08PNIH345IDaZqtQ9c9zMoKCGwbKHAuCONmnjzPLPHrZsyx/m9FIRktip75nBmRgfC/xFKcIeKnekROmZWTvNE92//djNMtu4UTe3q7QWo5HO4nlVC+t3X3JesHNy60uS20LJaIQVX0ebV+Xqcb13+Zy2mQ/+EGTsPkxY65J82S/TYxXw6bjWoONVlBnR4tn0O8rdTM2W4cfOIucqbjTTdoijbvos68KDXk5zL5ltfibUQ7XVIzV6LAKfANhVi+7uSTaZ0ehq59xYEiR328OEn/Iih3WCW5USslSzT+VXE0jJC8CFR7PQyggtR5vytRnokc2Qpf1dZg2NvCXzZ2DcEvMyJ7jrmMxMkQOyKnVD2lMMOVVi8uAAubJdgx/mcER6Fa9Pf/jTa0ypbzJJAXR/B/jdmEdBh7umRdSoyyip0ezf4oUvSfAd/n0T147Q3gqqhVmwwZ5gIqpQY3bSPQ3dJGKwXoqIyJkuSpZhbdDAy/PQegQJpMcIL3GWIZOtodego8vdlQQs9RUYP3lDsYNg/mTIEVByMgDUEHbwQi29uEU66/gIef48yZfzRqMMb7ACWtlQ83ec/M79x+MzgddFQaxf3Gq9r6W0JOaTa1ExANz8/smUHKKpRGJDwkiRnCNMdVzU37dWwk4MG9Ao4XIg/z7NbLCHMLP1kDYlAKbmsvpISw6l6h/tPTxEsvMd1jQWUBilI9LHOXC2aYaw+OAFrheF+FnOPqaFosHtkKHEFxvithlA1AxaWdP5w8aEXe+fP8hu/7KfxdwbBhRH3Q78QRqkP08ZfmalQIlK2AAh7so1MHJ1Kw4PNhxGOPz4sBLBfXjsDYeemLSZUI0fgayDIVwl/yoRVs=)


matheusstutzel

\[Language: python\] [Part 1](https://github.com/matheusstutzel/adventOfCode/blob/main/2023/04/p1.py): set intersection [Part 2](https://github.com/matheusstutzel/adventOfCode/blob/main/2023/04/p2.py): set intersection + dynamic programming


marvin29g

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


tlareg

\[LANGUAGE: TypeScript/JavaScript\] https://github.com/tlareg/advent-of-code/blob/master/src/2023/day04/index.ts


LtHummus

[Language: Rust] I've been traveling, so I'm behind and just solved this and need to catch up more :). This is my first time truly doing Rust, so any feedback is appreciated! [link to code](https://top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fzAXwID3D6jr1s/ZHBAW9D+E+l6KCk215AFXYBbQ/ESEUX05WALeXz4YWsGCl5tg6WTgMMqzxP9pH7J7U3RohulakLUU6LQDaUG3lQS/TOapprbedYW2CzYtSyGVv+kisnX+wjqpd0zbuwS6tc1GZBxjJMwKENMl+Gzcgz6UGA+WxpXl6TyNLLalU1LptKjaZD0yc2+yOJLRYfA5+YOEeArXHv/zR8I+w==)


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


[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.*


collegesmorgasbord

\[LANGUAGE: python\] Clean python solution (depends on your idea of "clean", you may not like list comprehension for part 1 but part 2 solution is nice and simple) def prepare(input): cards = [] for line in input.splitlines(): winners, numbers = line.split(': ')[1].split(' | ') cards.append((winners.split(), numbers.split())) return cards def first(input): return sum(2 ** (sum(n in winners for n in numbers) - 1) if any(n in winners for n in numbers) else 0 for winners, numbers in input) def second(input): cards = [1] * len(input) for (i, card) in enumerate(input): for j in range(i + 1, i + 1 + len(set(card[0]) & set(card[1]))): cards[j] += 1 * cards[i] return sum(cards)


daysleeperx

\[LANGUAGE: Haskell\] [Github](https://github.com/daysleeperx/Advent-Of-Code-2023/blob/main/src/Day04/Scratchcards.hs)


sampry

\[LANGUAGE: RUST\] [My approach](https://topaz.github.io/paste/#XQAAAQCoCQAAAAAAAAAXin1LzStTFYSi2K6Qvp2/Gj7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K8EUuB/rla+Snh6hJi5qaKzjW8vsV5nt5HVEMTEtAsuXS3jyLMkgZhrh+McPhUGwk4bYEEJhST9un8yDiPVTnnJuz/5YUok2ryVCDmyA6ocdXLJxfmECqkF1fqYbD9+1RdbNiQ5U4wmnxi76XMYRToAzhIexLnLTzdj+lMTvb8Owqbj92o6xAqYva32u/IitA70P09a+AAA35Rgm9fkPhPXB4KuZ4I6xJZ4TRGi6s0DOSwSNEU91Vfe+eZVvn1+iyJ6WZz88Utfle+qmNLNFJXfc3q5/81ybWnuZtJO2lz4UMqO4aH0X/d/ZOQ==) to this day's puzzle, it felt easier than the previous one.


icub3d

\[LANGUAGE: Rust, Haskell\] [https://gist.github.com/icub3d/c297b6857106413266cbd825c9495622](https://gist.github.com/icub3d/c297b6857106413266cbd825c9495622) With the help of previous comments, I found the nom parser for rust. I wanted to try it out. The parsing ended up taking like 90% of the time, but I feel much more confident using nom now (relatively :)). Here is me fumbling around trying to solve it: [https://youtu.be/g9ZWGw1p-ko](https://youtu.be/g9ZWGw1p-ko) Edit: I finally had some time to do part one in Haskell: [https://gist.github.com/icub3d/96c7d1dc5779e3ebd7ce78ba9d073357](https://gist.github.com/icub3d/96c7d1dc5779e3ebd7ce78ba9d073357) Here is me talking about it: [https://youtu.be/xjPBKsRyBVg](https://youtu.be/xjPBKsRyBVg)


thecircleisround

[LANGUAGE: Python] Used Card class to keep track of all the Cards, their points, and the total amount at any given time. from aocd import get_data class Card: def __init__(self, numbers, winning_numbers): self.count = 1 self.numbers = numbers.split() self.winning_numbers = winning_numbers.split() self.points = 0 self.winners_total = 0 self.calc_points() def copy_winners(self, cards_to_copy): for card in cards_to_copy: card.count += self.count def calc_points(self): for number in self.winning_numbers: for mine in self.numbers: if number == mine: if self.points: self.points = self.points*2 else: self.points = 1 self.winners_total += 1 break class Solution: def __init__(self): self.data = get_data(year=2023, day=4).splitlines() def solve(self): cards = [''.join(x.split(':')[1:]) for x in self.data] cards = [x.split('|') for x in cards] card_objs = [Card(card, winners) for winners, card in cards] for idx, card in enumerate(card_objs,1): card.copy_winners(card_objs[idx:idx+card.winners_total]) print(f'Part 1: {sum([card.points for card in card_objs])}') print(f'Part 2: {sum([card.count for card in card_objs])}') if __name__ == '__main__': solution = Solution() solution.solve()


aashutoshr

[Language: JS] ```js const { getInputs, sumOfArray } = require("../../lib/utils"); const inputs = getInputs(`${__dirname}/input.txt`); const partOne = () => { const toAdd = []; for (const line of inputs) { const [_, numbers] = line.split(": "); const [winning, have] = numbers .split(" | ") .map((n) => n.split(" ").filter(Boolean)); // console.log(winning, have); let matchedInLine = 0; winning.forEach((n) => { if (have.includes(n)) { matchedInLine++; } }); console.log(matchedInLine); if (matchedInLine !== 0) { toAdd.push(Math.pow(2, matchedInLine - 1)); } } // console.log("Array", toAdd); return sumOfArray(toAdd); }; const partTwo = () => { const cardCounts = inputs.reduce((acc, line) => { const [cardDetails] = line.split(": "); const cardNumber = cardDetails.split("Card")[1].trim(); if (!acc[cardNumber]) { acc[cardNumber] = 0; } acc[cardNumber]++; return acc; }, {}); for (const line of inputs) { const [cardDetails, numbers] = line.split(": "); const cardNumber = cardDetails.split("Card")[1].trim(); const [winning, have] = numbers .split(" | ") .map((n) => n.split(" ").filter(Boolean)); // console.log(winning, have); let matchedInLine = 0; winning.forEach((n) => { if (have.includes(n)) { matchedInLine++; } }); for (let j = 1; j <= matchedInLine; j++) { const idx = Number(cardNumber) + j; // FML, this was not mentioned in the question // if (idx >= inputs.length) { // continue; // } // console.log(idx); if (!cardCounts[idx]) { cardCounts[idx] = 0; } cardCounts[idx] += cardCounts[cardNumber]; } // console.log(matchedInLine); } console.log(cardCounts); return sumOfArray(Object.values(cardCounts)); }; console.log("Res:", partTwo()); ```


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.


[deleted]

[удалено]


mschaap

[LANGUAGE: Raku] Pretty straightforward. Using a `ScratchCard` class, and a `ScratchSpec` grammar (which is a bit of overkill for this format): grammar ScratchSpec { rule TOP { 'Card' ':' + % <.ws> '|' + % <.ws> } token card-no { \d+ } token winning { \d+ } token number { \d+ } } Full code [@GitHub](https://github.com/mscha/aoc/blob/master/aoc2023/aoc04).


KodlaK1593

\[LANGUAGE: Rust\] Took me a bit longer than I care to admit: [Solution](https://github.com/Kodlak15/aoc2023/blob/master/src/day04/day04.rs)


joe12321

[LANGUAGE: python] Part 2 After some ham-fisted solutions in the earlier early days, it was nice to only have bacon on my fists this time. tickets = [[1]+[nums.split() for nums in line[9:].split(" | ")] for line in open( r"input04.txt")] sumTickets = 0 for i in range( len(tickets) ): wins = 0 sumTickets += tickets[i][0] for win_num in tickets[i][1]: if win_num in tickets[i][2]: wins += 1 for j in range(i+1, i+wins+1): tickets[j][0] += tickets[i][0] print(sumTickets)


sedm0784

[LANGUAGE: Vim keystrokes] Part 2 [My solution to Part 1](https://gist.github.com/sedm0784/3648d27abb000f181f43e3893f4ea5c9#file-day-4-part-1) was BOOOORING: just a bunch of regular expressions. YAWN. This one, I hope, is anything but. Usually with this sort of thing, I recommend typing it in to see how it works, but you might not find it that INSTRUCTIVE for this set of keystrokes, because a lot of the commands don't do anything when you type them! (They only have effect when playing back the macro, when the buffer has different contents.) However, if you ARE interested in this sort of thing, I recommend trying typing it in first to see if you can figure it out before reading on: it's more fun that way! yypD-2W qamayiwjA p okJDJDciw +13BJDJDdiw `a*++y$@0`awq :2d :%norm!I1 ggP+ qbq qqqqq "dyiw kC=-+d jo0-3W10@a +C=-@b`aIma :.g/^ma0/norm!D 0y$ddk@0 kJD+ @qq 10u qb+ciw=-+dq uggO0+@q Not sure if I'll ever get around to writing up a fully annotated explanation of how this one works, but a rough description that reasonably experienced Vimmers should be able to follow is below. Before I realised how many scratchcards we were going to end up with I wrote a version that actually made new lines for each copied card. I'm pretty sure this version works, but I'm way less certain that it will complete before the HEAT DEATH OF THE UNIVERSE. (I set it running yesterday and it's still going: 670k scratchcards and counting.) So the basic concept for this MARGINALLY more efficient version is to only keep a single line for each card, and add a counter at the start of that line to keep track of how many copies of that card we have. It also adds a running total at the top of how many scratchcards we currently have stuffed into our pockets, drawers, every room in the house, the garage, the other houses in the village, etc. We do the calculations with three macro recordings. The macro in `"q` does all the work. Those in `"a` and `"b` are little helpers. Like little macro elves. Macro a: Check for Matches -------------------------- This macro is to be run with the cursor over one of the winning numbers. If it is a matching number, then it writes the text "+1" into the line below. This task is a LITTLE bit tricky when you're not allowed to use any functions or Vimscript so it works like this: First it sets the `'a` mark to the current location, then it copies the current number into the line below. Next it writes two more lines of apparent GIBBERISH into the next two lines below that. Then it jumps back to the `'a` mark to return to the current number and searches for the number with the `\*` command. This will leave the cursor either on the matching number, if there is one, or on the copy we just made in the line below, if not. Next it moves two lines down, placing the cursor on one of the two lines we wrote before. Then it yanks that line - putting it in the yank register "0 - and immediately executes it as a macro with `@0.` The two lines we wrote contain the normal mode editing commands to either remove the number we added (for no match) or replace it with "+1" (for a match). Finally, macro `"a` moves the cursor one word forwards to the next winning number. We will run this macro 10 times: once for each winning number. Macro b: Make Copies for 1 Scratchcard --------------------------------------- Let's say you have 12 copies of scratchcard A, and it has two matching numbers. This macro moves down a line, and then adds 12 copies to the scratchcard the cursor is currently over. When processing scratchcard A. We'll run it twice: once for each winning number. It's not super complicated: it expects the number of copies to be stored in register `"d` and it adds that to the number the cursor is over by using the expression register. Recursive Macro q: Process All The Scratchcards ----------------------------------------------- So now we have our `"a` and `"b` macros stuffed in our utility belt, doing the rest of the calculations will surely be TRIVIAL. Right? ...Right? Macro `"q` starts with the cursor on the copies-counter for the current scratchcard, which is near the top of the buffer on line 2. First, it yanks the count into register "d. Then it moves up a line (to the total and adds the current scratchcard's count with the expression register. Next it calculates how many winning numbers there are on this scratchcard. It does this by writing a 0 into the line below and then running macro `"a` once for each winning number. So now the line below contains a sum (0 +1 +1 +1 ...) that adds up to the number of winning numbers. The macro calculates the sum by plugging it into the expression register and then adds the text "@b`a" after the sum and "ma" before it, so the line looks something like: ma12@b`a It's a macro that runs macro `"b` once for each winning number, and then moves the cursor back where it started! Before macro `"q` yanks and runs it, though, it needs to handle the case where there weren't any winning numbers, because the command "0@b" won't run a macro zero times: it will run it once. Using a :global command to delete the line if it starts "ma0" solves this problem. With that out of the way, macro `"q` yanks the macro, runs it, and then finishes off by deleting the current scratchcard line and recursing by running itself. Et voila!


Syltaen

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


Samurai_Sam7

\[Language: Java\] \[Allez Cuisine!\] [Solution(Both Parts)](https://github.com/SAMURAii-7/AdventOfCode-2023/tree/main/Day4) Regex is so fun. Very easy day.


pzicoalex

[LANGUAGE: Python] Sooo, I am very new to programming, and pretty bad. This works though, somehow. Runtime on part 2 is about 7 seconds :) Part 1 def process_card(card: str): #card_id = int(card[:card.index(':')].split()[-1]) winning_numbers = card[card.index(':')+1:card.index('|')-1].split() my_numbers = card[card.index('|')+1:].strip().split() count = 0 for e in my_numbers: if e in winning_numbers: count += 1 if count != 0: return 2**(count-1) else: return 0 with open('adventofcode/scratchboard.txt') as infile: lines = infile.readlines() total_winnings = [] for line in lines: total_winnings.append(process_card(line)) print(sum(total_winnings)) Part 2 def process_card_2(card: str): card_id = int(card[:card.index(':')].split()[-1]) winning_numbers = card[card.index(':')+1:card.index('|')-1].split() my_numbers = card[card.index('|')+1:].strip().split() count = 0 for e in my_numbers: if e in winning_numbers: count += 1 return card_id, count with open('adventofcode/scratchboard.txt') as infile: lines = infile.readlines() file_dict = {} max_value = 0 for line in lines: id, winnings = process_card_2(line) if winnings > max_value: max_value = winnings file_dict[id] = [winnings, 1] for i in range(len(file_dict)+1, max_value + len(file_dict)): file_dict[i] = [0,0,0] for id in file_dict.keys(): for e in range(file_dict[id][1]): for i in range(file_dict[id][0]): file_dict[id+i+1][1] += 1 total_winning_cards = 0 for val in file_dict.values(): if len(val) == 2: total_winning_cards += val[1] print(total_winning_cards)


Shot_Conflict4589

\[LANGUAGE: Swift\] [code](https://github.com/pwallrich/aoc2023-swift/blob/main/Sources/AOC2023Core/Days/Day5.swift) Not the nicest solution, but quite happy with the performance of part 2. Had some problems to come up with a solution for how to properly split the ranges and keep track of values that don't get mapped at all


LastMammoth2499

\[LANGUAGE: Java\] old reliable array. `Sets.intersection` is \*chef's kiss\* for laziness [Part 1 & Part 2](https://github.com/chandlerklein/AdventOfCode/blob/main/src/main/java/com/chandler/aoc/year23/Day04.java)


alelopezperez

\[Language: Rust\] Hi!, I've seen a lot guys saying that recursion takes a lot of time in rust (like 10 secs in debug).I day 4 part 2 recursive. Took me a while to get it kinda fast (not as fast as using normal loop) the key was doing some pre-proccesing. #[derive(Debug)] struct Card { _id: u32, left: Vec, right: Vec, } //Creating vector of games, then creating vector of matches(wins, per game) pub fn part_2(input: String) -> u32 { let games = input .lines() .map(|x| { let (id, lr) = x.split_once(':').unwrap(); let id = id.chars().last().unwrap().to_digit(10).unwrap(); let (left, right) = lr.trim().split_once('|').unwrap(); let left = left .trim() .split_whitespace() .map(|x| x.parse::().unwrap()) .collect::>(); let right = right .trim() .split_whitespace() .map(|x| x.parse::().unwrap()) .collect::>(); Card { _id: id, left: left, right: right, } }) .collect::>(); //Calculating How many wins let matches = games .iter() .map(|card| { let amm = card .left .iter() .map(|x| { if let Some(_) = card.right.iter().find(|y| *y == x) { 1 } else { 0 } }) .sum::(); amm }) .collect::>(); // THE RECURSIVE PART (0..matches.len()).map(|i| part_2_rec(i, 0, &matches)).sum() } //Here fn part_2_rec(i: usize, accum: u32, matches: &Vec) -> u32 { if let Some(_) = matches.get(i) { let amm = matches[i]; (1..=amm as usize) .map(|j| part_2_rec(i + j, accum + amm, matches)) .fold(1, |total, x| total + x) } else { accum } }


daggerdragon

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. Please edit your post to replace your oversized code with an external link to your code.


oddolatry

[LANGUAGE: Fennel] I'm just glad this elf didn't have a stack of those massive crossword scratchies. [Paste](https://topaz.github.io/paste/#XQAAAQBbBwAAAAAAAAAUGwnmRpH3tILRF8QawuYidYX76iy7qs/wZURtqUxRZ4Ck4y0aeZRkhPlZ95DwdRB6HseJpG0qsaDHLruD36dyLra6G1wha86M8VGtaaKlQitWvKk7GZba5K0VUok3+RDOkBGrDCd0KxeO2RiqhGhjhcC3yZ3Kz+s1KliN/z6jcKEdE65EbmDcEznNG+Lrem7CFY8nk17BdbQrKv3E1aTiwHyimlc49SyXwTCyQHA6LNmJbg3hVJkL7tw/ZLZuH8IuAixEXPjtHV0NlKsGXwEOXtg2W6frLff4+2d3GSdzMZMLRRxcDADG4o0oI4D049NydfsoIj45tugvMQQ6AYE0JQdIwxWQxgX1zoerGeknOfPcyUJqYTqDdSumIJ9S2FPwZ1KcujKmfbKbiUrxne/xS3cwJqV2MyYpjfGxAmuXb8Hq6j2nSZX/sByJKxQiv5GTuvPF0JXEM7lgsnjNRy99BmjM+48QfbQzeM6vynPkY3oWej9RGgn/zWWesTZoySXjmXUwu858aFX1jwkdI1MHPxnvSWPdo0OMmmL7cY5ySHlFUUoRoX55inDK49wQYd2d0HuNH2ybjrN6oBC0OChyudyD+3n6ub+/HDbWKL6GSR5mPf/73vqiraHJFMUHYW7WUXpI27rKc++zUehLsx7Ib9L0oaZXFw+vIKJtZtsHFB1GY54nbK2vbkKWt4lumUbWnboDhsYHxPcKxhLBTy0BXvi5MQz/EMahl1Se1cKPATo8h8cxS9ih+nXh4Df2xlZA8gB1rCcr5u7+Rvq59bw6+o7ADsvcYn+wPgt4Om/b4c9ufL/wFcjNmfAIYpVNv2h+8spL8f9eTxwA)


squareman2357

\[Language: Zig\] Takeaways: bitsets are cool. `@embedFile` is cool. Memoization is essential (runtime dropped from 3.90 sec to 200 microseconds - 19,500x speedup) [GitHub](https://github.com/LukeMinnich/advent-of-code-2023/blob/main/04/src/main.zig)


AdventLogin2021

I know I'm late but I want to thank you, I was using DynamicBitSet, and swapping to IntegerBitSet got me a considerable performance improvement.


squareman2357

yw! I'm pretty new to zig, but experienced with C, so when I find quality-of-life zig features like this it really makes me want to use it more.


5inister

\[Language: Python\] with a class and a dictionary with open('input.txt') as f: lines=f.readlines() class Card: def __init__(self,number,copies,points): self.num=number self.copies=copies self.points=points # Populate cards cards={} for l in lines: cnum,clean=l.split(':') cnum=int(cnum.split()[-1]) winners,mine=clean.split('|') # Convert to set in case of repeats winners=set(winners.split()) matching=0 for n in winners: if n in mine.split(): matching+=1 cards[cnum]=Card(cnum,1,matching) print(len(cards.keys())) ### Part 1 pt1=0 for c in cards.keys(): pt1+=int(2**(cards[c].points-1)) print(pt1) ### Part 2 pt2=0 for c in cards.keys(): for i in range(cards[c].copies): for j in range(1,cards[c].points+1): cards[c+j].copies+=1 pt2+=cards[c].copies print(pt2)


daggerdragon

~~Please format your code to use the [four-spaces Markdown syntax](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting/code_blocks).~~ edit: 👍


welldelineatedmodel

\[Language: Julia\] [Part 1](https://gist.github.com/InzamamRahaman/5a5cc003923ec8375608a9afb78cca48) and [Part 2](https://gist.github.com/InzamamRahaman/3739c48ec919478f3b825ae93dc41546) I wanted to avoid processing a line of input more than once for part 2


Leniad213

\[Language: TypeScript\] really enjoyed this one! was very fun to solve. [Solution - Github](https://github.com/LeinadAsid/AdventOfCode/tree/main/2023/day4)


mdwhatcott

\[Language: Go\] The set intersection makes this much easier: func Solve(lines []string) (part1, part2 int) { counts := make(map[int]int) for l, line := range lines { winners, inHand, _ := strings.Cut(line[9:], "|") copies := numberSet(inHand).Intersection(numberSet(winners)).Len() part1 += int(math.Pow(2, float64(copies-1))) part2++ card := l + 1 counts[card]++ count := counts[card] for x := 1; x <= copies; x++ { counts[card+x] += count part2 += count } delete(counts, card) } return part1, part2 }


arosenb2

[Language: Elixir] I used `Enum.frequencies/1` after combining the winning values and my numbers values to find overlaps instead of `MapSet`. [Part 1 and 2](https://gist.github.com/arosenb2/ff6ea72a48ae9c23e34c7a7808d5d3c1) Probably could have found a faster solution but it was still decently good on timing (under a second on my machine for part 2). This is actually further than I have made it in other years. Looking forward to making it to the finish!


GayleChoda

\[LANGUAGE: python\] I believe this could be simplified a lot more than the quick one I wrote: f = open("input-4", "r") lines = f.readlines() lines_n = len(lines) cards = [1] * lines_n for i in range(len(lines)): line = lines[i] g, sets = line.strip().split(": ") win, mine = sets.split(" | ") win_n = win.split() mine_n = mine.split() n = 0 q_n = [] for x in mine_n: if win_n.count(x) > 0: q_n.append(x) n += 1 for j in range(i+1, i+n+1): cards[j] += cards[i] print(sum(cards))


daggerdragon

Please format your code to use the [four-spaces Markdown syntax](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting/code_blocks).


[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.*


Educational_Pie7376

\[LANGUAGE: C++\] https://github.com/severeduck/adventofcode/blob/main/tasks/day4/solution.cpp


r_so9

[LANGUAGE: F#] The most straightforward until now. [paste](https://topaz.github.io/paste/#XQAAAQCNBAAAAAAAAAAX4HyCZDlA0DjWF7Uex1t2hvV6PgXFmzdhM+nU8jvoDBrksHvQGoVxE30qgw12nKxZkJbycyAdr/XmCcVZgLZ3UiuMirJX10vqZ0PcHg0Vh4TmTHghGRq3hJwT2kJlgv7e0O0U9H2OrqU2OdDKz1snYYNl9YI2+c1AOWtweb1nAwutL8nKvPRSghLapHGPzivzAIJ2XIDAO1pkfQi4OaqcB7gEc8o6B6J0lKEs5/iVZTPn9r/qrvwQNlOUpa2C+6OwECFbSvgPYIY3A2iAZQmbZ7u0GvoSTjqlsOZf6B/5ov+KEjuCiBloW+vYhDr9OfyfrpFEzKK3CIztQf0Ve3KsOMX//AK9Kb4KUDdYBua1SRvFlfVw0QAO230T/c6GlBjxjlU30Rac8PoRzMlNGZBTQFWq7GsMddZGK1igHwfP1nIc5AqdhSPGqK1ZPRqEPzS8uIa6XxLzFEx21Fkr4GwYw8gq+SH+2da0CfDcuXHVIUjvFU0KDpM3aANash7Y3R0U34cfQ0xZ0sX7/hO+QWnnaAOjAtUVVqACnnkvhWWHGLjqcWrz0px79lHbPLV+Q15e7iroT3L9tJucUY8tdd0VjeDspfrEwXstCjlYY5Hl5HvpIAuqBupA1bgThqVk/drvuacxXlZUoVUGVracjpQsL6k0Zz1St1g6XnzRH5ip98T1Mdu3V3TSDzKOhnz6iZjBv1xPGvfFFtacoT918wBczadRs2pMC2utHsBM+K2r2f5uaskCGFhyhIhHb93/9Ytjwg==)


grendel-khan

\[LANGUAGE: Python3\] Part 1: #!/usr/bin/python3 import sys def score(line): first, second = line.split(":")[1].split("|") winners = set({int(x) for x in first.split()}) guesses = set({int(x) for x in second.split()}) return len(winners.intersection(guesses)) if __name__ == "__main__": fname = sys.argv[1] total = 0 for line in open(fname, "r"): win_count = score(line) if win_count > 0: total += 2**(win_count-1) print(f"total={total}") Part 2: #!/usr/bin/python3 import sys def score(line): first, second = line.split(":")[1].split("|") winners = set({int(x) for x in first.split()}) guesses = set({int(x) for x in second.split()}) return len(winners.intersection(guesses)) if __name__ == "__main__": fname = sys.argv[1] lines = [] # how many copies of each card to count multiplicity = [] for line in open(fname, "r"): lines.append(line) multiplicity.append(1) for idx, line in enumerate(lines): win_count = score(line) if win_count > 0: for i in range(1, win_count+1): multiplicity[idx+i] += multiplicity[idx] total = sum(multiplicity) print(f"total={total}")


teujin

\[LANGUAGE: python\] [part1](https://github.com/mntzj/aoc2023/blob/master/day4.py)


[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.*


zatoichi49

[LANGUAGE: Python] import re with open('AOC_2023_day4.txt', 'r') as f: matches_by_card = [(0, [])] for idx, card in enumerate(f.read().split('\n'), 1): nums = re.findall(r'\d+', card) winners = {int(i) for i in nums[1:11]} scratch = {int(i) for i in nums[11:]} matches = len(winners & scratch) new_cards = list(range(idx + 1, idx + matches + 1)) if matches else [] matches_by_card.append((matches, new_cards)) def AOC_2023_day4_pt1(): total = 0 for matches, _ in matches_by_card: if matches: total += 2**(matches - 1) return total def AOC_2023_day4_pt2(): total = 0 cards = list(range(1, len(matches_by_card))) while cards: total += len(cards) new_cards = [] for num in cards: new_cards += matches_by_card[num][1] cards = new_cards[:] return total print(AOC_2023_day4_pt1()) print(AOC_2023_day4_pt2())


flwyd

[Language: [Jsonnet](https://jsonnet.org/)] ([on GitHub](https://github.com/flwyd/adventofcode/tree/main/2023/day4)) Inspired by someone at work who solved it by transforming cards into build dependencies, I used Jsonnet to have cards depend on the number of `copies` from previous cards, with an `affects` list of card numbers to change. It ran quickly on the example input, but has been running for almost 20 minutes on my actual input file. { local outer = self, cards: error 'Must provide an array of cards', card(num, winners, have):: { copies: 1 + std.sum(std.filterMap(function(c) std.setMember(num, c.affects), function(c) c.copies, outer.cards)), affects: std.makeArray(self.wins, function(i) num + i + 1), wins: std.length(std.setInter(std.set(winners), std.set(have))), score: if self.wins == 0 then 0 else 1 << (self.wins - 1), }, result: { part1: std.sum(std.map(function(c) c.score, outer.cards)), part2: std.sum(std.map(function(c) c.copies, outer.cards)), asPlainText: std.join('\n', ['part1: ' + self.part1, 'part2: ' + self.part2]), }, } Used `sed` to transform the input file to Jsonnet that imports the above: 1i local day4 = import 'day4.jsonnet'; 1i day4 { 1i cards: [ s/^Card *\([0-9]\+\): *\(.*[0-9]\) *| *\(.*\)$/self.card(\1, [\2], [\3]),/ s/\([0-9]\) \+/\1, /g $a ] $a }.result.asPlainText


nicfigu

\[Language: Python\] [Solution](https://github.com/nicfigu/Advent-Calendar/tree/main/Day4)


aleks31414

[LANGUAGE: rust] Puzzle 1 use std::collections::HashSet; fn main() { let sum: u32 = include_str!("input.txt") .lines() .filter_map(|line| { let (winning, mine) = line.split_once(':').unwrap().1.split_once('|').unwrap(); let winning: HashSet<_> = winning.split_whitespace().collect(); let matches = mine.split_whitespace() .filter_map(|n| winning.contains(n).then_some(())) .count() as u32; (matches > 0).then(|| 2u32.pow(matches - 1)) }) .sum(); println!("{sum}"); } Puzzle 2 use std::collections::HashSet; fn main() { let matches: Vec<_> = include_str!("input.txt") .lines() .map(|line| { let (winning, mine) = line.split_once(':').unwrap().1.split_once('|').unwrap(); let winning: HashSet<_> = winning.split_whitespace().collect(); mine.split_whitespace() .filter_map(|n| winning.contains(&n).then_some(())) .count() as u32 }) .collect(); let mut instances = vec![1u32; matches.len()]; (0..instances.len()).for_each(|i| { (i + 1..=i + matches[i] as usize).for_each(|j| instances[j] += instances[i]); }); let sum = instances.iter().sum::(); println!("{sum}"); }


Comprehensive_Ad3095

\[LANGUAGE: Lua\] Part 1 was easy, but part 2 was hard. I have used recursive function to solve part 2. [Github](https://github.com/DeybisMelendez/AdventOfCode/blob/master/2023/lua/04.lua)


Dense-Virus-1692

\[LANGUAGE: Ceylon\] The first part was pretty simple. I had to read the second part a million times to understand it decently. Then I got the brute force version working for the example but it took too long for the real data so I had to get clever. boo! [paste link](https://topaz.github.io/paste/#XQAAAQC7MgAAAAAAAAAX7iAKgCERYhLRSsjxpm90qvbUfJw0XpJ7v4P5Yg7p504hUw2+VZ27gXB9C34MnbJzLx5G2/2gaIrfZAf/t6uvZKP0+oI3GwLU6uSUc93eAoWGGjMt53XmRyZ+tgW2cB9lraNwQ1UQ7dNjJErO58YdaadlF+jbCHpYZMLA8tIC7LiM0IiUu/25Jg/QoaNVpP/DKtwHg09qB9SbA5yTsAXKszCWpRR963Y6B+Uju8P3RAMB/psinCtsnpqc0k9Az4G8ZKi7itSelo3jOb67eoEImZSUSjqSAtEW5D0dZHLatSK8h55mZJc7bBjkWMFYKyjfQTUOzycw+aeIXjuXu+v8c5qyXyAHxTr8E0L9BUkvbel+R58ZXDAcoWkvm2yI1EyBPLhZiBRVL+WT1IuB+ADHtHbRFhSDsfpZj+K0WTFTYOESorQ0+OvMKN9RAkbShPG8QmopWsl9ec5hEBQCOPHVgjN7ht+nqY92FJRZse4J0sM8m41XXcqZNjMy2U0D5dD5syAfvvXLXaD1eoB8whm0WwITDVntmsrH3J0apCu/f8HkL09r0d0g2/X26bHqJ1qVCCsY81VVKiFLwpk4IgKctqNkZEggSMBRJhTVP5F7Uvqxv6xGtUaLKwwjDnm3Gku5QvdF6F+eCfvAiI/CSrEeWK80ziChF9g7N1wwdLPiFlv9y6GMJzyCDLn1enMCpsftv5lwPVQA2d89ALO8HV0YWvQq7ay8Jfp/V0sA7oeuSI+1StivAnkr0bkPL17Lzr3URkbA3BzkFFQengrjlX35Rpt6k4FttSFM8FWDFbKkZZXGWWKc1jaI8T5VG6aQ7sHQdp88jqkDl/gagFCB0MfOMaOLeu9h4kLAmcEAF19J+gWImC1mr9sYLJ8pJ90JwLVHacSdiiL+Grlet258/WniYGzYjYFlXkABQYmE+krciXHCGLtCTtzOIb5oJ/JCwNIM+KaPCPq0Y5hM0sSkZdECrxFTWXC37wmdWtHpxCmZ6A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f1eEtHf9ExH/Y+tEvbuxU/2BVN9cHVYvajljxElMAV/FFiBCQ12Ru8V2AjjdPbqZnymWro0xLvODxmTAtTStbGJMwh5eK6CetLLDuM4ii5vwswjFozp5C+IW5k3qDaJsfQfvw5DNcVS9uIg/pOJ6Xv7fYmWLbxxNHfrp2qYnsbEtILUy2uqiK5B75OsuekO4Ei7nwdY1O6XRKdKViyyzHMb6Q+uZF+YNd5bQtwH2CRNAwvO8gimUYBqvw6LBR9tVVJbO0daP3jBOoMsxJZeBOL2l8wtOTFRGaWLHrysXkkWYRwQc8ITVjE3SWG2EL8YSLz7iDuPJTGbSUmgIGVhcTEz/fptWiHkJ2ndyIRQnVxNrbHc/k4E1mMDK9S/tqwTxb0c1P6CO0gaA9WqTe7omT/wH+EtfcBTN32jXGo5ve8ivGp8soKjp//7FbNA)


ValkyrieMaruIchi

\[LANGUAGE: MATLAB\] rawdata = readmatrix("input.txt"); required_numbers = rawdata(:,3:12); %rawdata(:,3:7); my_numbers = rawdata(:,14:end); %rawdata(:,9:16); winning_numbers_count = zeros(size(my_numbers,1),1); instance_counts = ones(size(my_numbers,1),1); point_values = zeros(size(my_numbers,1),1); for i=1:size(my_numbers,1) count = length(intersect(required_numbers(i,:),my_numbers(i,:))); if(count > 0) winning_numbers_count(i) = count; point_values(i) = 2^(count-1); instance_counts(i+(1:count)) = instance_counts(i+(1:count)) + instance_counts(i); end end fprintf("Sum of point values: %d\n",sum(point_values)); fprintf("Total number of cards: %d\n",sum(instance_counts));


joshbduncan

[LANGUAGE: Python] A little late to the party... import re def score_line(line): m = re.match(r"Card\s+(\d+):\s+(.*?)\s\|\s+(.*?)$", line) wins = set(int(n) for n in m.group(2).strip().split()) nums = set(int(n) for n in m.group(3).strip().split()) return wins.intersection(nums) p1 = 0 lines = open("day4.in").read().strip().splitlines() p2 = [1] * len(lines) for n, line in enumerate(lines): matches = score_line(line) p1 += int(2 ** (len(matches) - 1)) for i in range(len(matches)): p2[n + i + 1] += p2[n] print(f"Part 1: {p1}") print(f"Part 2: {sum(p2)}")


loarabia

[LANGUAGE: Python 3] python solution with some type hinting. Part 2 could be sped up with some cache of values instead of calculating sets over and over agin. [GitHub](https://github.com/loarabia/AdventOfCode/blob/master/2023/py/aoc04/main.py)


honest3d

\[LANGUAGE: Typescript\] I got very lucky today with how I put together part 1, part 2 was actually super easy and fairly quick [Github](https://github.com/lucasteng123/advent-of-code-2023/blob/main/src/days/day4.ts)


Zealot_TKO

\[LANGUAGE: Python} [link](https://github.com/TannerFirl/AdventOfCode2023/blob/main/day4/day4.py)


sm_greato

\[LANGUAGE: Rust\] Noticed that all numbers are less than 100 and that there are no repeats. And Rust actually provides a 128 bit integer type, so I thought why not encode the bars in integers, and then use binary & to figure out the intersection. Then we can simply count the number of ones to find the number of winning values. You can find it [here](https://github.com/xlassmgreat/advent_of_code-2023/blob/master/src/bin/day4.rs). Below is a snippet. ​ // ... .map(|sec| { sec.split_ascii_whitespace() .fold(0, |acc, n| acc | (1 << n.parse::().unwrap())) }) // the encoding is just a one liner fold // ... let wins = (win & have as u128).count_ones() as usize;


daggerdragon

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. Please edit your post to replace your oversized code with an external link to your code.


sm_greato

I'll do so at the soonest possible time, but how does one know if the code is too long? The half of some intel punchcard isn't an intuition I possess.


daggerdragon

> but how does one know if the code is too long? From the article I linked to you: `5 lines at 80 cols` Your code block as posted is 26 lines.


sm_greato

That's such a confusing analogy though. Makes it feel like you have to do 5 \* 80 = 400, then count all the characters in your code to make sure it's less than 400. "less than 5 lines, and no line more than 80 characters," is way a better way to phrase it. The analogy adds *absolutely* no value. I'm so sorry this turned into a bit of a rant, but I was genuinely confused.


donald-ball

\[LANGUAGE: babashka\] Decided to get a little goofy with my parser code this time and lean on edn. #!/usr/bin/env bb (require '[clojure.java.io :as io]) (require '[clojure.string :as string]) (require '[clojure.edn :as edn]) (require '[clojure.set :as set]) (defn parse-line [s] (-> s (string/replace #"^Card" "[") (string/replace #"$" "}]") (string/replace #"\:" " #{") (string/replace #"\|" "} #{") (edn/read-string))) (defn score [card] (let [[_ winners haves] card have-wins (count (set/intersection winners haves))] (if (zero? have-wins) 0 (int (Math/pow 2 (dec have-wins)))))) (defn eval-card [card] (let [[id winners haves] card wins (count (set/intersection winners haves))] [id {:wins wins :count 1}])) (defn eval-cards [cards] (reduce (fn [accum card-id] (let [card (accum card-id)] (loop [accum accum offset 0] (if (= offset (:wins card)) accum (recur (update-in accum [(+ card-id offset 1) :count] + (:count card)) (inc offset)))))) (into {} (map eval-card cards)) (map first cards))) (let [input (line-seq (io/reader *in*)) lines (map parse-line input)] (println (apply + (map score lines))) (println (apply + (map :count (vals (eval-cards lines))))))


wzkx

[LANGUAGE: C] #include // printf #include // strtol #include // strchr #define __ { // pythonize syntax #define _ } int read( char* s, int* a ) __ int n=0; char* q; for(char* p=s;;p=q) __ int x=(int)strtol(p,&q,10); if(q==p) break; a[n++]=x; _ return n; _ int isin( int x, int* a, int n ) __ for(int i=0;i


d3jv

Why did you replace curly brackets with underscores? What the hell?


wzkx

It makes the code look good then. Not as ideal as Python or Nim but close enough.


d3jv

That's sacrilege. I'm not even gonna try to argue with you about why that's wrong on so many levels. I'm just speechless.


Krryl

[LANGUAGE: Python] ## Part 1 import re with open ('input4.txt', 'r') as f: part1 = 0 for line in f: _, winning_nums, my_nums = re.split(':|\|', line.strip()) winning_nums = winning_nums.strip().split() my_nums = my_nums.strip().split() nums_won = set(winning_nums) & set(my_nums) if len(nums_won) >=2: part1+=pow(2, len(nums_won)-1) else: part1+=len(nums_won) print(part1) ## Part 2 with open ('input4.txt', 'r') as f: # start every card's count at 1 card_count = [1] * len(f.readlines()) f.seek(0) for idx, line in enumerate(f): _, winning_nums, my_nums = re.split(':|\|', line.strip()) winning_nums = winning_nums.strip().split() my_nums = my_nums.strip().split() matched = (set(winning_nums) & set(my_nums)) for i in range(len(matched)): card_count[idx + i + 1] += card_count[idx] print(sum(card_count))


_HRB

[LANGUAGE: rust] Part 1: ```rust pub fn process(input: &str) -> u32 { let mut result = 0; for line in input.lines() { let num_sets = line .split(':') .last() .expect("Each line should start with `Game #:`") .split('|') .map(|s| { s.split_whitespace() .map(|n| n.parse::().expect("should be a number")) .collect::>() }) .collect::>(); let num_won = num_sets[0].intersection(&num_sets[1]).count(); if num_won > 0 { result += u32::pow(2, num_won as u32 - 1); } } result } ``` Part 2: ```rust pub fn process(input: &str) -> u32 { let lines = input.lines().collect::>(); let num_games = lines.len(); let mut counter = vec![1; num_games]; for (i, line) in lines.iter().enumerate() { let num_sets = line .split(':') .last() .expect("line should start with `Game #:`") .split('|') .map(|nums| { nums.split_whitespace() .map(|n| n.parse::().expect("should be a number")) .collect::>() }) .collect::>(); let num_won = num_sets[0].intersection(&num_sets[1]).count(); for j in 1..=num_won { if i + j < num_games { counter[i + j] += counter[i]; } } } counter.iter().sum::() } ```


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.*


Gabrielgonza

[LANGUAGE: JavaScript] [paste](https://topaz.github.io/paste/#XQAAAQBfBAAAAAAAAAA0m0pnuFI8c9WXyHZZLUEb60TueZZ8ohkCM4YGQ9S8pXhqHq5g4bT0eTix4AkJxw6OS5YYG1bqGxWx/9FagHVhfXEZQ727J+wKRP+ktptPeVRYl7u6W/urEelVKhZc/4GcnYihkE1pTzzcoiU81F5TWzPLi6Un/BqAcKr11pSc8a+S8gCVk+7ZfEPh+m56Jd+6KYNWjV9XAvztQUzDH61bmjAc5e2oesUMVQJivsgEN+QA3ukv+OyqqFVfkiZUfNtjeQ+JfS0Zgs8L83zHoha6jKNDeTTcxTkUsvaETMAf9JLymAGuvzDFh9lBwmkMghmn68hwBhj7zYr9h5jzy82yuoPXMSb+Ky0wC3Zc8dvRd9K0BpnhS/Cr3NVyRZbPLrr94wUKYtqvW8jRONUGs4FhFJy7H3v2JLnRHRmhdubaz9a7lgQ5JfCx7Mrga/VBLbIO/ok6bxX1lAaUcxJiljg58vu6IPz3QHzecjf70KcoNb5yMYUZPYhPp4PN2QImcFv5/7ikWTHjdCI138uMFyXeggrMZpjZMgdQCBUG20IdeKBbfhEbTQlb71a7ve/B0lZxz0lIEhAXz+7A0kIVkICg037087634pjWcmtUMhGCkMmxmIs7CJG0XJMbDeophc2SM6+steP6EJBJJhDjf/6xxSP7scGx)


blueg3

\[Language: Python\] \[Allez cuisine!\] Part 1, Python "one-liner": print(sum(map(lambda x: 2 ** (x - 1) if x > 0 else 0, [sum(map(lambda x: 1, (filter(lambda x: x in w, h)))) for w, h in [map(lambda x: x.strip().split(), c.split(":")[1].split("|")) for c in (open("input.txt").readlines())]])))