From 50165e756dec9ca05130e84c03cca7549d5e3ed6 Mon Sep 17 00:00:00 2001 From: Fennel Kora Date: Mon, 5 Dec 2022 15:50:55 -0500 Subject: [PATCH] add binaries and code for 2022 Day 5 Changes to be committed: new file: 2022/day05/Day05A.class new file: 2022/day05/Day05A.java new file: 2022/day05/Day05B.class new file: 2022/day05/Day05B.java new file: 2022/day05/day05_algo.txt --- 2022/day05/Day05A.class | Bin 0 -> 2163 bytes 2022/day05/Day05A.java | 47 ++++++++++++++++++++++++++++++++++ 2022/day05/Day05B.class | Bin 0 -> 2233 bytes 2022/day05/Day05B.java | 52 ++++++++++++++++++++++++++++++++++++++ 2022/day05/day05_algo.txt | 24 ++++++++++++++++++ 5 files changed, 123 insertions(+) create mode 100644 2022/day05/Day05A.class create mode 100644 2022/day05/Day05A.java create mode 100644 2022/day05/Day05B.class create mode 100644 2022/day05/Day05B.java create mode 100644 2022/day05/day05_algo.txt diff --git a/2022/day05/Day05A.class b/2022/day05/Day05A.class new file mode 100644 index 0000000000000000000000000000000000000000..369ec86e61c5cd40389c3e677ba39383c72caff0 GIT binary patch literal 2163 zcmZ`)+jA3D9R5zS=}EU+3Wb!Gn^bNoAq}9QHi(u|G_@D7wOHzHn{L~MbhBnR(BfV3 z`XA_n4~#Q?1s_6(qB7%~4~`E$^5kX48K0dIW}tp&la>@}9&*n4o!|BQeZR9m-Mjuh zfT!_=ju65Unt=#v1lFE4&zq^7=}e{u#?D$LKO7^wYK9H{dRxGmuOfKXwWa%&ZLfYXp~rQU;|=H zF&sOeny_ZIvUc}!u_Vn*qB@BK8(*6GoQMQvr#3Lj}5IyS0en*}yj{H)Yy8&*1- z3~a_0fv986_`SAc32ca0JO=Po;_6s~Z3Z5}b^&R7-P5i=8$fkN;!%NyDlmLAdrn6y z!pdfsf!)|cbaR$7=}!eF8FkTSU@sE9$WEDsPFAbMGi_Zul4zGm8Q6~lOXvi7bWzRB z`c{GQ&zrfTH83I29Pg^mbyyG4(IcUPlXZhE@fxFS*we!{?$c#652 zW}8x7cGU_DFZJm&zfslY-10G47`M4R%5!Zgc4S+1oLC|!$}F(Prs zz$ji>IS;d*Z%wm?e35X($}Hv}oaC7JbYhv)?Bi>AUE&P`CdQWduu_M7aFs>Q`JYw= zJRdh;slktAb9v8_m|SuekZWkFkiTG#<%qxpYy)R;PN1IMA1u0V!SXzdbvIYny=;+I zj|HnJKNd_aGtfPgwOrL`9XaF;xGHc%kmR5mjv*4+ih@iQ+tn!e28uY(+&njD6N)|6 zftH!^@Zjs1!K}nZH7su}JxD=48OOIK)u^oMs>)MEkvPxb9Ru$wDntq9u$nNEpesMl zr9nLm{l)1qt1zTc;>EOS6Yr+@YVQ%rQXdrBXS#vD#K!`m(Tv2W1Zou%0evuEEM%<{ zd;|sR%7fFc$V*Bd<=d@(VGtwK@c%uYwsI9*BYUqwT;)dKeg3O!kl3~O03UKs)ldi; z<-olK$dPNP?OlLA(rARg!N?CrjA+6^<;Rl}bB0wD93bEJESV%V=)V zlCek$@p(}7;OR>dF}L_kH^%2E7#zi4yuQV{?m^P#Gxu5>A#dSWe=%Z*f|s@hZb+ z=Md{JuXP<-Qi-RMKg=Pj>T|9Vz!u!X@8tL&_zQn?jp85tOO}QL>O;0-l-feiQ70Bx{ErA9?G{b zG7)}-KPj6)NK7oU=i0J}fW@ r9v4b@dtgLM)GY5KiRjY4Z{HOfm1)NK+$R`xA5#2i34F$L2%rB4QPm6D literal 0 HcmV?d00001 diff --git a/2022/day05/Day05A.java b/2022/day05/Day05A.java new file mode 100644 index 0000000..444bb54 --- /dev/null +++ b/2022/day05/Day05A.java @@ -0,0 +1,47 @@ +import java.nio.file.Paths; +import java.util.Scanner; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Stack; + +public class Day05A { + public static void main (String[] args) { + ArrayList> cargoList = new ArrayList>(); + try (Scanner inputFile = new Scanner(Paths.get("input.txt"))) { + String cargoLine = inputFile.nextLine(); + while (!cargoLine.isEmpty()) { + Stack tempStack = new Stack(); + for (int i = 0; i < cargoLine.length(); i++) { + tempStack.push(cargoLine.charAt(i)); + } + cargoList.add(tempStack); + cargoLine = inputFile.nextLine(); + } + + while (inputFile.hasNext()) { + int[] moveInstruction = getInstructionList(inputFile.nextLine()); + for (int i = 0; i < moveInstruction[0]; i++) { + cargoList.get(moveInstruction[2]-1).push(cargoList.get(moveInstruction[1]-1).pop()); + } + } + + StringBuilder topCrates = new StringBuilder(); + for (int i = 0; i < cargoList.size(); i++) { + topCrates.append(cargoList.get(i).peek()); + } + + System.out.println(topCrates); + + } catch (IOException e) { + e.printStackTrace(); + } + + + } + + public static int[] getInstructionList (String s) { + String[] instruction = s.split(" "); + int[] numericInstructions = {Integer.valueOf(instruction[1]), Integer.valueOf(instruction[3]), Integer.valueOf(instruction[5])}; + return numericInstructions; + } +} diff --git a/2022/day05/Day05B.class b/2022/day05/Day05B.class new file mode 100644 index 0000000000000000000000000000000000000000..566d7e42d4e2e0b0c0b5a33d945de8c870affc4b GIT binary patch literal 2233 zcmZ`)TW}Lq82(PPo0D$0Efmr=6leniNkbbzsoDSv1vJ{UU~93|+csU2g>a^wk-iamII_oPily|FfHf6lxxF&i?1We*gEM^XtmD zKL9w4&lEHuz#*xS5oFkKUc0Cz3!0frj!vJ~vo1sMkYO6`5r#l4KEV-U*yJgdT%(XY zYTMfUu;I80xbV}&u<_pMSXMJl-43Cdq0um{lH2djxg4!)0*$%0VdfNUL>tE@6`K*R z#xRXy@|;o7lczK{?@$Ukoia+rhVP~6Tc@TT6vchf8XqYI3OYowoeUinKWjDO6GCT; ziY{zr2$}kvJ8YOb!{%7UqX$nVu7Vb9SJ90g25vYfW-WK#gX%2D4u;k`FkCHrK|vG& zVY5rcZtNj+3%Z$e^PWjs9K==hB0&e)yk;MzYNc2@KBS-z{TxXZ`>?-?j+e&}scBhP zw<-Qbtx(cO&oOkyhU#;z=}`GmJgnjo93;n9$)Uiz>(TU^o~k#lh(v=QRq+@er(Cs} z8HVtBhGzwMDHVr2K=PV1Lii~-f+skhRB;r?sI=qS{Jw*OD$EOGzj^lzHCq(4*x*10Cl!~X3rVOp3b$`cdg+%eJieZtUtc!kej1U>_r_`~M zQJB$f1sTyXj?*fh6CINr`7+JZ~2-Y10Klz(7I8EXY|i@nNiFS+?#t zI@R4-Uw6$SC9V(ZN-eeY=!v>o6T`Evt2$2=MZ)S;B^!XwU2|BQnxg# z19Dg<;X@1Pj7WW9xs2EnNcF&(YcgA0{W}qn${0A~885E>MC;%e$cfJ-93GUaj^w~V zFdU3Xf&K;Th{$9m^-D8l4BbMsjFY!OG00_*T>c)RAkSP!Hz~0I#f+lQ-id|?%Khby)tw_^4N^jl- z+Gy@K(aeXTqa7wXa0#8{BZ4n!UBFg+MJ@c9{QW{L{tY|u2X^8gTK}atWCbzGLSY@~ zrRXu%g*fY`*W!JOp2vn2Z0Fz{3P;Ob@xVse9c;UUmKAhyeBgiYxs9C-chSU=q0^pK zN{!y~k9> cargoList = new ArrayList>(); + try (Scanner inputFile = new Scanner(Paths.get("input.txt"))) { + String cargoLine = inputFile.nextLine(); + while (!cargoLine.isEmpty()) { + Stack tempStack = new Stack(); + for (int i = 0; i < cargoLine.length(); i++) { + tempStack.push(cargoLine.charAt(i)); + } + cargoList.add(tempStack); + cargoLine = inputFile.nextLine(); + } + + while (inputFile.hasNext()) { + int[] moveInstruction = getInstructionList(inputFile.nextLine()); + Stack tempStack = new Stack(); + for (int i = 0; i < moveInstruction[0]; i++) { + tempStack.push(cargoList.get(moveInstruction[1]-1).pop()); + } + + while (!tempStack.empty()) { + cargoList.get(moveInstruction[2]-1).push(tempStack.pop()); + } + } + + StringBuilder topCrates = new StringBuilder(); + for (int i = 0; i < cargoList.size(); i++) { + topCrates.append(cargoList.get(i).peek()); + } + + System.out.println(topCrates); + + } catch (IOException e) { + e.printStackTrace(); + } + + + } + + public static int[] getInstructionList (String s) { + String[] instruction = s.split(" "); + int[] numericInstructions = {Integer.valueOf(instruction[1]), Integer.valueOf(instruction[3]), Integer.valueOf(instruction[5])}; + return numericInstructions; + } +} diff --git a/2022/day05/day05_algo.txt b/2022/day05/day05_algo.txt new file mode 100644 index 0000000..6ece2f2 --- /dev/null +++ b/2022/day05/day05_algo.txt @@ -0,0 +1,24 @@ +Hand-reformatted initial state input. Andy can't tell me how to live my life. + +=====Reading input state into 2D array===== +Open file +Loop - for incrementer i = 0, while line read in is not empty, i++ + Inner Loop to iterate through read string + push char to stack + End inner loop + Add now complete stack to list +End Loop + +=====processing move instructions===== +Outer Loop - while file has contents + read line into split string, array of 3 ints, position 1 is quantity, position 2 is source stack, position 3 is destination stack. (moveInstruction) + inner loop - for incrementer i = 0, while i < moveInstruction[0], i++ + push to cargoList[moveInstruction[2]] char that is popped from cargoList[moveInstruction[1]] + End inner loop +End outer loop + +loop - for incrementer i = 0, while i is less than the first dimension of the two dimensional array, i++ + peek top crate of each stack, assign to string topCratesList +end loop + +print topCratesList to screen \ No newline at end of file