From 5bf68a6003e646dfbb3676d0e08962701e6d267d Mon Sep 17 00:00:00 2001 From: LordMathis Date: Mon, 18 Dec 2023 19:56:21 +0100 Subject: [PATCH 1/6] Restructure code --- .gitignore | 6 +- 2023/01-trebuchet/src/main.rs | 7 - 2023/02-cube-conundrum/Cargo.toml | 8 - 2023/02-cube-conundrum/src/main.rs | 7 - 2023/03-gear-ratios/Cargo.toml | 8 - 2023/03-gear-ratios/src/main.rs | 7 - 2023/03-gear-ratios/src/part_one.rs | 145 ------------------ 2023/04-scratchcards/Cargo.toml | 8 - 2023/04-scratchcards/src/main.rs | 5 - 2023/06-wait-for-it/Cargo.toml | 8 - 2023/06-wait-for-it/src/main.rs | 7 - 2023/01-trebuchet/Cargo.toml => Cargo.toml | 2 +- {2023 => input/2023}/01-trebuchet/input.txt | 0 .../2023}/01-trebuchet/input_test.txt | 0 .../2023}/02-cube-conundrum/input.txt | 0 .../2023}/02-cube-conundrum/input_test.txt | 0 {2023 => input/2023}/03-gear-ratios/input.txt | 0 .../2023}/03-gear-ratios/input_test.txt | 0 .../2023}/04-scratchcards/input.txt | 0 .../2023}/04-scratchcards/input_test.txt | 0 {2023 => input/2023}/06-wait-for-it/input.txt | 0 .../2023}/06-wait-for-it/input_test.txt | 0 .../src => src/2023/01-trebuchet}/part_one.rs | 0 .../src => src/2023/01-trebuchet}/part_two.rs | 0 .../2023/02-cube-conundrum}/part_one.rs | 0 .../2023/02-cube-conundrum}/part_two.rs | 0 src/2023/03-gear-ratios/input.txt | 140 +++++++++++++++++ src/2023/03-gear-ratios/input_test.txt | 10 ++ .../2023/04-scratchcards}/part_one.rs | 0 .../2023/06-wait-for-it}/part_one.rs | 0 .../2023/06-wait-for-it}/part_two.rs | 0 src/main.rs | 3 + 32 files changed, 159 insertions(+), 212 deletions(-) delete mode 100644 2023/01-trebuchet/src/main.rs delete mode 100644 2023/02-cube-conundrum/Cargo.toml delete mode 100644 2023/02-cube-conundrum/src/main.rs delete mode 100644 2023/03-gear-ratios/Cargo.toml delete mode 100644 2023/03-gear-ratios/src/main.rs delete mode 100644 2023/03-gear-ratios/src/part_one.rs delete mode 100644 2023/04-scratchcards/Cargo.toml delete mode 100644 2023/04-scratchcards/src/main.rs delete mode 100644 2023/06-wait-for-it/Cargo.toml delete mode 100644 2023/06-wait-for-it/src/main.rs rename 2023/01-trebuchet/Cargo.toml => Cargo.toml (86%) rename {2023 => input/2023}/01-trebuchet/input.txt (100%) rename {2023 => input/2023}/01-trebuchet/input_test.txt (100%) rename {2023 => input/2023}/02-cube-conundrum/input.txt (100%) rename {2023 => input/2023}/02-cube-conundrum/input_test.txt (100%) rename {2023 => input/2023}/03-gear-ratios/input.txt (100%) rename {2023 => input/2023}/03-gear-ratios/input_test.txt (100%) rename {2023 => input/2023}/04-scratchcards/input.txt (100%) rename {2023 => input/2023}/04-scratchcards/input_test.txt (100%) rename {2023 => input/2023}/06-wait-for-it/input.txt (100%) rename {2023 => input/2023}/06-wait-for-it/input_test.txt (100%) rename {2023/01-trebuchet/src => src/2023/01-trebuchet}/part_one.rs (100%) rename {2023/01-trebuchet/src => src/2023/01-trebuchet}/part_two.rs (100%) rename {2023/02-cube-conundrum/src => src/2023/02-cube-conundrum}/part_one.rs (100%) rename {2023/02-cube-conundrum/src => src/2023/02-cube-conundrum}/part_two.rs (100%) create mode 100644 src/2023/03-gear-ratios/input.txt create mode 100644 src/2023/03-gear-ratios/input_test.txt rename {2023/04-scratchcards/src => src/2023/04-scratchcards}/part_one.rs (100%) rename {2023/06-wait-for-it/src => src/2023/06-wait-for-it}/part_one.rs (100%) rename {2023/06-wait-for-it/src => src/2023/06-wait-for-it}/part_two.rs (100%) create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore index ada8be9..ccf6d7f 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,8 @@ Cargo.lock **/*.rs.bk # MSVC Windows builds of rustc generate these, which store debugging information -*.pdb \ No newline at end of file +*.pdb + +# Added by cargo + +/target diff --git a/2023/01-trebuchet/src/main.rs b/2023/01-trebuchet/src/main.rs deleted file mode 100644 index e072d03..0000000 --- a/2023/01-trebuchet/src/main.rs +++ /dev/null @@ -1,7 +0,0 @@ -mod part_one; -mod part_two; - -fn main() { - //part_one::part_one(); - part_two::part_two(); -} diff --git a/2023/02-cube-conundrum/Cargo.toml b/2023/02-cube-conundrum/Cargo.toml deleted file mode 100644 index e660896..0000000 --- a/2023/02-cube-conundrum/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "cube-conundrum" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] diff --git a/2023/02-cube-conundrum/src/main.rs b/2023/02-cube-conundrum/src/main.rs deleted file mode 100644 index e072d03..0000000 --- a/2023/02-cube-conundrum/src/main.rs +++ /dev/null @@ -1,7 +0,0 @@ -mod part_one; -mod part_two; - -fn main() { - //part_one::part_one(); - part_two::part_two(); -} diff --git a/2023/03-gear-ratios/Cargo.toml b/2023/03-gear-ratios/Cargo.toml deleted file mode 100644 index e44941d..0000000 --- a/2023/03-gear-ratios/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "gear-ratios" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] diff --git a/2023/03-gear-ratios/src/main.rs b/2023/03-gear-ratios/src/main.rs deleted file mode 100644 index 71d7c6a..0000000 --- a/2023/03-gear-ratios/src/main.rs +++ /dev/null @@ -1,7 +0,0 @@ -mod part_one; -//mod part_two; - -fn main() { - part_one::part_one(); - //part_two::part_two(); -} diff --git a/2023/03-gear-ratios/src/part_one.rs b/2023/03-gear-ratios/src/part_one.rs deleted file mode 100644 index e7c5ad1..0000000 --- a/2023/03-gear-ratios/src/part_one.rs +++ /dev/null @@ -1,145 +0,0 @@ -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; - -enum Engine { - Symbol(char), - Number(u32), - Empty, -} - -pub fn part_one() { - if let Ok(lines) = read_lines("./input.txt") { - let matrix = create_matrix(lines); - let result = process_matrix(&matrix); - - println!("{}", result); - } -} - -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} - -fn create_matrix(lines: io::Lines>) -> Vec> { - let mut matrix: Vec> = Vec::new(); - - for (i, line) in lines.enumerate() { - matrix.push(Vec::new()); - - let line = line.unwrap(); - - for ch in line.chars() { - if ch == '.' { - matrix[i].push(Engine::Empty); - } else if ch.is_digit(10) { - matrix[i].push(Engine::Number(ch.to_digit(10).unwrap())); - } else { - matrix[i].push(Engine::Symbol(ch)); - } - } - } - - return matrix; -} - -fn process_matrix(matrix: &Vec>) -> u32 { - let mut result = 0; - - for i in 0..matrix.len() { - let mut is_num = false; - let mut num = 0; - let mut found_symbol = false; - - for j in 0..matrix[i].len() { - match matrix[i][j] { - Engine::Number(value) => { - if !found_symbol { - found_symbol = check_symbol(&matrix, i, j); - } - - if is_num { - num = num * 10 + value; - } else { - num = value; - } - - is_num = true; - } - _ => { - if is_num && found_symbol { - result += num; - } - - is_num = false; - found_symbol = false; - num = 0; - } - } - } - - if is_num && found_symbol { - result += num; - } - } - - return result; -} - -fn check_symbol(matrix: &Vec>, x: usize, y: usize) -> bool { - // There's probably a better way - - if x > 0 { - if let Engine::Symbol(_) = matrix[x - 1][y] { - return true; - } - - if y > 0 { - if let Engine::Symbol(_) = matrix[x - 1][y - 1] { - return true; - } - } - - if y < matrix[x - 1].len() - 1 { - if let Engine::Symbol(_) = matrix[x - 1][y + 1] { - return true; - } - } - } - - if y > 0 { - if let Engine::Symbol(_) = matrix[x][y - 1] { - return true; - } - } - - if y < matrix[x].len() - 1 { - if let Engine::Symbol(_) = matrix[x][y + 1] { - return true; - } - } - - if x < matrix.len() - 1 { - if let Engine::Symbol(_) = matrix[x + 1][y] { - return true; - } - - if y > 0 { - if let Engine::Symbol(_) = matrix[x + 1][y - 1] { - return true; - } - } - - if y < matrix[x + 1].len() - 1 { - if let Engine::Symbol(_) = matrix[x + 1][y + 1] { - return true; - } - } - } - - return false; -} diff --git a/2023/04-scratchcards/Cargo.toml b/2023/04-scratchcards/Cargo.toml deleted file mode 100644 index 89ee6ef..0000000 --- a/2023/04-scratchcards/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "scratchcards" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] diff --git a/2023/04-scratchcards/src/main.rs b/2023/04-scratchcards/src/main.rs deleted file mode 100644 index d52adb4..0000000 --- a/2023/04-scratchcards/src/main.rs +++ /dev/null @@ -1,5 +0,0 @@ -mod part_one; - -fn main() { - part_one::part_one(); -} diff --git a/2023/06-wait-for-it/Cargo.toml b/2023/06-wait-for-it/Cargo.toml deleted file mode 100644 index 49c02f8..0000000 --- a/2023/06-wait-for-it/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "wait-for-it" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] diff --git a/2023/06-wait-for-it/src/main.rs b/2023/06-wait-for-it/src/main.rs deleted file mode 100644 index df0b383..0000000 --- a/2023/06-wait-for-it/src/main.rs +++ /dev/null @@ -1,7 +0,0 @@ -//mod part_one; -mod part_two; - -fn main() { - //part_one::part_one(); - part_two::part_two(); -} diff --git a/2023/01-trebuchet/Cargo.toml b/Cargo.toml similarity index 86% rename from 2023/01-trebuchet/Cargo.toml rename to Cargo.toml index 5d49fb2..cb161c1 100644 --- a/2023/01-trebuchet/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "trebuchet" +name = "advent-of-code" version = "0.1.0" edition = "2021" diff --git a/2023/01-trebuchet/input.txt b/input/2023/01-trebuchet/input.txt similarity index 100% rename from 2023/01-trebuchet/input.txt rename to input/2023/01-trebuchet/input.txt diff --git a/2023/01-trebuchet/input_test.txt b/input/2023/01-trebuchet/input_test.txt similarity index 100% rename from 2023/01-trebuchet/input_test.txt rename to input/2023/01-trebuchet/input_test.txt diff --git a/2023/02-cube-conundrum/input.txt b/input/2023/02-cube-conundrum/input.txt similarity index 100% rename from 2023/02-cube-conundrum/input.txt rename to input/2023/02-cube-conundrum/input.txt diff --git a/2023/02-cube-conundrum/input_test.txt b/input/2023/02-cube-conundrum/input_test.txt similarity index 100% rename from 2023/02-cube-conundrum/input_test.txt rename to input/2023/02-cube-conundrum/input_test.txt diff --git a/2023/03-gear-ratios/input.txt b/input/2023/03-gear-ratios/input.txt similarity index 100% rename from 2023/03-gear-ratios/input.txt rename to input/2023/03-gear-ratios/input.txt diff --git a/2023/03-gear-ratios/input_test.txt b/input/2023/03-gear-ratios/input_test.txt similarity index 100% rename from 2023/03-gear-ratios/input_test.txt rename to input/2023/03-gear-ratios/input_test.txt diff --git a/2023/04-scratchcards/input.txt b/input/2023/04-scratchcards/input.txt similarity index 100% rename from 2023/04-scratchcards/input.txt rename to input/2023/04-scratchcards/input.txt diff --git a/2023/04-scratchcards/input_test.txt b/input/2023/04-scratchcards/input_test.txt similarity index 100% rename from 2023/04-scratchcards/input_test.txt rename to input/2023/04-scratchcards/input_test.txt diff --git a/2023/06-wait-for-it/input.txt b/input/2023/06-wait-for-it/input.txt similarity index 100% rename from 2023/06-wait-for-it/input.txt rename to input/2023/06-wait-for-it/input.txt diff --git a/2023/06-wait-for-it/input_test.txt b/input/2023/06-wait-for-it/input_test.txt similarity index 100% rename from 2023/06-wait-for-it/input_test.txt rename to input/2023/06-wait-for-it/input_test.txt diff --git a/2023/01-trebuchet/src/part_one.rs b/src/2023/01-trebuchet/part_one.rs similarity index 100% rename from 2023/01-trebuchet/src/part_one.rs rename to src/2023/01-trebuchet/part_one.rs diff --git a/2023/01-trebuchet/src/part_two.rs b/src/2023/01-trebuchet/part_two.rs similarity index 100% rename from 2023/01-trebuchet/src/part_two.rs rename to src/2023/01-trebuchet/part_two.rs diff --git a/2023/02-cube-conundrum/src/part_one.rs b/src/2023/02-cube-conundrum/part_one.rs similarity index 100% rename from 2023/02-cube-conundrum/src/part_one.rs rename to src/2023/02-cube-conundrum/part_one.rs diff --git a/2023/02-cube-conundrum/src/part_two.rs b/src/2023/02-cube-conundrum/part_two.rs similarity index 100% rename from 2023/02-cube-conundrum/src/part_two.rs rename to src/2023/02-cube-conundrum/part_two.rs diff --git a/src/2023/03-gear-ratios/input.txt b/src/2023/03-gear-ratios/input.txt new file mode 100644 index 0000000..f4ced43 --- /dev/null +++ b/src/2023/03-gear-ratios/input.txt @@ -0,0 +1,140 @@ +......644............612.......254..638..............802.................................118.....................................317.691.... +.....*......321..176....+........&...=...906........*.......=518................994..938.*.....579....35....155...........320...........$... +...939.@225........*......................$........41......................./.....+......102....*.....*...............603....*.413=......... +............470.128...288...+......442-.......191.....%....&........360....218...............916.....552.171..747.51......111............... +.........................*.176...........62%...*......715.831.........*.........937....782...............*...*..../..............645........ +.688.......469&...=....284.....................49................318..182..............-........200......480.182....257...........%......... +...*....#..........197..........611....................#..159.71.*......../947..............190....................=....33..158......252.... +....537.900......................*......404%.........172......*.....+741...................*..................299......*.......*............ +...............908.............354............#............15..881....................@.939...................+.....286...945..767.......... +...........749....*...811..........*667......257.+309.477-.*..............212......746.........275*.............264......*..........720..... +.....-827.*....279....*.........319........................257.150.118.....#.......................979...377...%..........167......*.....793 +...........197.....298.....782.................................-.......87......534.......182+.....................................546...*... +...............................756....@............609................./..363.*..............................459....77................450... +....255.........................$......324.........-..............@...........486...191......947...........................$161..218........ +..........993...............73....523..................832...71....639..............*...736*....-.367...............109$..........#......370 +............-........................&.......254..712....*....*.....................945.....260.....@.......97.......................+...... +..............................569..............-.....&.82..512..............................................+............913..&888..695.%... +......221.146...*....../.............$..@............................../417........571................481#.....687.157..*...............926. +......./...-..897......621.....885..247.506.............&........221..............-..........464.513............*........340.*.............. +...........................715*...............456......445...992*...............................*.....475....132..638.........171.340....561 +.................839..........................=....528...................-.........785............626....*.......*.....850-................. +....$.........&.........975....278...................$..............997...992.........*485.....84*......923...397..259...................... +..328.207......509.........*.....*.529.846......$..*............%...$............%386.......................$.........*..157.....994........ +........-..........352...184...707....*......454....512....208.926....188......%............573*....731....19....566.757...*....*....348.... +.....%..............#...................404.................&...........*..255.317..-..991......126............$.*..........704.788.....*... +.....992.....*979.................264...*..........@...........242#..171....*......888......35.......39......978.955....25/.........%.256... +..........760............904..155../..492...........306...................456...............=.......+............................694........ +................158.............@.........@..115................*922..651.................................................#.............297. +...677....46....*..........573......243.595....*....446......121...........748.868....................835.........621..-...300...22......... +...........*.635..............*....&..........910.&........................*...-.....-.........907.......#......$...*...43......*.......791. +.140.....218......17..........246.................705...............@......445.......471......*......-........623...326..................... +...=.126.........*.......411........979.630.............81.419...131.......................241........660................................... +.....*........540..*..........858@....*.*.................*....................................733.56......816..........243....984......=... +........546.........518.............21...834..@641.............46#.........471....................*...675.+.....................&......521.. +...........*..................65.....................978....&........................548#....749......*.....106.........246................. +.......899..593.........420-....*..258.798*814...#...*...336....................360.........+......238.......*........../........354........ +..........&...................485....*.........361.520........742...683..........@....136................=.......=.................#.....76. +......$.........575...............220....#.......................*....*...800...........=.....545.....178....*.604.+....659*670.........*... +.......959....@......175....*825.......838..........547*..........346.553..*....867..........*.............60.......130................517.. +.............121.737*....773....................124.....609...$............16......%..........359..90.............................627....... +....485.........................*884.194../.........485......489..............138.......426..........*527.620..-600...99.....778............ +..........510*236........344/.51......*...766..588.@.....66*............$665....%.......-....366..........%..............999..%............. +......259........................../..525.......*...........447..128..............961........................752....797.$.......+872..739... +......*.....................771.840.......*537...493....@..........*.404*376..759....=.....287..................*....#....614.........*..... +......896.......513...........*........208.............4..11.....475............*.........*...........346.171...104........*..........34.... +..........292...............@................570+...58.....*..&.......@455.......275......781..162.87.*......*....../.....927..+220......883 +...........*..390........937.............680..........+.845....998..........890....................*...240...128....102.............788.#... +....365/..914..*....988.........439@......*...$472...................10./....*......*....894.......475...........@.......................... +.........................192..............851..........332...........*...868..351..160......*830................776............608...-...52. +.......542.+.....966...............................114*......345...984...........................708*...264.......................*.611.*... +.......*....45....*....@.......@344.........................*..........809*958........$..............86...........-727.........359......700. +.......45.........111...285...........23...306*755........720......727..............383.....65.250.......943...........@651................. +............98................192......*............*...........................*.......503..@..*.....=.......48..............814........... +...............932.918..............787......13&.918.326......545...............857.....*........175...682......*939..552.707..*............ +......@....663*....#..............................................39..................265...................328.......*......*..810.....=186 +295....97...............654..774.........505..........857..-8.......*613.....................734...$....382*........%.....591............... +..................62......*....%..........*....110...$...................928....393.789*69....#....470.......977.904................898..... +........405...819.........514..............201....*....*806.....196......*........*............../...........@..................644....*195. +........*......*.................@.....276......538.992...........*....720.692..880........+117.266..207.........+.......................... +........67....478..675*861...80..34.....*..+777..................313........*.......................*.........445.........200..*...@........ +..938......75..................*.....155.................................985..#........285.....181...160.....................$.872..595..... +....*..997*....................31.............148......946...........803.......195.......*.944...*.......551+........*...867................ +...233..........553.596...........436..........*..........*437..559-..*.............@..521.*......606..........519.226..........@........... +.....................*..268......%.....200..276....@...................793........22........773........./....1............*....603.@927.-... +..527..843...906..511.........$....698.*..........39......692&...............158$......912..............168............645...............264 +...&.........=..........152....849...*.667............365.........%.69*...............*......955...................................568...... +.......88........@25.......+.......104...............@.....474..210....788.-910.862...............728.%........876.......980..........*..... +446.....................$.................................*.....................*...#..../........@...544.........*.........*980...872...... +...*.........960=.....609...372..516....797*652...179......337.........*864..457..315.....207..............177..666......................... +.456..283..................*.......................*...51...........798.....................................#...........796.880............. +.....*.........%...+......341...........320.....410...*......767..........81+.953.........#.....51.......8.....@.........*...+.............. +.....704..48.407...864.........718..........606.......375.....%....$...........*...818.@...621..=....769*.....522.375...585..............389 +.746.....*................$.............&.....*..................225......=........*...887.................................................. +....../...67..............429...........569....355..58#.550.299......676.904.......29........787.817....@.....*....940........537....*...... +.....894..............564..........52+.....................*.....69.*..........773..........$........398......283...@....283........105..... +......................*...672.../........996.....171..*567...902*...290....324..@..............844.......#.................................. +...935...128*........892.....*...497................$..............................170................789..*........620...........484....... +....../......495..........612..................251*.......811.............660.........=....@669.............840.413.*.........#......@...... +..506............................-..../....=.......147.44.*...$............=......177..............539...............384......743.......24.. +...../...........818.505......962....777....180.........*...330......................*........895...*...........989...............13....%... +..................*.....*749..........................225......................136...295.433...*....612..$386..+.............907.*....=..... +....108..911.......230........449*499...........875..............211&..851.742.../.......*.....266..................293....../....854.548... +...........+..............61............&..........@..../....989.......#...............916.403.........................*........*........... +..690..........$..=553...*......202....312..............753.*....372...........268............/.......973...&.881...558..........58..321.... +...........=..997........452....%...........................629.....@..........*..........................495..*../......-..............*... +..142..55..98................................80-......528...................532.....69.........691@...........186..388.342............163... +......./........280.@.............350.............818*.................$..............*..............142....................248/............ +..896....$........*.893......350./............918.........158......%..574...444......297.656..........%............%..............369....... +.....*...191...264........+...%.....566.824....*..621........#..532...........#..904........*.....134...139..420...811....444*160..&........ +....112..................663...../..*....*..987......*...................896....&...........996......+..*.....*......................236.... +..............................434..57...79.........904....+...740.%.....*..............................72....447....................@....... +....#........474...480....................................307..*...447..612.....490......325.......715.............853...859................ +..424......................624*....../.../.......176...........714.................*..........184..............874*....................687.. +........81.944..748..............=.343.643......*.......=...................=.....735....../..*........................748........743....... +261/......*.....*........@....574..............119......650................298.43........954...202...314................#....589....%....... +...............773....729...-.....361.884%.......................743.............*885...................*..&......746..................262.. +...@.330....44.............349....*.........140*875.............*........................787....582...257.401........-.......219......*..... +499...*.....*.....977..........629..130.691...........629.....392.........*......23......$...................................*.......113.... +....929.383..616..*..................&...@.....&...................910.....820..*......................................926..54...80*........ +...........*......60.....755.........../....105..........521...254*.............667.......956.......-...............*..=............232..... +.........667.-795....292*..............299...............*................560....................750.........107...157...................... +...........................200/..619.......225....986.67.....................=..386...................#62.......*..................827*242.. +.........134..528.....416.........@..........*.......*............18.............*.....489.396..384.......735.424.....528................... +.....326........*..98...+.....306.....+.....147............*.....#........391.....210.....*....*.....#.....*......754....................... +...........884.313...*...........*378..498...........844....738.......926.*...........809......562...108.391.........*567...748..../........ +.....857..*..........758.............................*............../...@..738....198..........................190..................470..... +......*....586..855.........*77.412*13....113..413..810.......513..266............./...@556.650...............$............................. +...487.........-.........434................&./..............*............631.................*.......................434................... +...............................660......848..............................*............+........316....105...679.......*....289..700......... +.62*523........../.............&.......*.....433...328.................474....194...868................*...%........753....*.....*.......... +................31........116...........897....*..%...........406............%.........................33.....453=......636..580.649..520... +...66......#..............@....732....+.............71...857...../..645...................#.......71.......&.......450......&........=...... +.....*815...387......677............395.....365.928....................*300.........633....347....*..791..37......&......................... +................898...*.....823.................................................621.*...........764.*........285@.......743......+..645..... +..............*.....946................................563.....................*.....848............716...................*.......8...$..... +.....770...281.710......756..305........709*......#..../................245....572...........788.............-112....994.522................ +........@.....................*.....648.....351....679..........624........*................&...........-............*...................... +..546*......................333....@....-...............*..........*...778.546...........=........=.....957......#....522......734.......... +......270...959...108..569...............616.........417.246.......25.+........237....909.........10..............607......=...+...711...... +...........@.........*...@...*...244.301......................................*.......................................713.59................ +.114..717...........807.....501..#...*...................+.63..369..986..137...831...../584...@350...........61.=923...........82........... +....*....*............................479.........#....141...%..*..@........*............................419*.................*............. +.........607..+609.....952.........................287...................730..945...&..-...986.........$.........#...........876....806..... +...................102*....................................+97...608..........*...541..221.........592.165.....266...742.................... +..................................414...............................*439....684.@..................................=...*.....325.621....831. +........924.918.325...........248*.......986..595*601...........................434......499...801.....586......395...251.....&...*......... +..............*..+..32....73*.........&.../.............772.......271.404..403......791...........*...*.....920..................20....136.. +..529....610.935...+.........589....265..............................*.......*......*...........884....849.....*............#.........*..... +..........*................................694........292.629..............967.....744..........................452......290........818..... +886.......206..............*6.......595=.....*.85........*..............................286..$...23.....436................................. +.....406.........61*68..960................689....916........887....908....217....$.....*....997.*.........%................501....912...... +.....-...412...$................632...............+..........@...............*....412..602........830..............856*.............#....... +........*.....407......32......=..............%.........670......3...272......78............................853........862...428..........4. +....747....97.......................585*312..664...731*...@.&426.*.......243.....894...............896.......&...287...........*.839..724... +......*..%.../..........948.......................................67.....@.........*..598...........*....#..................5.88.*....%..... +...287..634.....359....................439......251.............7....961......#..139.*...........746......760....682.............136........ +............493*................410............*...........618..*...*....85..189......81............................$...568................. +.........................627../.........355.....258........*........738....*........................&..................+..............259... +......515*357............*...509..205.....*................203..7*........353.....209..........790...81.........................695.....*... +........................313............444....................................662..........................956.....714...................935 \ No newline at end of file diff --git a/src/2023/03-gear-ratios/input_test.txt b/src/2023/03-gear-ratios/input_test.txt new file mode 100644 index 0000000..624ea4f --- /dev/null +++ b/src/2023/03-gear-ratios/input_test.txt @@ -0,0 +1,10 @@ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. \ No newline at end of file diff --git a/2023/04-scratchcards/src/part_one.rs b/src/2023/04-scratchcards/part_one.rs similarity index 100% rename from 2023/04-scratchcards/src/part_one.rs rename to src/2023/04-scratchcards/part_one.rs diff --git a/2023/06-wait-for-it/src/part_one.rs b/src/2023/06-wait-for-it/part_one.rs similarity index 100% rename from 2023/06-wait-for-it/src/part_one.rs rename to src/2023/06-wait-for-it/part_one.rs diff --git a/2023/06-wait-for-it/src/part_two.rs b/src/2023/06-wait-for-it/part_two.rs similarity index 100% rename from 2023/06-wait-for-it/src/part_two.rs rename to src/2023/06-wait-for-it/part_two.rs diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} From 6d5fc9fb93ec51d88e76c46c4db20ee384e752e9 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Mon, 18 Dec 2023 20:02:55 +0100 Subject: [PATCH 2/6] Add back day 3 --- src/2023/03-gear-ratios/input.txt | 140 ------------------------ src/2023/03-gear-ratios/input_test.txt | 10 -- src/2023/03-gear-ratios/part_one.rs | 145 +++++++++++++++++++++++++ 3 files changed, 145 insertions(+), 150 deletions(-) delete mode 100644 src/2023/03-gear-ratios/input.txt delete mode 100644 src/2023/03-gear-ratios/input_test.txt create mode 100644 src/2023/03-gear-ratios/part_one.rs diff --git a/src/2023/03-gear-ratios/input.txt b/src/2023/03-gear-ratios/input.txt deleted file mode 100644 index f4ced43..0000000 --- a/src/2023/03-gear-ratios/input.txt +++ /dev/null @@ -1,140 +0,0 @@ -......644............612.......254..638..............802.................................118.....................................317.691.... -.....*......321..176....+........&...=...906........*.......=518................994..938.*.....579....35....155...........320...........$... -...939.@225........*......................$........41......................./.....+......102....*.....*...............603....*.413=......... -............470.128...288...+......442-.......191.....%....&........360....218...............916.....552.171..747.51......111............... -.........................*.176...........62%...*......715.831.........*.........937....782...............*...*..../..............645........ -.688.......469&...=....284.....................49................318..182..............-........200......480.182....257...........%......... -...*....#..........197..........611....................#..159.71.*......../947..............190....................=....33..158......252.... -....537.900......................*......404%.........172......*.....+741...................*..................299......*.......*............ -...............908.............354............#............15..881....................@.939...................+.....286...945..767.......... -...........749....*...811..........*667......257.+309.477-.*..............212......746.........275*.............264......*..........720..... -.....-827.*....279....*.........319........................257.150.118.....#.......................979...377...%..........167......*.....793 -...........197.....298.....782.................................-.......87......534.......182+.....................................546...*... -...............................756....@............609................./..363.*..............................459....77................450... -....255.........................$......324.........-..............@...........486...191......947...........................$161..218........ -..........993...............73....523..................832...71....639..............*...736*....-.367...............109$..........#......370 -............-........................&.......254..712....*....*.....................945.....260.....@.......97.......................+...... -..............................569..............-.....&.82..512..............................................+............913..&888..695.%... -......221.146...*....../.............$..@............................../417........571................481#.....687.157..*...............926. -......./...-..897......621.....885..247.506.............&........221..............-..........464.513............*........340.*.............. -...........................715*...............456......445...992*...............................*.....475....132..638.........171.340....561 -.................839..........................=....528...................-.........785............626....*.......*.....850-................. -....$.........&.........975....278...................$..............997...992.........*485.....84*......923...397..259...................... -..328.207......509.........*.....*.529.846......$..*............%...$............%386.......................$.........*..157.....994........ -........-..........352...184...707....*......454....512....208.926....188......%............573*....731....19....566.757...*....*....348.... -.....%..............#...................404.................&...........*..255.317..-..991......126............$.*..........704.788.....*... -.....992.....*979.................264...*..........@...........242#..171....*......888......35.......39......978.955....25/.........%.256... -..........760............904..155../..492...........306...................456...............=.......+............................694........ -................158.............@.........@..115................*922..651.................................................#.............297. -...677....46....*..........573......243.595....*....446......121...........748.868....................835.........621..-...300...22......... -...........*.635..............*....&..........910.&........................*...-.....-.........907.......#......$...*...43......*.......791. -.140.....218......17..........246.................705...............@......445.......471......*......-........623...326..................... -...=.126.........*.......411........979.630.............81.419...131.......................241........660................................... -.....*........540..*..........858@....*.*.................*....................................733.56......816..........243....984......=... -........546.........518.............21...834..@641.............46#.........471....................*...675.+.....................&......521.. -...........*..................65.....................978....&........................548#....749......*.....106.........246................. -.......899..593.........420-....*..258.798*814...#...*...336....................360.........+......238.......*........../........354........ -..........&...................485....*.........361.520........742...683..........@....136................=.......=.................#.....76. -......$.........575...............220....#.......................*....*...800...........=.....545.....178....*.604.+....659*670.........*... -.......959....@......175....*825.......838..........547*..........346.553..*....867..........*.............60.......130................517.. -.............121.737*....773....................124.....609...$............16......%..........359..90.............................627....... -....485.........................*884.194../.........485......489..............138.......426..........*527.620..-600...99.....778............ -..........510*236........344/.51......*...766..588.@.....66*............$665....%.......-....366..........%..............999..%............. -......259........................../..525.......*...........447..128..............961........................752....797.$.......+872..739... -......*.....................771.840.......*537...493....@..........*.404*376..759....=.....287..................*....#....614.........*..... -......896.......513...........*........208.............4..11.....475............*.........*...........346.171...104........*..........34.... -..........292...............@................570+...58.....*..&.......@455.......275......781..162.87.*......*....../.....927..+220......883 -...........*..390........937.............680..........+.845....998..........890....................*...240...128....102.............788.#... -....365/..914..*....988.........439@......*...$472...................10./....*......*....894.......475...........@.......................... -.........................192..............851..........332...........*...868..351..160......*830................776............608...-...52. -.......542.+.....966...............................114*......345...984...........................708*...264.......................*.611.*... -.......*....45....*....@.......@344.........................*..........809*958........$..............86...........-727.........359......700. -.......45.........111...285...........23...306*755........720......727..............383.....65.250.......943...........@651................. -............98................192......*............*...........................*.......503..@..*.....=.......48..............814........... -...............932.918..............787......13&.918.326......545...............857.....*........175...682......*939..552.707..*............ -......@....663*....#..............................................39..................265...................328.......*......*..810.....=186 -295....97...............654..774.........505..........857..-8.......*613.....................734...$....382*........%.....591............... -..................62......*....%..........*....110...$...................928....393.789*69....#....470.......977.904................898..... -........405...819.........514..............201....*....*806.....196......*........*............../...........@..................644....*195. -........*......*.................@.....276......538.992...........*....720.692..880........+117.266..207.........+.......................... -........67....478..675*861...80..34.....*..+777..................313........*.......................*.........445.........200..*...@........ -..938......75..................*.....155.................................985..#........285.....181...160.....................$.872..595..... -....*..997*....................31.............148......946...........803.......195.......*.944...*.......551+........*...867................ -...233..........553.596...........436..........*..........*437..559-..*.............@..521.*......606..........519.226..........@........... -.....................*..268......%.....200..276....@...................793........22........773........./....1............*....603.@927.-... -..527..843...906..511.........$....698.*..........39......692&...............158$......912..............168............645...............264 -...&.........=..........152....849...*.667............365.........%.69*...............*......955...................................568...... -.......88........@25.......+.......104...............@.....474..210....788.-910.862...............728.%........876.......980..........*..... -446.....................$.................................*.....................*...#..../........@...544.........*.........*980...872...... -...*.........960=.....609...372..516....797*652...179......337.........*864..457..315.....207..............177..666......................... -.456..283..................*.......................*...51...........798.....................................#...........796.880............. -.....*.........%...+......341...........320.....410...*......767..........81+.953.........#.....51.......8.....@.........*...+.............. -.....704..48.407...864.........718..........606.......375.....%....$...........*...818.@...621..=....769*.....522.375...585..............389 -.746.....*................$.............&.....*..................225......=........*...887.................................................. -....../...67..............429...........569....355..58#.550.299......676.904.......29........787.817....@.....*....940........537....*...... -.....894..............564..........52+.....................*.....69.*..........773..........$........398......283...@....283........105..... -......................*...672.../........996.....171..*567...902*...290....324..@..............844.......#.................................. -...935...128*........892.....*...497................$..............................170................789..*........620...........484....... -....../......495..........612..................251*.......811.............660.........=....@669.............840.413.*.........#......@...... -..506............................-..../....=.......147.44.*...$............=......177..............539...............384......743.......24.. -...../...........818.505......962....777....180.........*...330......................*........895...*...........989...............13....%... -..................*.....*749..........................225......................136...295.433...*....612..$386..+.............907.*....=..... -....108..911.......230........449*499...........875..............211&..851.742.../.......*.....266..................293....../....854.548... -...........+..............61............&..........@..../....989.......#...............916.403.........................*........*........... -..690..........$..=553...*......202....312..............753.*....372...........268............/.......973...&.881...558..........58..321.... -...........=..997........452....%...........................629.....@..........*..........................495..*../......-..............*... -..142..55..98................................80-......528...................532.....69.........691@...........186..388.342............163... -......./........280.@.............350.............818*.................$..............*..............142....................248/............ -..896....$........*.893......350./............918.........158......%..574...444......297.656..........%............%..............369....... -.....*...191...264........+...%.....566.824....*..621........#..532...........#..904........*.....134...139..420...811....444*160..&........ -....112..................663...../..*....*..987......*...................896....&...........996......+..*.....*......................236.... -..............................434..57...79.........904....+...740.%.....*..............................72....447....................@....... -....#........474...480....................................307..*...447..612.....490......325.......715.............853...859................ -..424......................624*....../.../.......176...........714.................*..........184..............874*....................687.. -........81.944..748..............=.343.643......*.......=...................=.....735....../..*........................748........743....... -261/......*.....*........@....574..............119......650................298.43........954...202...314................#....589....%....... -...............773....729...-.....361.884%.......................743.............*885...................*..&......746..................262.. -...@.330....44.............349....*.........140*875.............*........................787....582...257.401........-.......219......*..... -499...*.....*.....977..........629..130.691...........629.....392.........*......23......$...................................*.......113.... -....929.383..616..*..................&...@.....&...................910.....820..*......................................926..54...80*........ -...........*......60.....755.........../....105..........521...254*.............667.......956.......-...............*..=............232..... -.........667.-795....292*..............299...............*................560....................750.........107...157...................... -...........................200/..619.......225....986.67.....................=..386...................#62.......*..................827*242.. -.........134..528.....416.........@..........*.......*............18.............*.....489.396..384.......735.424.....528................... -.....326........*..98...+.....306.....+.....147............*.....#........391.....210.....*....*.....#.....*......754....................... -...........884.313...*...........*378..498...........844....738.......926.*...........809......562...108.391.........*567...748..../........ -.....857..*..........758.............................*............../...@..738....198..........................190..................470..... -......*....586..855.........*77.412*13....113..413..810.......513..266............./...@556.650...............$............................. -...487.........-.........434................&./..............*............631.................*.......................434................... -...............................660......848..............................*............+........316....105...679.......*....289..700......... -.62*523........../.............&.......*.....433...328.................474....194...868................*...%........753....*.....*.......... -................31........116...........897....*..%...........406............%.........................33.....453=......636..580.649..520... -...66......#..............@....732....+.............71...857...../..645...................#.......71.......&.......450......&........=...... -.....*815...387......677............395.....365.928....................*300.........633....347....*..791..37......&......................... -................898...*.....823.................................................621.*...........764.*........285@.......743......+..645..... -..............*.....946................................563.....................*.....848............716...................*.......8...$..... -.....770...281.710......756..305........709*......#..../................245....572...........788.............-112....994.522................ -........@.....................*.....648.....351....679..........624........*................&...........-............*...................... -..546*......................333....@....-...............*..........*...778.546...........=........=.....957......#....522......734.......... -......270...959...108..569...............616.........417.246.......25.+........237....909.........10..............607......=...+...711...... -...........@.........*...@...*...244.301......................................*.......................................713.59................ -.114..717...........807.....501..#...*...................+.63..369..986..137...831...../584...@350...........61.=923...........82........... -....*....*............................479.........#....141...%..*..@........*............................419*.................*............. -.........607..+609.....952.........................287...................730..945...&..-...986.........$.........#...........876....806..... -...................102*....................................+97...608..........*...541..221.........592.165.....266...742.................... -..................................414...............................*439....684.@..................................=...*.....325.621....831. -........924.918.325...........248*.......986..595*601...........................434......499...801.....586......395...251.....&...*......... -..............*..+..32....73*.........&.../.............772.......271.404..403......791...........*...*.....920..................20....136.. -..529....610.935...+.........589....265..............................*.......*......*...........884....849.....*............#.........*..... -..........*................................694........292.629..............967.....744..........................452......290........818..... -886.......206..............*6.......595=.....*.85........*..............................286..$...23.....436................................. -.....406.........61*68..960................689....916........887....908....217....$.....*....997.*.........%................501....912...... -.....-...412...$................632...............+..........@...............*....412..602........830..............856*.............#....... -........*.....407......32......=..............%.........670......3...272......78............................853........862...428..........4. -....747....97.......................585*312..664...731*...@.&426.*.......243.....894...............896.......&...287...........*.839..724... -......*..%.../..........948.......................................67.....@.........*..598...........*....#..................5.88.*....%..... -...287..634.....359....................439......251.............7....961......#..139.*...........746......760....682.............136........ -............493*................410............*...........618..*...*....85..189......81............................$...568................. -.........................627../.........355.....258........*........738....*........................&..................+..............259... -......515*357............*...509..205.....*................203..7*........353.....209..........790...81.........................695.....*... -........................313............444....................................662..........................956.....714...................935 \ No newline at end of file diff --git a/src/2023/03-gear-ratios/input_test.txt b/src/2023/03-gear-ratios/input_test.txt deleted file mode 100644 index 624ea4f..0000000 --- a/src/2023/03-gear-ratios/input_test.txt +++ /dev/null @@ -1,10 +0,0 @@ -467..114.. -...*...... -..35..633. -......#... -617*...... -.....+.58. -..592..... -......755. -...$.*.... -.664.598.. \ No newline at end of file diff --git a/src/2023/03-gear-ratios/part_one.rs b/src/2023/03-gear-ratios/part_one.rs new file mode 100644 index 0000000..e7c5ad1 --- /dev/null +++ b/src/2023/03-gear-ratios/part_one.rs @@ -0,0 +1,145 @@ +use std::fs::File; +use std::io::{self, BufRead}; +use std::path::Path; + +enum Engine { + Symbol(char), + Number(u32), + Empty, +} + +pub fn part_one() { + if let Ok(lines) = read_lines("./input.txt") { + let matrix = create_matrix(lines); + let result = process_matrix(&matrix); + + println!("{}", result); + } +} + +fn read_lines

(filename: P) -> io::Result>> +where + P: AsRef, +{ + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +} + +fn create_matrix(lines: io::Lines>) -> Vec> { + let mut matrix: Vec> = Vec::new(); + + for (i, line) in lines.enumerate() { + matrix.push(Vec::new()); + + let line = line.unwrap(); + + for ch in line.chars() { + if ch == '.' { + matrix[i].push(Engine::Empty); + } else if ch.is_digit(10) { + matrix[i].push(Engine::Number(ch.to_digit(10).unwrap())); + } else { + matrix[i].push(Engine::Symbol(ch)); + } + } + } + + return matrix; +} + +fn process_matrix(matrix: &Vec>) -> u32 { + let mut result = 0; + + for i in 0..matrix.len() { + let mut is_num = false; + let mut num = 0; + let mut found_symbol = false; + + for j in 0..matrix[i].len() { + match matrix[i][j] { + Engine::Number(value) => { + if !found_symbol { + found_symbol = check_symbol(&matrix, i, j); + } + + if is_num { + num = num * 10 + value; + } else { + num = value; + } + + is_num = true; + } + _ => { + if is_num && found_symbol { + result += num; + } + + is_num = false; + found_symbol = false; + num = 0; + } + } + } + + if is_num && found_symbol { + result += num; + } + } + + return result; +} + +fn check_symbol(matrix: &Vec>, x: usize, y: usize) -> bool { + // There's probably a better way + + if x > 0 { + if let Engine::Symbol(_) = matrix[x - 1][y] { + return true; + } + + if y > 0 { + if let Engine::Symbol(_) = matrix[x - 1][y - 1] { + return true; + } + } + + if y < matrix[x - 1].len() - 1 { + if let Engine::Symbol(_) = matrix[x - 1][y + 1] { + return true; + } + } + } + + if y > 0 { + if let Engine::Symbol(_) = matrix[x][y - 1] { + return true; + } + } + + if y < matrix[x].len() - 1 { + if let Engine::Symbol(_) = matrix[x][y + 1] { + return true; + } + } + + if x < matrix.len() - 1 { + if let Engine::Symbol(_) = matrix[x + 1][y] { + return true; + } + + if y > 0 { + if let Engine::Symbol(_) = matrix[x + 1][y - 1] { + return true; + } + } + + if y < matrix[x + 1].len() - 1 { + if let Engine::Symbol(_) = matrix[x + 1][y + 1] { + return true; + } + } + } + + return false; +} From a3752c5cee3e9f8fb56a26c2218d331b6bc82884 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Mon, 18 Dec 2023 20:52:02 +0100 Subject: [PATCH 3/6] Rename modules --- .vscode/settings.json | 6 ++++++ src/main.rs | 2 ++ src/year_2023/day_01_trebuchet/mod.rs | 2 ++ .../01-trebuchet => year_2023/day_01_trebuchet}/part_one.rs | 0 .../01-trebuchet => year_2023/day_01_trebuchet}/part_two.rs | 0 src/year_2023/day_02_cube_conundrum/mod.rs | 2 ++ .../day_02_cube_conundrum}/part_one.rs | 0 .../day_02_cube_conundrum}/part_two.rs | 0 src/year_2023/day_03_gear_ratios/mod.rs | 1 + .../day_03_gear_ratios}/part_one.rs | 0 src/year_2023/day_04_scratchcards/mod.rs | 1 + .../day_04_scratchcards}/part_one.rs | 0 src/year_2023/day_06_wait_for_it/mod.rs | 2 ++ .../day_06_wait_for_it}/part_one.rs | 0 .../day_06_wait_for_it}/part_two.rs | 0 src/year_2023/mod.rs | 5 +++++ 16 files changed, 21 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 src/year_2023/day_01_trebuchet/mod.rs rename src/{2023/01-trebuchet => year_2023/day_01_trebuchet}/part_one.rs (100%) rename src/{2023/01-trebuchet => year_2023/day_01_trebuchet}/part_two.rs (100%) create mode 100644 src/year_2023/day_02_cube_conundrum/mod.rs rename src/{2023/02-cube-conundrum => year_2023/day_02_cube_conundrum}/part_one.rs (100%) rename src/{2023/02-cube-conundrum => year_2023/day_02_cube_conundrum}/part_two.rs (100%) create mode 100644 src/year_2023/day_03_gear_ratios/mod.rs rename src/{2023/03-gear-ratios => year_2023/day_03_gear_ratios}/part_one.rs (100%) create mode 100644 src/year_2023/day_04_scratchcards/mod.rs rename src/{2023/04-scratchcards => year_2023/day_04_scratchcards}/part_one.rs (100%) create mode 100644 src/year_2023/day_06_wait_for_it/mod.rs rename src/{2023/06-wait-for-it => year_2023/day_06_wait_for_it}/part_one.rs (100%) rename src/{2023/06-wait-for-it => year_2023/day_06_wait_for_it}/part_two.rs (100%) create mode 100644 src/year_2023/mod.rs diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e47d9bb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "rust-analyzer.linkedProjects": [ + "./Cargo.toml", + "./Cargo.toml" + ] +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e7a11a9..03ed285 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +pub mod year_2023; + fn main() { println!("Hello, world!"); } diff --git a/src/year_2023/day_01_trebuchet/mod.rs b/src/year_2023/day_01_trebuchet/mod.rs new file mode 100644 index 0000000..5109e4e --- /dev/null +++ b/src/year_2023/day_01_trebuchet/mod.rs @@ -0,0 +1,2 @@ +pub mod part_one; +pub mod part_two; \ No newline at end of file diff --git a/src/2023/01-trebuchet/part_one.rs b/src/year_2023/day_01_trebuchet/part_one.rs similarity index 100% rename from src/2023/01-trebuchet/part_one.rs rename to src/year_2023/day_01_trebuchet/part_one.rs diff --git a/src/2023/01-trebuchet/part_two.rs b/src/year_2023/day_01_trebuchet/part_two.rs similarity index 100% rename from src/2023/01-trebuchet/part_two.rs rename to src/year_2023/day_01_trebuchet/part_two.rs diff --git a/src/year_2023/day_02_cube_conundrum/mod.rs b/src/year_2023/day_02_cube_conundrum/mod.rs new file mode 100644 index 0000000..5109e4e --- /dev/null +++ b/src/year_2023/day_02_cube_conundrum/mod.rs @@ -0,0 +1,2 @@ +pub mod part_one; +pub mod part_two; \ No newline at end of file diff --git a/src/2023/02-cube-conundrum/part_one.rs b/src/year_2023/day_02_cube_conundrum/part_one.rs similarity index 100% rename from src/2023/02-cube-conundrum/part_one.rs rename to src/year_2023/day_02_cube_conundrum/part_one.rs diff --git a/src/2023/02-cube-conundrum/part_two.rs b/src/year_2023/day_02_cube_conundrum/part_two.rs similarity index 100% rename from src/2023/02-cube-conundrum/part_two.rs rename to src/year_2023/day_02_cube_conundrum/part_two.rs diff --git a/src/year_2023/day_03_gear_ratios/mod.rs b/src/year_2023/day_03_gear_ratios/mod.rs new file mode 100644 index 0000000..8783a16 --- /dev/null +++ b/src/year_2023/day_03_gear_ratios/mod.rs @@ -0,0 +1 @@ +pub mod part_one; diff --git a/src/2023/03-gear-ratios/part_one.rs b/src/year_2023/day_03_gear_ratios/part_one.rs similarity index 100% rename from src/2023/03-gear-ratios/part_one.rs rename to src/year_2023/day_03_gear_ratios/part_one.rs diff --git a/src/year_2023/day_04_scratchcards/mod.rs b/src/year_2023/day_04_scratchcards/mod.rs new file mode 100644 index 0000000..8783a16 --- /dev/null +++ b/src/year_2023/day_04_scratchcards/mod.rs @@ -0,0 +1 @@ +pub mod part_one; diff --git a/src/2023/04-scratchcards/part_one.rs b/src/year_2023/day_04_scratchcards/part_one.rs similarity index 100% rename from src/2023/04-scratchcards/part_one.rs rename to src/year_2023/day_04_scratchcards/part_one.rs diff --git a/src/year_2023/day_06_wait_for_it/mod.rs b/src/year_2023/day_06_wait_for_it/mod.rs new file mode 100644 index 0000000..5109e4e --- /dev/null +++ b/src/year_2023/day_06_wait_for_it/mod.rs @@ -0,0 +1,2 @@ +pub mod part_one; +pub mod part_two; \ No newline at end of file diff --git a/src/2023/06-wait-for-it/part_one.rs b/src/year_2023/day_06_wait_for_it/part_one.rs similarity index 100% rename from src/2023/06-wait-for-it/part_one.rs rename to src/year_2023/day_06_wait_for_it/part_one.rs diff --git a/src/2023/06-wait-for-it/part_two.rs b/src/year_2023/day_06_wait_for_it/part_two.rs similarity index 100% rename from src/2023/06-wait-for-it/part_two.rs rename to src/year_2023/day_06_wait_for_it/part_two.rs diff --git a/src/year_2023/mod.rs b/src/year_2023/mod.rs new file mode 100644 index 0000000..4f53189 --- /dev/null +++ b/src/year_2023/mod.rs @@ -0,0 +1,5 @@ +pub mod day_01_trebuchet; +pub mod day_02_cube_conundrum; +pub mod day_03_gear_ratios; +pub mod day_04_scratchcards; +pub mod day_06_wait_for_it; From 3d6b90e328e76c30d6e7ba7e4c50f35aee3785a2 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Mon, 18 Dec 2023 21:10:48 +0100 Subject: [PATCH 4/6] Move read_lines to utils --- src/main.rs | 1 + src/utils/input_reader.rs | 13 +++++++++++++ src/utils/mod.rs | 1 + src/year_2023/day_01_trebuchet/part_one.rs | 15 ++------------- src/year_2023/day_01_trebuchet/part_two.rs | 14 +++----------- src/year_2023/day_02_cube_conundrum/part_one.rs | 14 ++------------ src/year_2023/day_02_cube_conundrum/part_two.rs | 15 ++++----------- src/year_2023/day_03_gear_ratios/part_one.rs | 15 ++++----------- src/year_2023/day_04_scratchcards/part_one.rs | 14 +++----------- src/year_2023/day_06_wait_for_it/part_one.rs | 14 +++----------- src/year_2023/day_06_wait_for_it/part_two.rs | 14 +++----------- 11 files changed, 39 insertions(+), 91 deletions(-) create mode 100644 src/utils/input_reader.rs create mode 100644 src/utils/mod.rs diff --git a/src/main.rs b/src/main.rs index 03ed285..5d26c58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ pub mod year_2023; +pub mod utils; fn main() { println!("Hello, world!"); diff --git a/src/utils/input_reader.rs b/src/utils/input_reader.rs new file mode 100644 index 0000000..e7b2fb4 --- /dev/null +++ b/src/utils/input_reader.rs @@ -0,0 +1,13 @@ +// https://doc.rust-lang.org/stable/rust-by-example/std_misc/file/read_lines.html +use std::fs::File; +use std::io::{self, BufRead}; +use std::path::Path; + + +pub fn read_lines

(filename: P) -> io::Result>> +where + P: AsRef, +{ + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +} \ No newline at end of file diff --git a/src/utils/mod.rs b/src/utils/mod.rs new file mode 100644 index 0000000..44b92ab --- /dev/null +++ b/src/utils/mod.rs @@ -0,0 +1 @@ +pub mod input_reader; \ No newline at end of file diff --git a/src/year_2023/day_01_trebuchet/part_one.rs b/src/year_2023/day_01_trebuchet/part_one.rs index 98fa680..59e923a 100644 --- a/src/year_2023/day_01_trebuchet/part_one.rs +++ b/src/year_2023/day_01_trebuchet/part_one.rs @@ -1,10 +1,7 @@ -// https://doc.rust-lang.org/stable/rust-by-example/std_misc/file/read_lines.html -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; +use crate::utils::input_reader; pub fn part_one() { - if let Ok(lines) = read_lines("./input.txt") { + if let Ok(lines) = input_reader::read_lines("./input.txt") { let mut result = 0; for line in lines { @@ -15,14 +12,6 @@ pub fn part_one() { } } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} - fn parse_line(s: &str) -> u32 { const RADIX: u32 = 10; diff --git a/src/year_2023/day_01_trebuchet/part_two.rs b/src/year_2023/day_01_trebuchet/part_two.rs index 37c7ce0..0f3ae59 100644 --- a/src/year_2023/day_01_trebuchet/part_two.rs +++ b/src/year_2023/day_01_trebuchet/part_two.rs @@ -1,10 +1,9 @@ use std::collections::HashMap; -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; + +use crate::utils::input_reader; pub fn part_two() { - if let Ok(lines) = read_lines("./input.txt") { + if let Ok(lines) = input_reader::read_lines("./input.txt") { let mut result = 0; for line in lines { @@ -15,13 +14,6 @@ pub fn part_two() { } } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} fn parse_line(s: &str) -> u32 { const RADIX: u32 = 10; diff --git a/src/year_2023/day_02_cube_conundrum/part_one.rs b/src/year_2023/day_02_cube_conundrum/part_one.rs index f14bcbf..45c725a 100644 --- a/src/year_2023/day_02_cube_conundrum/part_one.rs +++ b/src/year_2023/day_02_cube_conundrum/part_one.rs @@ -1,9 +1,7 @@ -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; +use crate::utils::input_reader; pub fn part_one() { - if let Ok(lines) = read_lines("./input.txt") { + if let Ok(lines) = input_reader::read_lines("./input.txt") { let mut result = 0; for line in lines { @@ -14,14 +12,6 @@ pub fn part_one() { } } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} - fn process_line(s: &str) -> u32 { let v: Vec<&str> = s.split(':').collect(); diff --git a/src/year_2023/day_02_cube_conundrum/part_two.rs b/src/year_2023/day_02_cube_conundrum/part_two.rs index 671eb80..7b42d58 100644 --- a/src/year_2023/day_02_cube_conundrum/part_two.rs +++ b/src/year_2023/day_02_cube_conundrum/part_two.rs @@ -1,7 +1,6 @@ use std::cmp::max; -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; + +use crate::utils::input_reader; struct Game { red: u32, @@ -10,7 +9,7 @@ struct Game { } pub fn part_two() { - if let Ok(lines) = read_lines("./input.txt") { + if let Ok(lines) = input_reader::read_lines("./input.txt") { let mut result = 0; for line in lines { @@ -21,13 +20,7 @@ pub fn part_two() { } } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} + fn process_line(s: &str) -> u32 { let v: Vec<&str> = s.split(':').collect(); diff --git a/src/year_2023/day_03_gear_ratios/part_one.rs b/src/year_2023/day_03_gear_ratios/part_one.rs index e7c5ad1..8d6e24c 100644 --- a/src/year_2023/day_03_gear_ratios/part_one.rs +++ b/src/year_2023/day_03_gear_ratios/part_one.rs @@ -1,6 +1,6 @@ -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; +use std::{fs::File, io}; + +use crate::utils::input_reader; enum Engine { Symbol(char), @@ -9,7 +9,7 @@ enum Engine { } pub fn part_one() { - if let Ok(lines) = read_lines("./input.txt") { + if let Ok(lines) = input_reader::read_lines("./input.txt") { let matrix = create_matrix(lines); let result = process_matrix(&matrix); @@ -17,13 +17,6 @@ pub fn part_one() { } } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} fn create_matrix(lines: io::Lines>) -> Vec> { let mut matrix: Vec> = Vec::new(); diff --git a/src/year_2023/day_04_scratchcards/part_one.rs b/src/year_2023/day_04_scratchcards/part_one.rs index 6c5bcdc..35ee2cf 100644 --- a/src/year_2023/day_04_scratchcards/part_one.rs +++ b/src/year_2023/day_04_scratchcards/part_one.rs @@ -1,10 +1,9 @@ -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; use std::collections::HashSet; +use crate::utils::input_reader; + pub fn part_one() { - if let Ok(lines) = read_lines("./input.txt") { + if let Ok(lines) = input_reader::read_lines("./input.txt") { let mut result = 0; for line in lines { @@ -46,10 +45,3 @@ fn process_line(s: &str) -> u32 { } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} \ No newline at end of file diff --git a/src/year_2023/day_06_wait_for_it/part_one.rs b/src/year_2023/day_06_wait_for_it/part_one.rs index 93d1e1e..12c567a 100644 --- a/src/year_2023/day_06_wait_for_it/part_one.rs +++ b/src/year_2023/day_06_wait_for_it/part_one.rs @@ -1,9 +1,7 @@ -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; +use crate::utils::input_reader; pub fn part_one() { - if let Ok(lines) = read_lines("./input.txt") { + if let Ok(lines) = input_reader::read_lines("./input.txt") { let mut margin = 1; let mut race_results: Vec> = Vec::new(); @@ -41,10 +39,4 @@ fn process_race_result(time: u32, distance: u32) -> u32 { return win_count; } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} + diff --git a/src/year_2023/day_06_wait_for_it/part_two.rs b/src/year_2023/day_06_wait_for_it/part_two.rs index dfa2ab6..7f19ccd 100644 --- a/src/year_2023/day_06_wait_for_it/part_two.rs +++ b/src/year_2023/day_06_wait_for_it/part_two.rs @@ -1,9 +1,7 @@ -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; +use crate::utils::input_reader; pub fn part_two() { - if let Ok(lines) = read_lines("./input.txt") { + if let Ok(lines) = input_reader::read_lines("./input.txt") { let mut race_result: Vec = Vec::new(); for line in lines { @@ -38,10 +36,4 @@ fn process_race_result(time: u64, distance: u64) -> u64 { return win_count; } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} + From 439b15d11ef05d03134d06e0bea0896c3189968a Mon Sep 17 00:00:00 2001 From: LordMathis Date: Mon, 18 Dec 2023 21:11:04 +0100 Subject: [PATCH 5/6] Harmonize module and input naming --- .../{2023/01-trebuchet => year_2023/day_01_trebuchet}/input.txt | 0 .../01-trebuchet => year_2023/day_01_trebuchet}/input_test.txt | 0 .../day_02_cube_conundrum}/input.txt | 0 .../day_02_cube_conundrum}/input_test.txt | 0 .../03-gear-ratios => year_2023/day_03_gear_ratios}/input.txt | 0 .../day_03_gear_ratios}/input_test.txt | 0 .../04-scratchcards => year_2023/day_04_scratchcards}/input.txt | 0 .../day_04_scratchcards}/input_test.txt | 0 .../06-wait-for-it => year_2023/day_06_wait_for_it}/input.txt | 0 .../day_06_wait_for_it}/input_test.txt | 0 src/year_2023/day_01_trebuchet/part_one.rs | 2 +- 11 files changed, 1 insertion(+), 1 deletion(-) rename input/{2023/01-trebuchet => year_2023/day_01_trebuchet}/input.txt (100%) rename input/{2023/01-trebuchet => year_2023/day_01_trebuchet}/input_test.txt (100%) rename input/{2023/02-cube-conundrum => year_2023/day_02_cube_conundrum}/input.txt (100%) rename input/{2023/02-cube-conundrum => year_2023/day_02_cube_conundrum}/input_test.txt (100%) rename input/{2023/03-gear-ratios => year_2023/day_03_gear_ratios}/input.txt (100%) rename input/{2023/03-gear-ratios => year_2023/day_03_gear_ratios}/input_test.txt (100%) rename input/{2023/04-scratchcards => year_2023/day_04_scratchcards}/input.txt (100%) rename input/{2023/04-scratchcards => year_2023/day_04_scratchcards}/input_test.txt (100%) rename input/{2023/06-wait-for-it => year_2023/day_06_wait_for_it}/input.txt (100%) rename input/{2023/06-wait-for-it => year_2023/day_06_wait_for_it}/input_test.txt (100%) diff --git a/input/2023/01-trebuchet/input.txt b/input/year_2023/day_01_trebuchet/input.txt similarity index 100% rename from input/2023/01-trebuchet/input.txt rename to input/year_2023/day_01_trebuchet/input.txt diff --git a/input/2023/01-trebuchet/input_test.txt b/input/year_2023/day_01_trebuchet/input_test.txt similarity index 100% rename from input/2023/01-trebuchet/input_test.txt rename to input/year_2023/day_01_trebuchet/input_test.txt diff --git a/input/2023/02-cube-conundrum/input.txt b/input/year_2023/day_02_cube_conundrum/input.txt similarity index 100% rename from input/2023/02-cube-conundrum/input.txt rename to input/year_2023/day_02_cube_conundrum/input.txt diff --git a/input/2023/02-cube-conundrum/input_test.txt b/input/year_2023/day_02_cube_conundrum/input_test.txt similarity index 100% rename from input/2023/02-cube-conundrum/input_test.txt rename to input/year_2023/day_02_cube_conundrum/input_test.txt diff --git a/input/2023/03-gear-ratios/input.txt b/input/year_2023/day_03_gear_ratios/input.txt similarity index 100% rename from input/2023/03-gear-ratios/input.txt rename to input/year_2023/day_03_gear_ratios/input.txt diff --git a/input/2023/03-gear-ratios/input_test.txt b/input/year_2023/day_03_gear_ratios/input_test.txt similarity index 100% rename from input/2023/03-gear-ratios/input_test.txt rename to input/year_2023/day_03_gear_ratios/input_test.txt diff --git a/input/2023/04-scratchcards/input.txt b/input/year_2023/day_04_scratchcards/input.txt similarity index 100% rename from input/2023/04-scratchcards/input.txt rename to input/year_2023/day_04_scratchcards/input.txt diff --git a/input/2023/04-scratchcards/input_test.txt b/input/year_2023/day_04_scratchcards/input_test.txt similarity index 100% rename from input/2023/04-scratchcards/input_test.txt rename to input/year_2023/day_04_scratchcards/input_test.txt diff --git a/input/2023/06-wait-for-it/input.txt b/input/year_2023/day_06_wait_for_it/input.txt similarity index 100% rename from input/2023/06-wait-for-it/input.txt rename to input/year_2023/day_06_wait_for_it/input.txt diff --git a/input/2023/06-wait-for-it/input_test.txt b/input/year_2023/day_06_wait_for_it/input_test.txt similarity index 100% rename from input/2023/06-wait-for-it/input_test.txt rename to input/year_2023/day_06_wait_for_it/input_test.txt diff --git a/src/year_2023/day_01_trebuchet/part_one.rs b/src/year_2023/day_01_trebuchet/part_one.rs index 59e923a..c6eda37 100644 --- a/src/year_2023/day_01_trebuchet/part_one.rs +++ b/src/year_2023/day_01_trebuchet/part_one.rs @@ -1,7 +1,7 @@ use crate::utils::input_reader; pub fn part_one() { - if let Ok(lines) = input_reader::read_lines("./input.txt") { + if let Ok(lines) = input_reader::read_lines("./input/input.txt") { let mut result = 0; for line in lines { From e74ff527553ef78337ea98399da72ef692864104 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Mon, 18 Dec 2023 22:45:49 +0100 Subject: [PATCH 6/6] Make restructuring work --- .../day_01}/input.txt | 0 .../day_01}/input_test.txt | 0 .../day_02}/input.txt | 0 .../day_02}/input_test.txt | 0 .../day_03}/input.txt | 0 .../day_03}/input_test.txt | 0 .../day_04}/input.txt | 0 .../day_04}/input_test.txt | 0 .../day_06}/input.txt | 0 .../day_06}/input_test.txt | 0 src/main.rs | 23 +++++++++++++++---- src/utils/mod.rs | 3 ++- src/utils/solution.rs | 4 ++++ src/year_2023/day_01_trebuchet/mod.rs | 20 +++++++++++++++- src/year_2023/day_01_trebuchet/part_one.rs | 8 ++++--- src/year_2023/day_01_trebuchet/part_two.rs | 8 ++++--- src/year_2023/day_02_cube_conundrum/mod.rs | 20 +++++++++++++++- .../day_02_cube_conundrum/part_one.rs | 8 ++++--- .../day_02_cube_conundrum/part_two.rs | 11 +++++---- src/year_2023/day_03_gear_ratios/mod.rs | 19 +++++++++++++++ src/year_2023/day_03_gear_ratios/part_one.rs | 8 ++++--- src/year_2023/day_04_scratchcards/mod.rs | 19 +++++++++++++++ src/year_2023/day_04_scratchcards/part_one.rs | 8 ++++--- src/year_2023/day_06_wait_for_it/mod.rs | 21 ++++++++++++++++- src/year_2023/day_06_wait_for_it/part_one.rs | 8 ++++--- src/year_2023/day_06_wait_for_it/part_two.rs | 8 ++++--- src/year_2023/mod.rs | 17 ++++++++++++++ 27 files changed, 180 insertions(+), 33 deletions(-) rename input/{year_2023/day_01_trebuchet => 2023/day_01}/input.txt (100%) rename input/{year_2023/day_01_trebuchet => 2023/day_01}/input_test.txt (100%) rename input/{year_2023/day_02_cube_conundrum => 2023/day_02}/input.txt (100%) rename input/{year_2023/day_02_cube_conundrum => 2023/day_02}/input_test.txt (100%) rename input/{year_2023/day_03_gear_ratios => 2023/day_03}/input.txt (100%) rename input/{year_2023/day_03_gear_ratios => 2023/day_03}/input_test.txt (100%) rename input/{year_2023/day_04_scratchcards => 2023/day_04}/input.txt (100%) rename input/{year_2023/day_04_scratchcards => 2023/day_04}/input_test.txt (100%) rename input/{year_2023/day_06_wait_for_it => 2023/day_06}/input.txt (100%) rename input/{year_2023/day_06_wait_for_it => 2023/day_06}/input_test.txt (100%) create mode 100644 src/utils/solution.rs diff --git a/input/year_2023/day_01_trebuchet/input.txt b/input/2023/day_01/input.txt similarity index 100% rename from input/year_2023/day_01_trebuchet/input.txt rename to input/2023/day_01/input.txt diff --git a/input/year_2023/day_01_trebuchet/input_test.txt b/input/2023/day_01/input_test.txt similarity index 100% rename from input/year_2023/day_01_trebuchet/input_test.txt rename to input/2023/day_01/input_test.txt diff --git a/input/year_2023/day_02_cube_conundrum/input.txt b/input/2023/day_02/input.txt similarity index 100% rename from input/year_2023/day_02_cube_conundrum/input.txt rename to input/2023/day_02/input.txt diff --git a/input/year_2023/day_02_cube_conundrum/input_test.txt b/input/2023/day_02/input_test.txt similarity index 100% rename from input/year_2023/day_02_cube_conundrum/input_test.txt rename to input/2023/day_02/input_test.txt diff --git a/input/year_2023/day_03_gear_ratios/input.txt b/input/2023/day_03/input.txt similarity index 100% rename from input/year_2023/day_03_gear_ratios/input.txt rename to input/2023/day_03/input.txt diff --git a/input/year_2023/day_03_gear_ratios/input_test.txt b/input/2023/day_03/input_test.txt similarity index 100% rename from input/year_2023/day_03_gear_ratios/input_test.txt rename to input/2023/day_03/input_test.txt diff --git a/input/year_2023/day_04_scratchcards/input.txt b/input/2023/day_04/input.txt similarity index 100% rename from input/year_2023/day_04_scratchcards/input.txt rename to input/2023/day_04/input.txt diff --git a/input/year_2023/day_04_scratchcards/input_test.txt b/input/2023/day_04/input_test.txt similarity index 100% rename from input/year_2023/day_04_scratchcards/input_test.txt rename to input/2023/day_04/input_test.txt diff --git a/input/year_2023/day_06_wait_for_it/input.txt b/input/2023/day_06/input.txt similarity index 100% rename from input/year_2023/day_06_wait_for_it/input.txt rename to input/2023/day_06/input.txt diff --git a/input/year_2023/day_06_wait_for_it/input_test.txt b/input/2023/day_06/input_test.txt similarity index 100% rename from input/year_2023/day_06_wait_for_it/input_test.txt rename to input/2023/day_06/input_test.txt diff --git a/src/main.rs b/src/main.rs index 5d26c58..26be4a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,21 @@ -pub mod year_2023; -pub mod utils; +mod utils; +use std::env; + +mod year_2023; fn main() { - println!("Hello, world!"); -} + // Parse command-line arguments + let args: Vec = env::args().collect(); + + if args.len() != 3 { + panic!("Usage: {} ", args[0]); + } + + let year = &args[1]; + let day = &args[2]; + + match year.as_str() { + "2023" => year_2023::run(day), + _ => panic!("Invalid year specified"), + }; +} \ No newline at end of file diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 44b92ab..b777308 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1 +1,2 @@ -pub mod input_reader; \ No newline at end of file +pub mod input_reader; +pub mod solution; \ No newline at end of file diff --git a/src/utils/solution.rs b/src/utils/solution.rs new file mode 100644 index 0000000..7bd1d8c --- /dev/null +++ b/src/utils/solution.rs @@ -0,0 +1,4 @@ +pub trait Solution { + fn part_one(&self) -> String; + fn part_two(&self) -> String; +} diff --git a/src/year_2023/day_01_trebuchet/mod.rs b/src/year_2023/day_01_trebuchet/mod.rs index 5109e4e..7575fc9 100644 --- a/src/year_2023/day_01_trebuchet/mod.rs +++ b/src/year_2023/day_01_trebuchet/mod.rs @@ -1,2 +1,20 @@ pub mod part_one; -pub mod part_two; \ No newline at end of file +pub mod part_two; + +use crate::utils::solution::Solution; + +pub struct Day1Solution; + +impl Solution for Day1Solution { + fn part_one(&self) -> String{ + // Implementation for part one of day 1 + // ... + part_one::part_one() + } + + fn part_two(&self) -> String { + // Implementation for part two of day 1 + // ... + part_two::part_two() + } +} \ No newline at end of file diff --git a/src/year_2023/day_01_trebuchet/part_one.rs b/src/year_2023/day_01_trebuchet/part_one.rs index c6eda37..d6a90c6 100644 --- a/src/year_2023/day_01_trebuchet/part_one.rs +++ b/src/year_2023/day_01_trebuchet/part_one.rs @@ -1,14 +1,16 @@ use crate::utils::input_reader; -pub fn part_one() { - if let Ok(lines) = input_reader::read_lines("./input/input.txt") { +pub fn part_one() -> String { + if let Ok(lines) = input_reader::read_lines("./input/2023/day_01/input.txt") { let mut result = 0; for line in lines { result += parse_line(&line.unwrap()); } - println!("{}", result); + result.to_string() + } else { + panic!("Failed to read lines from input file"); } } diff --git a/src/year_2023/day_01_trebuchet/part_two.rs b/src/year_2023/day_01_trebuchet/part_two.rs index 0f3ae59..8b60128 100644 --- a/src/year_2023/day_01_trebuchet/part_two.rs +++ b/src/year_2023/day_01_trebuchet/part_two.rs @@ -2,15 +2,17 @@ use std::collections::HashMap; use crate::utils::input_reader; -pub fn part_two() { - if let Ok(lines) = input_reader::read_lines("./input.txt") { +pub fn part_two() -> String { + if let Ok(lines) = input_reader::read_lines("./input/2023/day_01/input.txt") { let mut result = 0; for line in lines { result += parse_line(&line.unwrap()); } - println!("{}", result); + result.to_string() + } else { + panic!("Failed to read lines from input file"); } } diff --git a/src/year_2023/day_02_cube_conundrum/mod.rs b/src/year_2023/day_02_cube_conundrum/mod.rs index 5109e4e..47dedb7 100644 --- a/src/year_2023/day_02_cube_conundrum/mod.rs +++ b/src/year_2023/day_02_cube_conundrum/mod.rs @@ -1,2 +1,20 @@ pub mod part_one; -pub mod part_two; \ No newline at end of file +pub mod part_two; + +use crate::utils::solution::Solution; + +pub struct Day2Solution; + +impl Solution for Day2Solution { + fn part_one(&self) -> String{ + // Implementation for part one of day 1 + // ... + part_one::part_one() + } + + fn part_two(&self) -> String { + // Implementation for part two of day 1 + // ... + part_two::part_two() + } +} \ No newline at end of file diff --git a/src/year_2023/day_02_cube_conundrum/part_one.rs b/src/year_2023/day_02_cube_conundrum/part_one.rs index 45c725a..0ec7a14 100644 --- a/src/year_2023/day_02_cube_conundrum/part_one.rs +++ b/src/year_2023/day_02_cube_conundrum/part_one.rs @@ -1,14 +1,16 @@ use crate::utils::input_reader; -pub fn part_one() { - if let Ok(lines) = input_reader::read_lines("./input.txt") { +pub fn part_one() -> String{ + if let Ok(lines) = input_reader::read_lines("./input/2023/day_02/input.txt") { let mut result = 0; for line in lines { result += process_line(&line.unwrap()); } - println!("{}", result); + result.to_string() + } else { + panic!("Failed to read lines from input file"); } } diff --git a/src/year_2023/day_02_cube_conundrum/part_two.rs b/src/year_2023/day_02_cube_conundrum/part_two.rs index 7b42d58..64b3e51 100644 --- a/src/year_2023/day_02_cube_conundrum/part_two.rs +++ b/src/year_2023/day_02_cube_conundrum/part_two.rs @@ -8,15 +8,17 @@ struct Game { blue: u32, } -pub fn part_two() { - if let Ok(lines) = input_reader::read_lines("./input.txt") { +pub fn part_two() -> String { + if let Ok(lines) = input_reader::read_lines("./input/2023/day_02/input.txt") { let mut result = 0; for line in lines { result += process_line(&line.unwrap()); } - println!("{}", result); + result.to_string() + } else { + panic!("Failed to read lines from input file"); } } @@ -25,9 +27,10 @@ pub fn part_two() { fn process_line(s: &str) -> u32 { let v: Vec<&str> = s.split(':').collect(); - let game_id: u32 = v[0].split("Game ").collect::>()[1] + v[0].split("Game ").collect::>()[1] .parse::() .unwrap(); + let games: Vec<&str> = v[1].split(';').collect(); let mut min_game = Game { diff --git a/src/year_2023/day_03_gear_ratios/mod.rs b/src/year_2023/day_03_gear_ratios/mod.rs index 8783a16..4f5eb87 100644 --- a/src/year_2023/day_03_gear_ratios/mod.rs +++ b/src/year_2023/day_03_gear_ratios/mod.rs @@ -1 +1,20 @@ pub mod part_one; + + +use crate::utils::solution::Solution; + +pub struct Day3Solution; + +impl Solution for Day3Solution { + fn part_one(&self) -> String{ + // Implementation for part one of day 1 + // ... + part_one::part_one() + } + + fn part_two(&self) -> String { + // Implementation for part two of day 1 + // ... + String::from("Not implemented") + } +} \ No newline at end of file diff --git a/src/year_2023/day_03_gear_ratios/part_one.rs b/src/year_2023/day_03_gear_ratios/part_one.rs index 8d6e24c..e43544c 100644 --- a/src/year_2023/day_03_gear_ratios/part_one.rs +++ b/src/year_2023/day_03_gear_ratios/part_one.rs @@ -8,12 +8,14 @@ enum Engine { Empty, } -pub fn part_one() { - if let Ok(lines) = input_reader::read_lines("./input.txt") { +pub fn part_one() -> String { + if let Ok(lines) = input_reader::read_lines("./input/2023/day_03/input.txt") { let matrix = create_matrix(lines); let result = process_matrix(&matrix); - println!("{}", result); + result.to_string() + } else { + panic!("Failed to read lines from input file"); } } diff --git a/src/year_2023/day_04_scratchcards/mod.rs b/src/year_2023/day_04_scratchcards/mod.rs index 8783a16..a2378e4 100644 --- a/src/year_2023/day_04_scratchcards/mod.rs +++ b/src/year_2023/day_04_scratchcards/mod.rs @@ -1 +1,20 @@ pub mod part_one; + + +use crate::utils::solution::Solution; + +pub struct Day4Solution; + +impl Solution for Day4Solution { + fn part_one(&self) -> String{ + // Implementation for part one of day 1 + // ... + part_one::part_one() + } + + fn part_two(&self) -> String { + // Implementation for part two of day 1 + // ... + String::from("Not implemented") + } +} \ No newline at end of file diff --git a/src/year_2023/day_04_scratchcards/part_one.rs b/src/year_2023/day_04_scratchcards/part_one.rs index 35ee2cf..aab305f 100644 --- a/src/year_2023/day_04_scratchcards/part_one.rs +++ b/src/year_2023/day_04_scratchcards/part_one.rs @@ -2,15 +2,17 @@ use std::collections::HashSet; use crate::utils::input_reader; -pub fn part_one() { - if let Ok(lines) = input_reader::read_lines("./input.txt") { +pub fn part_one() -> String { + if let Ok(lines) = input_reader::read_lines("./input/2023/day_04/input.txt") { let mut result = 0; for line in lines { result += process_line(&line.unwrap()); } - println!("{}", result); + result.to_string() + } else { + panic!("Failed to read lines from input file"); } } diff --git a/src/year_2023/day_06_wait_for_it/mod.rs b/src/year_2023/day_06_wait_for_it/mod.rs index 5109e4e..d9ed005 100644 --- a/src/year_2023/day_06_wait_for_it/mod.rs +++ b/src/year_2023/day_06_wait_for_it/mod.rs @@ -1,2 +1,21 @@ pub mod part_one; -pub mod part_two; \ No newline at end of file +pub mod part_two; + + +use crate::utils::solution::Solution; + +pub struct Day6Solution; + +impl Solution for Day6Solution { + fn part_one(&self) -> String{ + // Implementation for part one of day 1 + // ... + part_one::part_one() + } + + fn part_two(&self) -> String { + // Implementation for part two of day 1 + // ... + part_two::part_two() + } +} \ No newline at end of file diff --git a/src/year_2023/day_06_wait_for_it/part_one.rs b/src/year_2023/day_06_wait_for_it/part_one.rs index 12c567a..392b441 100644 --- a/src/year_2023/day_06_wait_for_it/part_one.rs +++ b/src/year_2023/day_06_wait_for_it/part_one.rs @@ -1,7 +1,7 @@ use crate::utils::input_reader; -pub fn part_one() { - if let Ok(lines) = input_reader::read_lines("./input.txt") { +pub fn part_one() -> String { + if let Ok(lines) = input_reader::read_lines("./input/2023/day_06/input.txt") { let mut margin = 1; let mut race_results: Vec> = Vec::new(); @@ -14,7 +14,9 @@ pub fn part_one() { margin *= process_race_result(race_results[0][i], race_results[1][i]); } - println!("{:?}", margin); + margin.to_string() + } else { + panic!("Failed to read lines from input file"); } } diff --git a/src/year_2023/day_06_wait_for_it/part_two.rs b/src/year_2023/day_06_wait_for_it/part_two.rs index 7f19ccd..d4905f1 100644 --- a/src/year_2023/day_06_wait_for_it/part_two.rs +++ b/src/year_2023/day_06_wait_for_it/part_two.rs @@ -1,7 +1,7 @@ use crate::utils::input_reader; -pub fn part_two() { - if let Ok(lines) = input_reader::read_lines("./input.txt") { +pub fn part_two() -> String { + if let Ok(lines) = input_reader::read_lines("./input/2023/day_06/input.txt") { let mut race_result: Vec = Vec::new(); for line in lines { @@ -10,7 +10,9 @@ pub fn part_two() { let win_count = process_race_result(race_result[0], race_result[1]); - println!("{:?}", win_count); + win_count.to_string() + } else { + panic!("Failed to read lines from input file"); } } diff --git a/src/year_2023/mod.rs b/src/year_2023/mod.rs index 4f53189..9005afc 100644 --- a/src/year_2023/mod.rs +++ b/src/year_2023/mod.rs @@ -1,5 +1,22 @@ +use crate::utils::solution::Solution; + pub mod day_01_trebuchet; pub mod day_02_cube_conundrum; pub mod day_03_gear_ratios; pub mod day_04_scratchcards; pub mod day_06_wait_for_it; + +pub fn run(day: &str) { + let solution: Box = match day { + "1" => Box::new(day_01_trebuchet::Day1Solution), + "2" => Box::new(day_02_cube_conundrum::Day2Solution), + "3" => Box::new(day_03_gear_ratios::Day3Solution), + "4" => Box::new(day_04_scratchcards::Day4Solution), + "6" => Box::new(day_06_wait_for_it::Day6Solution), + _ => panic!("Invalid day specified"), + }; + + // Call part_one and part_two on the solution instance + println!("Part One Result: {}", solution.part_one()); + println!("Part Two Result: {}", solution.part_two()); +} \ No newline at end of file