diff --git a/Makefile b/Makefile index 455df21..5ad5543 100644 --- a/Makefile +++ b/Makefile @@ -18,5 +18,16 @@ day2part2: @echo ./build/day2part2 +day3part1: + fpc src/day3/part1.pas -obuild/day3part1 + @echo + ./build/day3part1 + +day3part2: + fpc src/day3/part2.pas -obuild/day3part2 + @echo + ./build/day3part2 + + clean: rm build/* \ No newline at end of file diff --git a/readme.md b/readme.md index 666027e..89619b1 100644 --- a/readme.md +++ b/readme.md @@ -21,3 +21,11 @@ and `Y` is the part the compiled stuff is in build ig +# python one liners + +there's python one liners in pythonsrc. + +just run em. there's no libraries. + +thankies. + diff --git a/resources/day3.txt b/resources/day3.txt new file mode 100644 index 0000000..9191bee --- /dev/null +++ b/resources/day3.txt @@ -0,0 +1,140 @@ +........936..672.........846.922........359...332......582..856........................579..93......674..740.....243.156.................... +...........%.........4=...*...*........*.......*......#....................806..481.........................*.......*.........900......$564. +.............520........624.965....143..405.....960.............273...651...*....*.........554....139@.....38...*.........58..*...392....... +................$....................*.........................*.....&......634.3..../.................-......310....*313.*.........*....... +...196....544......541................775.216...+557..225/..463.......................517..........960.228........452.....593.......772..... +.....*....*...654.*........638............*...........................163....................386#......................20......169.......... +.....566..20...$...132........*969......747........*971..342............=.245*.......................277.522............*..............+.... +.............+..............................205.716...........................855.............324......................871..752.........291. +.........47..390.......128...%....528........%..........91*192.......=.....................=.#.............%..852*156......+................ +.....975..*.......=....*...322.......#............&63.............946...................462....939..520...268............#.....*....380..... +.........55..=.....871..68.............................................491.......................*.....*............674.770.333.415...*.275. +..............633...........@.....642.5........+.......739........632.....*.......992.74.615...858...998.............*..............761.*... +....446......................180./..../........83...&.....*924....&........379...*......................................................118. +...........488*240.......150......................958..........$.....&...........964...351...........+..633.........123....../603........... +...................987....../........546-...674...............466.....602.............@...........956..#............./...................... +......503=....465.................91.........*..........................................583..............128..124..........$.....217........ +..............*...32$..53....&594.........648...827....*..994.......971...................*..........304...%....*.......387..80............. +............756.......#..........................*..888...$............@....339.......+..868........*........851..............*.........128. +...905-.........312..................729...356*.697.........736.....+.......+.......748.......797....41....................272..753......... +.................*....&604............*.............%915.......*.529..889...................*.........................992.........*.....718. +...........272....487..............+...961.892..............632........*.................502.118....141................@......546.669....... +787..........*....................85............/..330...........-...374.............................*.........446...%.........*............ +....-...../...120....919&.....................339....*....*663...403........167...533.170.............698..587...*....279.......776.+....... +.575.....34.*.................668...19..759.......804..730...../.............*...........*.....789...........*....803......666.......25..... +............375...773*.............*.........................$.876...287......201........484......*.........196............................. +565...866.............23..477...455....................794.996.........*.....................887.959...146...........496....790............. +..............648...........+........325.738....156.....*.........534..941.....%536..31..../.%...........*............*.....*.......511..... +..............*.................166.....*......*.....647..........*......................262...........887..210.3..860.....161....4....*420. +......141.606...........193.111..*..........252......................935*689..................500...........*...*.................*......... +..635....*........=........*.....959.....................@834.................-.....542..547....-..........770.8...........439.............. +.................896.........93......................244..........@......247..735..*.................................497.............%...... +...............................%........................*..........200....+.......342.613.245.@............$.........#....*.......176...21.. +......51......702.37.......798....325.....&974.....834.860.....+.........................*.....533..866....864.............269.............. +..141..*......................-...&............875*.............207...882*.........................*............309.739........%......../... +.......394....144.....................................$...................835.3..........290.......489...........&.....*177.833....866$.153. +.689.................719.314/..$......673.....5*557...443......+..#649.............575....*..634*........................................... +....&...........794...*........614...*......................350.............411......*.104.......141........369...934...........997......... +.......519......*...787..............739.....628...................................784.......$..........989*........*...987.......*....#.... +.........*...309.........209.............957*........50.....@995...765.......269.@........380..624.................643.....*....836...356... +......886............234.+....................51*935..*...........*.....188.......73..............*696...14.....#........................... +............203..948...&...............................957......804.736*......388....689.219*660........*.....218....................$..40.. +..............*.....*.....475.....930..........%................................*......*..............477.624................848...687...... +....57*.50.229....636....*.................804.164....590.........-....547..80..310....675.......=.@...........................*............ +........................398......&........*..............*.....748...........*................226..200................160......853.......... +......292........................889.......867.....365...............192+..530.........................@910....=..222*.............894...... +..427....#.../...959........................................322.....................735*304....742..........195..........$..............22.. +...........465..-.........52.$......168.......418.......+.........................................$..............&......563...649*26........ +....................464..../.969....*......-..*........150......724.....83..301...............527...............858.506..................... +...935.........910....+...........116....427...347.............-........*.....=.......891.......*....847...750........@................531.. +....*.........$.........676...............................510............11.......647.*.................#.....%.683.............193...+..... +....761.169...............=...524........&......152..........*...975.994.........*....122..........858*...........%.......776...........*... +...........-..180/.850..............$.....524....-...........940.=......*......199........963..............#........836...*.....34...543.448 +482...947...................94&....541..............265..............477..890..............*....988.....343..........&...599....+........... +.......*...532..81..73..417............992/...99&...#........................*.33@.149..305.........678.....31.........@.................417 +....304.....@.....*...#../....452.842.................519.....307.949.....268................=.........*....*...........259................. +..................361............*.......346............*.+......*...........................288.......492..985.............727.683......... +....530....438..........950..........*......*........787..316...............762.724.257..889.....................844.......*......=....@125. +..............*...446...*......879..827......148......................*730.......@..........*...832...............*.......647.......48...... +....500.......994........41.......*.....#805...........*...........614...............*....940................291&.129.8............=........ +....*...#..........698..........581.................940.462.....@......443...82.784.593......../.#375..%148............*..#................. +.....39.562...784............................................805...$......@...*..*..........844.....................776....363...667........ +................%...265.......337.....438...295.....................499......341.74..................271.405.......................%.155.... +.........97..........*......@....*252.*......*....+.........270.229.................394...879*247.....*...*.....146.....................*... +........%.............226..258........996.944......793........*.../...................+..............321.578.....$..........838.....529.831. +.................%109.........................................615....822.....................501.............616....293.....&........-...... +.......%....482...........................%717.236.......562...........*...........915...580....*............*.....+..........230........... +.....303.......*128..86&......762.928.............$........*......861*.311........*....../....517..........471.544.....996.......*38...628.. +...........563............89.*......*.583...%.148.......$.987...................35..................996...........=....*...............*.... +......776.....+...........*......967...@..168...*.....772.........838*.....480..............................-........976..............530... +................649*32...84.#...................315...................37............398.656...631..........340.985*..........522............ +............................922........................./.173....894................*.........$....................177.......#.............. +......................................................423....*....*.....816.........13.................=..............................251... +..154...................878*......................568......723..329....&......928.................-71.123.378..100...................*...... +........*21..250............448.734.718.....&....*......................................599...............%....*.....700+.....*803..401..... +.779.550........*.817............./..*....118....980..589.....235......=....539...........*...281*236..........591.........927..........*... +...-.................*...............854.............*.................911.=............939........................616..*............770.50. +.............82......399.......452..................184........643..........................*871..................*....370.................. +.185.405.....*...634............*......189.838.................*...............@623......252...........315.86.....568...........157.104..... +...&......586.......*........957..........*.......841.........509.600........................896..565.....*...............+............*.... +..................633.628...........811..........*.....677.........-..580..751...183.........................540.81.......656..665..634..... +......112.............*.....=........./.322...430.........*..........+.....*...............683............$.....*........................... +830&...*..426........649..910...........*................717..359........831........=..157..%.............143......758................579... +.....373....*..................215.......666....................*.#...........459..547...+.........773.........980*.........404...126....... +205......636....................*...420.......440..........225.11.516...568....*......................*....474........620...*.....*......... +..............250......685...209.....+...........*257.................../...407.......295..498....122.............970...@.392.412........... +783..83+...........706....=.................330*............833.................*250....%..=..........%...........*............#....898..... +...........$....................................398..35........*......646....759.....#.............527..........160.....254................. +500........777...........112..428...663....957#..............524.....*..../........249..530*672............567......*......*759.....284*.... +....861....................*.*......*............*...277*832.....644.130..216.446............................*...683.955................950. +....*........826..........68.535..192.........306..................#..................762%...%....#631..500.406..........763...137.......... +..65............-...499=..................546.....&.........$........../..........177........988.......@.............336...*..*............. +.....362..................@...$..........-....610..65......64.........830..........-...........................452......+.604.542........... +........*902.633*........873..63.....781...31...*.................258.....12...681..........18*........&...&............................848. +..803............752.........................-.....672...................*.............................470..86.59.......%................... +.......@..547..........$...184.......320..........+..........471......834...=...551..108.............................176.................82. +..950.562............261...$.....754..+..............134.654*...............778..&......%..713.837.......................329........186..... +............112..72...............*..........917......*.................415............................946....=....614.....*................ +..............*.$......850.....766.....308.....*....666................*......812.....293................*.624.......%..296.....612...333... +..996......154...........................*......283.....*.555.%........................-.......552.714.563.......227..............-......... +....*.682.................=.......281.217..............84.*....908.....@...=....522..............$.*.......630......*79..............556.... +..635...*............516...402............................333........114..496..*.......324..........522..................................... +.........511....779...*............@............350.853.........................791...................................$.....821.....+....... +.................*.....486........984......332.....*..............893..730*480...................756...................776...........155.... +......161......57..590....................@....398..................*....................477.....*......602....506............996........... +.151.....*..=.......*......955.......916........#.....=..269.........423..........289......*..379...*..@.....................*.............. +...-....123..927.600...........-........*395........226./....676*755...........30..........58.....582.........671.118..43.367...943......478 +.....................725........74...............................................+...........................-......*..*................&... +........378......563*.....*999............*...................497.349...26.....$............362.........&........723....20....717...317..... +...*....................22.........../.575.552..........291......*..........600......152.../......./...169......................-.@....*977. +886.250..........$.............383.603.............36*.../....%......................*.............253........215..........139....583....... +................225............................................721...814..............829..................*.......613*544.*...=........901. +..........741.......595.........785.............%677..50................=.................252......710....91...............912.180.......... +...........*...................*....808...956..........+.....812...............*381......*.....676.../..........599....................+.... +............134.184......759..81...*.........*.782........41*......=....125.216........679...../..........@........*.................567.... +.......165.........*......@................66...*.....*.........379.....................................&..626...241........................ +.........*.........232.........387.86...........635.47.806.857.........$537...............240........617..............806..941..........531. +.......687...654.......735......*...$......................%.....357..............*884.......*571..........273.551......*.....*....576..*... +............*................648...................716.304..........*...690....663.................*38...........*....365......911....@..424 +....67*156...250..828...................&222.......#...*......16....300.+..................-....-.9....813...982..443....................... +.....................*......136..757..................65..314...*........................615.291.......&....*................972............ +................721..592...@.............................../...808.............665...176.............................200.....*.............. +..198..459+............................218........-772..............169...39..*........*........939..742......#....=......532...#..264...... +.....*...............$..........713...*............................*.....*...831....689...589*.......*.......91....75.........641...#....... +..896..167*........372...138...........462...*227..$.....801......512.........................775.673....................................... +...............47........*.......135...............725.....*...............743..........489*.............735..622.815.551...........519..... +.......=........*......636.987...*...................................827........496*........48.121................*.....*........#.....*311. +....525.......819.580.........#.369......119...............77.........*...............10*........*.%.....975.....961....888......304........ +........................96*..........................816............857......=...........376...859.522..=................................... +629...419..259...#360.......44%.........190..=969...........=...539.....-913.429......................................119.....476...707..... +...*.....*...*....................*906.*.....................84.....286...............$...=...127.200.465........324.............=.....*599. +905.....762...262.....-597.....847......311......@654...837............*....455..37..439..147....*.....*...262...*.....787..778............. +...........................25...........................=...........908....*.......*................827......%....567....-....#.427..819.... +..............................#.$....643=...............................886......243....../.....672..............................*....*..... +...........185*....107.....226..965........$.................756........................808.221...-.../574...541&...+....871...326....349... +......................*.....................841.....=...........*688........177................*....................166....*................ +.......189..744......308.......99*391..630/........463......................@...930..........543..........................519............... +..984.%........-.741.......................................522.915+.....769......*................207....976.....158*....................... +....*...............*...............&.331...787........48...........224..*......184........874.......=.....*........................537..... +.....934....*339...829....495.....682...*.............*....+..........*..794..........-430...*....&........848..367....+............*....505 +.........175..........................381............270....198......911...................52......642...............45............445...... \ No newline at end of file diff --git a/src/day3/part1.pas b/src/day3/part1.pas new file mode 100644 index 0000000..b2c959c --- /dev/null +++ b/src/day3/part1.pas @@ -0,0 +1,238 @@ +program day3part1; +uses sysutils; + +var +file_: text; +prev, curr, next: string; +sum, game: int32; +state: int32; +number: int32; +valid: boolean; + +i, b: int32; +c: char; + +LINE_LENGTH: int32 = 140; + +function is_symbol(str: string; ind: int32): boolean; +begin + is_symbol := ((not ((ind < 1) or (ind > LINE_LENGTH))) and (('0' > str[ind]) or (str[ind] > '9')) and (str[ind] <> '.')); +end; + +function is_ok(prev: string; curr: string; next: string; ind: int32): boolean; +begin + is_ok := is_symbol(curr, i-1); + is_ok := (is_ok or is_symbol(curr, i+1)); + is_ok := (is_ok or is_symbol(next, i-1)); + is_ok := (is_ok or is_symbol(next, i)); + is_ok := (is_ok or is_symbol(next, i+1)); + is_ok := (is_ok or is_symbol(prev, i-1)); + is_ok := (is_ok or is_symbol(prev, i)); + is_ok := (is_ok or is_symbol(prev, i+1)); +end; + + +(* Here the main program block starts *) +begin + { open inp.txt for reading } + assign(file_, 'resources/day3.txt'); + reset(file_); + + { initialize sum and count to 0 } + sum := 0; + + { read the first two lines } + readln(file_, curr); + readln(file_, next); + + writeln(length(curr)); + + writeln(booltostr(true)); + writeln(booltostr(false)); + + { writeln(curr); } + { writeln(next); } + + state := 0; + number := 0; + { 0 : waiting for number } + { 1 : reading number } + + { first loop } + for i := 1 to LINE_LENGTH do + begin + c := curr[i]; + case state of + {waiting for number} + 0: + begin + {number} + if (('0' <= c) and (c <= '9')) then + begin + {add number to number} + val(c, number); + valid := false; + state := 1; + + valid := (valid or is_ok(next, curr, next, i-1)); + + end; + end; + {reading number and checking true} + 1: + begin + if (('0' <= c) and (c <= '9')) then + begin + {add number to number} + val(c, b); + number := number * 10 + b; + + {look for symbol} + + valid := (valid or is_ok(next, curr, next, i-1)); + + end + else + begin + state := 0; + + if (valid) then + sum := sum + number; + + end; + + end; + end; + end; + + { main loop } + while not eof(file_) do + begin + + state := 0; + + prev := curr; + curr := next; + readln(file_, next); + + writeln(); + writeln(prev); + writeln(curr); + writeln(next); + + for i := 1 to LINE_LENGTH do + begin + c := curr[i]; + case state of + {waiting for number} + 0: + begin + {number} + if (('0' <= c) and (c <= '9')) then + begin + {add number to number} + val(c, number); + valid := false; + state := 1; + + valid := (valid or is_ok(prev, curr, next, i-1)); + + end; + end; + {reading number and checking true} + 1: + begin + if (('0' <= c) and (c <= '9')) then + begin + {add number to number} + val(c, b); + number := number * 10 + b; + + {look for symbol} + + valid := (valid or is_ok(prev, curr, next, i-1)); + + end + else + begin + state := 0; + + if (valid) then + sum := sum + number; + + end; + + end; + end; + + write(state); + + end; + + if (state = 1) then + begin + begin + + if (valid) then + sum := sum + number; + + end; + end; + + end; + + state := 0; + + prev := curr; + curr := next; + + + for i := 1 to LINE_LENGTH do + begin + c := curr[i]; + case state of + {waiting for number} + 0: + begin + {number} + if (('0' <= c) and (c <= '9')) then + begin + {add number to number} + val(c, number); + valid := false; + state := 1; + + valid := (valid or is_ok(prev, curr, prev, i-1)); + + end; + end; + {reading number and checking true} + 1: + begin + if (('0' <= c) and (c <= '9')) then + begin + {add number to number} + val(c, b); + number := number * 10 + b; + + {look for symbol} + + valid := (valid or is_ok(prev, curr, prev, i-1)); + + end + else + begin + state := 0; + + if (valid) then + sum := sum + number; + + end; + + end; + end; + end; + + writeln(); + writeln(sum); +end. \ No newline at end of file diff --git a/src/day3/part2.pas b/src/day3/part2.pas new file mode 100644 index 0000000..4d76962 --- /dev/null +++ b/src/day3/part2.pas @@ -0,0 +1,224 @@ +program day3part2; +uses sysutils; + +var +file_: text; +curr: string; +sum, game: int32; +state: int32; +number: int32; +valid: boolean; + +i, a, b: int32; +line: int32; +c: char; + +LINE_LENGTH: int32 = 140; + +curr_asterisk: int32; +asterisks: int32; +asterisk_values: array[1..1000] of int32; +asterisk_locations: array[1..1000, 1..2] of int32; + +function is_symbol(str: string; ind: int32): boolean; +begin + is_symbol := (str[ind] = '*') +end; + +function find_asterisk(hpos: int32; vpos: int32): int32; +var + j: int32; + +begin + + find_asterisk := -1; + + for j := 1 to asterisks do + begin + if ((asterisk_locations[j, 1] = hpos) and (asterisk_locations[j, 2] = vpos)) then + begin + find_asterisk := j; + break; + end; + end; +end; + +function get_asterisk_id(hpos: int32; vpos: int32): int32; +var + fucked_up: int32; + +begin + + get_asterisk_id := -1; + + fucked_up := find_asterisk(hpos-1, vpos-1); + if (fucked_up <> -1) then + get_asterisk_id := fucked_up; + fucked_up := find_asterisk(hpos, vpos-1); + if (fucked_up <> -1) then + get_asterisk_id := fucked_up; + fucked_up := find_asterisk(hpos+1, vpos-1); + if (fucked_up <> -1) then + get_asterisk_id := fucked_up; + fucked_up := find_asterisk(hpos-1, vpos); + if (fucked_up <> -1) then + get_asterisk_id := fucked_up; + fucked_up := find_asterisk(hpos+1, vpos); + if (fucked_up <> -1) then + get_asterisk_id := fucked_up; + fucked_up := find_asterisk(hpos-1, vpos+1); + if (fucked_up <> -1) then + get_asterisk_id := fucked_up; + fucked_up := find_asterisk(hpos, vpos+1); + if (fucked_up <> -1) then + get_asterisk_id := fucked_up; + fucked_up := find_asterisk(hpos+1, vpos+1); + if (fucked_up <> -1) then + get_asterisk_id := fucked_up; + +end; + +(* Here the main program block starts *) +begin + { open inp.txt for reading } + assign(file_, 'resources/day3.txt'); + reset(file_); + + { initialize sum and count to 0 } + sum := 0; + + { read the first two lines } + + line := 1; + asterisks := 0; + + + {find the position of all asterisks} + + while not eof(file_) do + begin + + readln(file_, curr); + + for i := 1 to LINE_LENGTH do + begin + if is_symbol(curr, i) then + begin + asterisk_locations[asterisks + 1][1] := i; + asterisk_locations[asterisks + 1][2] := line; + + asterisk_values[asterisks + 1] := 0; + + asterisks := asterisks + 1; + end; + end; + + line := line + 1; + + end; + + line := 0; + + reset(file_); + + writeln('boobs'); + + { main loop } + while not eof(file_) do + begin + + state := 0; + line := line + 1; + + readln(file_, curr); + + for i := 1 to LINE_LENGTH do + begin + c := curr[i]; + write(c); + case state of + {waiting for number} + 0: + begin + {number} + if (('0' <= c) and (c <= '9')) then + begin + {add number to number} + val(c, number); + state := 1; + + curr_asterisk := get_asterisk_id(i, line); + + end; + end; + {reading number and checking true} + 1: + begin + if (('0' <= c) and (c <= '9')) then + begin + + writeln('boobs2'); + + {add number to number} + val(c, b); + number := number * 10 + b; + + {look for symbol} + b := get_asterisk_id(i, line); + if (b <> -1) then + curr_asterisk := b; + + end + else + begin + state := 0; + writeln(curr_asterisk); + + if (curr_asterisk <> -1) then + begin + if (asterisk_values[curr_asterisk] = 0) then + asterisk_values[curr_asterisk] := number + else if (asterisk_values[curr_asterisk] > 0) then + begin + sum := sum + asterisk_values[curr_asterisk] * number; + asterisk_values[curr_asterisk] := -1; + end; + end; + + end; + + end; + end; + + write(state); + + end; + + if (state = 1) then + begin + begin + + if (curr_asterisk <> -1) then + begin + if (asterisk_values[curr_asterisk] = 0) then + asterisk_values[curr_asterisk] := number + else if (asterisk_values[curr_asterisk] > 0) then + begin + sum := sum + asterisk_values[curr_asterisk] * number; + asterisk_values[curr_asterisk] := -1; + end; + end; + + end; + end; + + end; + + i := get_asterisk_id(114, 126); + writeln(i); + writeln(asterisk_locations[i, 1]); + writeln(asterisk_locations[i, 2]); + writeln(sum); + + +end. \ No newline at end of file