diff options
author | Federico Igne <undyamon@disroot.org> | 2023-12-03 18:59:26 +0100 |
---|---|---|
committer | Federico Igne <undyamon@disroot.org> | 2023-12-03 18:59:26 +0100 |
commit | 3752b45ea18e2f2917aad182a42c13b0908234d5 (patch) | |
tree | 8da10287dec001f7a0a0cb63a1cd9d6c53d85c04 /2023 | |
parent | baaa6cd61c302c80c664e9aaa5e4ab2ab157b5cf (diff) | |
download | aoc-3752b45ea18e2f2917aad182a42c13b0908234d5.tar.gz aoc-3752b45ea18e2f2917aad182a42c13b0908234d5.zip |
aoc(2303): Gear Ratios
Diffstat (limited to '2023')
-rw-r--r-- | 2023/03/Makefile | 15 | ||||
-rw-r--r-- | 2023/03/resources/input.txt | 140 | ||||
-rw-r--r-- | 2023/03/resources/input_small.txt | 10 | ||||
-rw-r--r-- | 2023/03/src/part1.cpp | 79 | ||||
-rw-r--r-- | 2023/03/src/part2.cpp | 97 |
5 files changed, 341 insertions, 0 deletions
diff --git a/2023/03/Makefile b/2023/03/Makefile new file mode 100644 index 0000000..2f375fd --- /dev/null +++ b/2023/03/Makefile | |||
@@ -0,0 +1,15 @@ | |||
1 | CXXFLAGS := -std=c++17 | ||
2 | EXE := part1 part2 | ||
3 | |||
4 | .PHONY: all clean | ||
5 | |||
6 | all: $(EXE) | ||
7 | |||
8 | %.o: %.cpp | ||
9 | $(CXX) -c $(CXXFLAGS) $< -o $@ | ||
10 | |||
11 | clean: | ||
12 | rm -rf $(EXE) src/*.o | ||
13 | |||
14 | %: src/%.o | ||
15 | $(CXX) $^ -o $@ | ||
diff --git a/2023/03/resources/input.txt b/2023/03/resources/input.txt new file mode 100644 index 0000000..e89c547 --- /dev/null +++ b/2023/03/resources/input.txt | |||
@@ -0,0 +1,140 @@ | |||
1 | ........936..672.........846.922........359...332......582..856........................579..93......674..740.....243.156.................... | ||
2 | ...........%.........4=...*...*........*.......*......#....................806..481.........................*.......*.........900......$564. | ||
3 | .............520........624.965....143..405.....960.............273...651...*....*.........554....139@.....38...*.........58..*...392....... | ||
4 | ................$....................*.........................*.....&......634.3..../.................-......310....*313.*.........*....... | ||
5 | ...196....544......541................775.216...+557..225/..463.......................517..........960.228........452.....593.......772..... | ||
6 | .....*....*...654.*........638............*...........................163....................386#......................20......169.......... | ||
7 | .....566..20...$...132........*969......747........*971..342............=.245*.......................277.522............*..............+.... | ||
8 | .............+..............................205.716...........................855.............324......................871..752.........291. | ||
9 | .........47..390.......128...%....528........%..........91*192.......=.....................=.#.............%..852*156......+................ | ||
10 | .....975..*.......=....*...322.......#............&63.............946...................462....939..520...268............#.....*....380..... | ||
11 | .........55..=.....871..68.............................................491.......................*.....*............674.770.333.415...*.275. | ||
12 | ..............633...........@.....642.5........+.......739........632.....*.......992.74.615...858...998.............*..............761.*... | ||
13 | ....446......................180./..../........83...&.....*924....&........379...*......................................................118. | ||
14 | ...........488*240.......150......................958..........$.....&...........964...351...........+..633.........123....../603........... | ||
15 | ...................987....../........546-...674...............466.....602.............@...........956..#............./...................... | ||
16 | ......503=....465.................91.........*..........................................583..............128..124..........$.....217........ | ||
17 | ..............*...32$..53....&594.........648...827....*..994.......971...................*..........304...%....*.......387..80............. | ||
18 | ............756.......#..........................*..888...$............@....339.......+..868........*........851..............*.........128. | ||
19 | ...905-.........312..................729...356*.697.........736.....+.......+.......748.......797....41....................272..753......... | ||
20 | .................*....&604............*.............%915.......*.529..889...................*.........................992.........*.....718. | ||
21 | ...........272....487..............+...961.892..............632........*.................502.118....141................@......546.669....... | ||
22 | 787..........*....................85............/..330...........-...374.............................*.........446...%.........*............ | ||
23 | ....-...../...120....919&.....................339....*....*663...403........167...533.170.............698..587...*....279.......776.+....... | ||
24 | .575.....34.*.................668...19..759.......804..730...../.............*...........*.....789...........*....803......666.......25..... | ||
25 | ............375...773*.............*.........................$.876...287......201........484......*.........196............................. | ||
26 | 565...866.............23..477...455....................794.996.........*.....................887.959...146...........496....790............. | ||
27 | ..............648...........+........325.738....156.....*.........534..941.....%536..31..../.%...........*............*.....*.......511..... | ||
28 | ..............*.................166.....*......*.....647..........*......................262...........887..210.3..860.....161....4....*420. | ||
29 | ......141.606...........193.111..*..........252......................935*689..................500...........*...*.................*......... | ||
30 | ..635....*........=........*.....959.....................@834.................-.....542..547....-..........770.8...........439.............. | ||
31 | .................896.........93......................244..........@......247..735..*.................................497.............%...... | ||
32 | ...............................%........................*..........200....+.......342.613.245.@............$.........#....*.......176...21.. | ||
33 | ......51......702.37.......798....325.....&974.....834.860.....+.........................*.....533..866....864.............269.............. | ||
34 | ..141..*......................-...&............875*.............207...882*.........................*............309.739........%......../... | ||
35 | .......394....144.....................................$...................835.3..........290.......489...........&.....*177.833....866$.153. | ||
36 | .689.................719.314/..$......673.....5*557...443......+..#649.............575....*..634*........................................... | ||
37 | ....&...........794...*........614...*......................350.............411......*.104.......141........369...934...........997......... | ||
38 | .......519......*...787..............739.....628...................................784.......$..........989*........*...987.......*....#.... | ||
39 | .........*...309.........209.............957*........50.....@995...765.......269.@........380..624.................643.....*....836...356... | ||
40 | ......886............234.+....................51*935..*...........*.....188.......73..............*696...14.....#........................... | ||
41 | ............203..948...&...............................957......804.736*......388....689.219*660........*.....218....................$..40.. | ||
42 | ..............*.....*.....475.....930..........%................................*......*..............477.624................848...687...... | ||
43 | ....57*.50.229....636....*.................804.164....590.........-....547..80..310....675.......=.@...........................*............ | ||
44 | ........................398......&........*..............*.....748...........*................226..200................160......853.......... | ||
45 | ......292........................889.......867.....365...............192+..530.........................@910....=..222*.............894...... | ||
46 | ..427....#.../...959........................................322.....................735*304....742..........195..........$..............22.. | ||
47 | ...........465..-.........52.$......168.......418.......+.........................................$..............&......563...649*26........ | ||
48 | ....................464..../.969....*......-..*........150......724.....83..301...............527...............858.506..................... | ||
49 | ...935.........910....+...........116....427...347.............-........*.....=.......891.......*....847...750........@................531.. | ||
50 | ....*.........$.........676...............................510............11.......647.*.................#.....%.683.............193...+..... | ||
51 | ....761.169...............=...524........&......152..........*...975.994.........*....122..........858*...........%.......776...........*... | ||
52 | ...........-..180/.850..............$.....524....-...........940.=......*......199........963..............#........836...*.....34...543.448 | ||
53 | 482...947...................94&....541..............265..............477..890..............*....988.....343..........&...599....+........... | ||
54 | .......*...532..81..73..417............992/...99&...#........................*.33@.149..305.........678.....31.........@.................417 | ||
55 | ....304.....@.....*...#../....452.842.................519.....307.949.....268................=.........*....*...........259................. | ||
56 | ..................361............*.......346............*.+......*...........................288.......492..985.............727.683......... | ||
57 | ....530....438..........950..........*......*........787..316...............762.724.257..889.....................844.......*......=....@125. | ||
58 | ..............*...446...*......879..827......148......................*730.......@..........*...832...............*.......647.......48...... | ||
59 | ....500.......994........41.......*.....#805...........*...........614...............*....940................291&.129.8............=........ | ||
60 | ....*...#..........698..........581.................940.462.....@......443...82.784.593......../.#375..%148............*..#................. | ||
61 | .....39.562...784............................................805...$......@...*..*..........844.....................776....363...667........ | ||
62 | ................%...265.......337.....438...295.....................499......341.74..................271.405.......................%.155.... | ||
63 | .........97..........*......@....*252.*......*....+.........270.229.................394...879*247.....*...*.....146.....................*... | ||
64 | ........%.............226..258........996.944......793........*.../...................+..............321.578.....$..........838.....529.831. | ||
65 | .................%109.........................................615....822.....................501.............616....293.....&........-...... | ||
66 | .......%....482...........................%717.236.......562...........*...........915...580....*............*.....+..........230........... | ||
67 | .....303.......*128..86&......762.928.............$........*......861*.311........*....../....517..........471.544.....996.......*38...628.. | ||
68 | ...........563............89.*......*.583...%.148.......$.987...................35..................996...........=....*...............*.... | ||
69 | ......776.....+...........*......967...@..168...*.....772.........838*.....480..............................-........976..............530... | ||
70 | ................649*32...84.#...................315...................37............398.656...631..........340.985*..........522............ | ||
71 | ............................922........................./.173....894................*.........$....................177.......#.............. | ||
72 | ......................................................423....*....*.....816.........13.................=..............................251... | ||
73 | ..154...................878*......................568......723..329....&......928.................-71.123.378..100...................*...... | ||
74 | ........*21..250............448.734.718.....&....*......................................599...............%....*.....700+.....*803..401..... | ||
75 | .779.550........*.817............./..*....118....980..589.....235......=....539...........*...281*236..........591.........927..........*... | ||
76 | ...-.................*...............854.............*.................911.=............939........................616..*............770.50. | ||
77 | .............82......399.......452..................184........643..........................*871..................*....370.................. | ||
78 | .185.405.....*...634............*......189.838.................*...............@623......252...........315.86.....568...........157.104..... | ||
79 | ...&......586.......*........957..........*.......841.........509.600........................896..565.....*...............+............*.... | ||
80 | ..................633.628...........811..........*.....677.........-..580..751...183.........................540.81.......656..665..634..... | ||
81 | ......112.............*.....=........./.322...430.........*..........+.....*...............683............$.....*........................... | ||
82 | 830&...*..426........649..910...........*................717..359........831........=..157..%.............143......758................579... | ||
83 | .....373....*..................215.......666....................*.#...........459..547...+.........773.........980*.........404...126....... | ||
84 | 205......636....................*...420.......440..........225.11.516...568....*......................*....474........620...*.....*......... | ||
85 | ..............250......685...209.....+...........*257.................../...407.......295..498....122.............970...@.392.412........... | ||
86 | 783..83+...........706....=.................330*............833.................*250....%..=..........%...........*............#....898..... | ||
87 | ...........$....................................398..35........*......646....759.....#.............527..........160.....254................. | ||
88 | 500........777...........112..428...663....957#..............524.....*..../........249..530*672............567......*......*759.....284*.... | ||
89 | ....861....................*.*......*............*...277*832.....644.130..216.446............................*...683.955................950. | ||
90 | ....*........826..........68.535..192.........306..................#..................762%...%....#631..500.406..........763...137.......... | ||
91 | ..65............-...499=..................546.....&.........$........../..........177........988.......@.............336...*..*............. | ||
92 | .....362..................@...$..........-....610..65......64.........830..........-...........................452......+.604.542........... | ||
93 | ........*902.633*........873..63.....781...31...*.................258.....12...681..........18*........&...&............................848. | ||
94 | ..803............752.........................-.....672...................*.............................470..86.59.......%................... | ||
95 | .......@..547..........$...184.......320..........+..........471......834...=...551..108.............................176.................82. | ||
96 | ..950.562............261...$.....754..+..............134.654*...............778..&......%..713.837.......................329........186..... | ||
97 | ............112..72...............*..........917......*.................415............................946....=....614.....*................ | ||
98 | ..............*.$......850.....766.....308.....*....666................*......812.....293................*.624.......%..296.....612...333... | ||
99 | ..996......154...........................*......283.....*.555.%........................-.......552.714.563.......227..............-......... | ||
100 | ....*.682.................=.......281.217..............84.*....908.....@...=....522..............$.*.......630......*79..............556.... | ||
101 | ..635...*............516...402............................333........114..496..*.......324..........522..................................... | ||
102 | .........511....779...*............@............350.853.........................791...................................$.....821.....+....... | ||
103 | .................*.....486........984......332.....*..............893..730*480...................756...................776...........155.... | ||
104 | ......161......57..590....................@....398..................*....................477.....*......602....506............996........... | ||
105 | .151.....*..=.......*......955.......916........#.....=..269.........423..........289......*..379...*..@.....................*.............. | ||
106 | ...-....123..927.600...........-........*395........226./....676*755...........30..........58.....582.........671.118..43.367...943......478 | ||
107 | .....................725........74...............................................+...........................-......*..*................&... | ||
108 | ........378......563*.....*999............*...................497.349...26.....$............362.........&........723....20....717...317..... | ||
109 | ...*....................22.........../.575.552..........291......*..........600......152.../......./...169......................-.@....*977. | ||
110 | 886.250..........$.............383.603.............36*.../....%......................*.............253........215..........139....583....... | ||
111 | ................225............................................721...814..............829..................*.......613*544.*...=........901. | ||
112 | ..........741.......595.........785.............%677..50................=.................252......710....91...............912.180.......... | ||
113 | ...........*...................*....808...956..........+.....812...............*381......*.....676.../..........599....................+.... | ||
114 | ............134.184......759..81...*.........*.782........41*......=....125.216........679...../..........@........*.................567.... | ||
115 | .......165.........*......@................66...*.....*.........379.....................................&..626...241........................ | ||
116 | .........*.........232.........387.86...........635.47.806.857.........$537...............240........617..............806..941..........531. | ||
117 | .......687...654.......735......*...$......................%.....357..............*884.......*571..........273.551......*.....*....576..*... | ||
118 | ............*................648...................716.304..........*...690....663.................*38...........*....365......911....@..424 | ||
119 | ....67*156...250..828...................&222.......#...*......16....300.+..................-....-.9....813...982..443....................... | ||
120 | .....................*......136..757..................65..314...*........................615.291.......&....*................972............ | ||
121 | ................721..592...@.............................../...808.............665...176.............................200.....*.............. | ||
122 | ..198..459+............................218........-772..............169...39..*........*........939..742......#....=......532...#..264...... | ||
123 | .....*...............$..........713...*............................*.....*...831....689...589*.......*.......91....75.........641...#....... | ||
124 | ..896..167*........372...138...........462...*227..$.....801......512.........................775.673....................................... | ||
125 | ...............47........*.......135...............725.....*...............743..........489*.............735..622.815.551...........519..... | ||
126 | .......=........*......636.987...*...................................827........496*........48.121................*.....*........#.....*311. | ||
127 | ....525.......819.580.........#.369......119...............77.........*...............10*........*.%.....975.....961....888......304........ | ||
128 | ........................96*..........................816............857......=...........376...859.522..=................................... | ||
129 | 629...419..259...#360.......44%.........190..=969...........=...539.....-913.429......................................119.....476...707..... | ||
130 | ...*.....*...*....................*906.*.....................84.....286...............$...=...127.200.465........324.............=.....*599. | ||
131 | 905.....762...262.....-597.....847......311......@654...837............*....455..37..439..147....*.....*...262...*.....787..778............. | ||
132 | ...........................25...........................=...........908....*.......*................827......%....567....-....#.427..819.... | ||
133 | ..............................#.$....643=...............................886......243....../.....672..............................*....*..... | ||
134 | ...........185*....107.....226..965........$.................756........................808.221...-.../574...541&...+....871...326....349... | ||
135 | ......................*.....................841.....=...........*688........177................*....................166....*................ | ||
136 | .......189..744......308.......99*391..630/........463......................@...930..........543..........................519............... | ||
137 | ..984.%........-.741.......................................522.915+.....769......*................207....976.....158*....................... | ||
138 | ....*...............*...............&.331...787........48...........224..*......184........874.......=.....*........................537..... | ||
139 | .....934....*339...829....495.....682...*.............*....+..........*..794..........-430...*....&........848..367....+............*....505 | ||
140 | .........175..........................381............270....198......911...................52......642...............45............445...... | ||
diff --git a/2023/03/resources/input_small.txt b/2023/03/resources/input_small.txt new file mode 100644 index 0000000..b20187f --- /dev/null +++ b/2023/03/resources/input_small.txt | |||
@@ -0,0 +1,10 @@ | |||
1 | 467..114.. | ||
2 | ...*...... | ||
3 | ..35..633. | ||
4 | ......#... | ||
5 | 617*...... | ||
6 | .....+.58. | ||
7 | ..592..... | ||
8 | ......755. | ||
9 | ...$.*.... | ||
10 | .664.598.. | ||
diff --git a/2023/03/src/part1.cpp b/2023/03/src/part1.cpp new file mode 100644 index 0000000..9cd1622 --- /dev/null +++ b/2023/03/src/part1.cpp | |||
@@ -0,0 +1,79 @@ | |||
1 | #include <iostream> | ||
2 | #include <fstream> | ||
3 | #include <vector> | ||
4 | |||
5 | using Schematic = std::vector<std::string>; | ||
6 | |||
7 | bool check(const Schematic& schematic, int x, int y) | ||
8 | { | ||
9 | return x >= 0 and y >= 0 and x < schematic.size() and y < schematic[x].size() | ||
10 | and schematic[x][y] != '.' and not std::isdigit(schematic[x][y]); | ||
11 | } | ||
12 | |||
13 | bool check_adj(const Schematic& schematic, int x, int y) | ||
14 | { | ||
15 | return check(schematic, x - 1, y - 1) | ||
16 | or check(schematic, x, y - 1) | ||
17 | or check(schematic, x + 1, y - 1) | ||
18 | or check(schematic, x - 1, y) | ||
19 | or check(schematic, x + 1, y); | ||
20 | } | ||
21 | |||
22 | int find_nums(const Schematic& schematic) | ||
23 | { | ||
24 | int sum{}; | ||
25 | |||
26 | for (int x = 0; x < schematic.size(); ++x) | ||
27 | { | ||
28 | const std::string& line = schematic[x]; | ||
29 | |||
30 | int cur{}; | ||
31 | bool adj{ false }; | ||
32 | |||
33 | for (int y = 0; y < line.size(); ++y) | ||
34 | { | ||
35 | if (std::isdigit(line[y])) | ||
36 | { | ||
37 | cur = 10 * cur + (line[y] - '0'); | ||
38 | adj = adj or check_adj(schematic, x, y); | ||
39 | } | ||
40 | else | ||
41 | { | ||
42 | if (cur > 0) | ||
43 | { | ||
44 | adj = adj or check(schematic, x - 1, y) | ||
45 | or check(schematic, x, y) | ||
46 | or check(schematic, x + 1, y); | ||
47 | sum += adj ? cur : 0; | ||
48 | } | ||
49 | cur = 0; | ||
50 | adj = false; | ||
51 | } | ||
52 | } | ||
53 | } | ||
54 | |||
55 | return sum; | ||
56 | } | ||
57 | |||
58 | int main(void) | ||
59 | { | ||
60 | int answer{}; | ||
61 | Schematic schematic{}; | ||
62 | |||
63 | std::ifstream input{ "resources/input.txt" }; | ||
64 | if (input.is_open()) | ||
65 | { | ||
66 | std::string line; | ||
67 | while (not std::getline(input,line).eof()) | ||
68 | { | ||
69 | line.push_back('.'); | ||
70 | schematic.push_back(std::move(line)); | ||
71 | } | ||
72 | } | ||
73 | input.close(); | ||
74 | |||
75 | answer = find_nums(schematic); | ||
76 | |||
77 | std::cout << answer << std::endl; | ||
78 | return 0; | ||
79 | } | ||
diff --git a/2023/03/src/part2.cpp b/2023/03/src/part2.cpp new file mode 100644 index 0000000..16e9678 --- /dev/null +++ b/2023/03/src/part2.cpp | |||
@@ -0,0 +1,97 @@ | |||
1 | #include <fstream> | ||
2 | #include <iostream> | ||
3 | #include <map> | ||
4 | #include <vector> | ||
5 | #include <cmath> | ||
6 | |||
7 | using Schematic = std::vector<std::string>; | ||
8 | using Gears = std::map<std::pair<int,int>,std::vector<int>>; | ||
9 | |||
10 | void check(const Schematic& schematic, Gears& gears, int num, int x, int y) | ||
11 | { | ||
12 | if (x >= 0 and y >= 0 and x < schematic.size() | ||
13 | and y < schematic[x].size() and schematic[x][y] == '*') | ||
14 | { | ||
15 | std::pair<int,int> key{ x, y }; | ||
16 | if (not gears.count(key)) | ||
17 | { | ||
18 | gears.insert({key, std::vector<int>{}}); | ||
19 | } | ||
20 | gears[key].push_back(num); | ||
21 | } | ||
22 | } | ||
23 | |||
24 | void check_gears(const Schematic& schematic, Gears& gears, int num, int x, int y) | ||
25 | { | ||
26 | int len = 1 + static_cast<int>(std::log10(num)); | ||
27 | check(schematic, gears, num, x - 1, y - len - 1); | ||
28 | check(schematic, gears, num, x, y - len - 1); | ||
29 | check(schematic, gears, num, x + 1, y - len - 1); | ||
30 | for (int a = 1; a <= len; ++a) | ||
31 | { | ||
32 | check(schematic, gears, num, x - 1, y - a); | ||
33 | check(schematic, gears, num, x + 1, y - a); | ||
34 | } | ||
35 | check(schematic, gears, num, x - 1, y); | ||
36 | check(schematic, gears, num, x, y); | ||
37 | check(schematic, gears, num, x + 1, y); | ||
38 | } | ||
39 | |||
40 | Gears find_gears(const Schematic& schematic) | ||
41 | { | ||
42 | Gears gears; | ||
43 | |||
44 | for (int x = 0; x < schematic.size(); ++x) | ||
45 | { | ||
46 | const std::string& line = schematic[x]; | ||
47 | |||
48 | int cur{}; | ||
49 | |||
50 | for (int y = 0; y < line.size(); ++y) | ||
51 | { | ||
52 | if (std::isdigit(line[y])) | ||
53 | { | ||
54 | cur = 10 * cur + (line[y] - '0'); | ||
55 | } | ||
56 | else | ||
57 | { | ||
58 | if (cur > 0) | ||
59 | { | ||
60 | check_gears(schematic, gears, cur, x, y); | ||
61 | } | ||
62 | cur = 0; | ||
63 | } | ||
64 | } | ||
65 | } | ||
66 | |||
67 | return gears; | ||
68 | } | ||
69 | |||
70 | int main(void) | ||
71 | { | ||
72 | int answer{}; | ||
73 | Schematic schematic{}; | ||
74 | |||
75 | std::ifstream input{ "resources/input.txt" }; | ||
76 | if (input.is_open()) | ||
77 | { | ||
78 | std::string line; | ||
79 | while (not std::getline(input,line).eof()) | ||
80 | { | ||
81 | line.push_back('.'); | ||
82 | schematic.push_back(std::move(line)); | ||
83 | } | ||
84 | } | ||
85 | input.close(); | ||
86 | |||
87 | Gears gears = find_gears(schematic); | ||
88 | for (const auto& [_, vals] : gears) { | ||
89 | if (vals.size() == 2) | ||
90 | { | ||
91 | answer += vals[0] * vals[1]; | ||
92 | } | ||
93 | } | ||
94 | |||
95 | std::cout << answer << std::endl; | ||
96 | return 0; | ||
97 | } | ||