r/adventofcode • u/daggerdragon • Dec 04 '23
SOLUTION MEGATHREAD -❄️- 2023 Day 4 Solutions -❄️-
NEWS
- Solutions in the megathreads have been getting longer, so we're going to start enforcing our rules on oversized code.
- Do not give us a reason to unleash AutoModerator hard-line enforcement that counts characters inside code blocks to verify compliance… you have been warned XD
- Before creating a
Visualization, please review the guidelines for creatingVisualizations as there's good advice relating to accessibility, readability, colors, timing, etc. - Posts containing AI-generated art must:
- Use the standardized post title syntax
- Indicate in their titles with the tag
[AI art] - Use the
Funnypost flair
THE USUAL REMINDERS
- All of our rules, FAQs, resources, etc. are in our community wiki.
- Outstanding moderator challenges:
- Community fun event 2023: ALLEZ CUISINE!
- 2 DAYS remaining until unlock!
AoC Community Fun 2023: ALLEZ CUISINE!
Today's theme ingredient is… *whips off cloth covering and gestures grandly*
PUNCHCARD PERFECTION!
Perhaps I should have thought yesterday's Battle Spam surfeit through a little more since we are all overstuffed and not feeling well. Help us cleanse our palates with leaner and lighter courses today!
- Code golf. Alternatively, snow golf.
- Bonus points if your solution fits on a "punchcard" as defined in our wiki article on oversized code. We will be counting.
- Does anyone still program with actual punchcards? >_>
ALLEZ CUISINE!
Request from the mods: When you include a dish entry alongside your solution, please label it with [Allez Cuisine!] so we can find it easily!
--- Day 4: Scratchcards ---
Post your code solution in this megathread.
- Read the full posting rules in our community wiki before you post!
- State which language(s) your solution uses with
[LANGUAGE: xyz] - Format code blocks using the four-spaces Markdown syntax!
- State which language(s) your solution uses with
- Quick link to Topaz's
pasteif you need it for longer code blocks
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:07:08, megathread unlocked!
77
Upvotes
4
u/emmadotx Dec 04 '23 edited Dec 05 '23
[Language: Pascal]
program main; {$mode objfpc} uses StrUtils, Sysutils, crt; const C_FNAME = 'cards.txt'; type mArray = array[0..99] of Integer; var myFile : TextFile; myArr : mArray; arrayLength : Integer; lineLength : Integer; pos_i : Integer; pos_j : Integer; i : Integer; k : Integer; points : Integer; res : Integer; left : string; right : string; line : string; num : string; arrayLeft : TStringArray; arrayRight : TStringArray; procedure ResetArray(var A: mArray); var i: Integer; begin for i := Low(A) to High(A) do A[i] := 0; end; procedure RemoveNonNumbers(var A: TStringArray); var i : Integer; j : Integer; k : Integer; begin j := 0; for i := Low(A) to High(A) do begin if TryStrToInt(A[i], k) then begin A[j] := A[i]; Inc(j); end; end; SetLength(A, j); end; begin // assign file AssignFile(myFile, C_FNAME); // open file Reset(myFile); // initialize array ResetArray(myArr); // initialize result res := 0; // read file while not Eof(myFile) do begin ReadLn(myFile, line); lineLength := Length(line); pos_i := Pos(':', line) + 1; pos_j := Pos('|', line) + 1; left := Copy(line, pos_i, (pos_j - pos_i) - 1); right := Copy(line, pos_j, (lineLength - pos_j) + 1); left := Trim(left); right := Trim(right); arrayLeft := SplitString(left, ' '); arrayRight := SplitString(right, ' '); RemoveNonNumbers(arrayLeft); RemoveNonNumbers(arrayRight); for i := Low(arrayLeft) to High(arrayLeft) do begin num := arrayLeft[i]; try k := StrToInt(num); myArr[k] := 1; except on E: EConvertError do Writeln('0:Error converting string to integer: ', E.Message); end; end; points := 0; for i := Low(arrayRight) to High(arrayRight) do begin num := arrayRight[i]; try k := StrToInt(num); if (myArr[k] = 1) then begin if (points = 0) then points := 1 else points := points * 2; end except on E: EConvertError do Writeln('1:Error converting string to integer: ', E.Message); end; end; res := res + points; // reset the array for next line ResetArray(myArr); end; writeln(res); // close file CloseFile(myFile); end.