feat: implement day03

This commit is contained in:
Lennard Brinkhaus 2023-12-03 12:32:13 +01:00
parent 3c9a857e33
commit 3dc05c1b9d
3 changed files with 590 additions and 1 deletions

445
src/day03.rs Normal file
View File

@ -0,0 +1,445 @@
pub fn execute_task01(content: &str) {
let sum_of_machine_parts = solve_01(content);
println!("Day03 - Task01 - Sum of Machine-Parts: {}", sum_of_machine_parts)
}
pub fn execute_task02(content: &str) {
let sum_of_machine_parts = solve_02(content);
println!("Day03 - Task02 - Sum of Machine-Gears: {}", sum_of_machine_parts)
}
pub fn solve_01(content: &str) -> i32 {
let map: Vec<Vec<char>> = content.lines().map(|line| line.chars().collect()).collect();
let (machine_parts, _) = parse_to_parts(map);
return machine_parts.iter().map(|number| number.parse::<i32>().unwrap()).sum();
}
pub fn solve_02(content: &str) -> i32 {
let map: Vec<Vec<char>> = content.lines().map(|line| line.chars().collect()).collect();
let mut data = 0;
for (row_index, row) in map.iter().enumerate() {
for (col_index, char) in row.iter().enumerate() {
if char == &'*' {
let numbers = get_numbers_around(&map, row_index, col_index);
if numbers.len() <= 1 {
continue
}
data += numbers
.iter()
.map(|data| data.parse::<i32>().unwrap()).product::<i32>();
}
}
}
return data;
}
fn get_numbers_around(map: &Vec<Vec<char>>, row: usize, col: usize) -> Vec<String> {
let mut nums = vec![];
let mut data = String::new();
if row > 0 {
let row_data = map.get(row - 1).unwrap();
let mut data = String::new();
if col > 0 {
if row_data.get(col - 1).unwrap().is_ascii_digit() {
data.push(row_data.get(col - 1).unwrap().clone());
let mut i = 2;
while (col as i32 - i as i32) >= 0 {
if let Some(char) = row_data.get(col - i) {
if char.is_ascii_digit() {
data = char.to_string() + data.as_str();
} else {
break;
}
} else {
break;
}
i += 1;
}
}
}
if row_data.get(col).unwrap().is_ascii_digit() {
data.push(row_data.get(col).unwrap().clone())
} else {
if data.len() > 0 {
nums.push(data);
data = String::new()
}
}
if row_data.len() - 1 > col {
if row_data.get(col + 1).unwrap().is_ascii_digit() {
data.push(row_data.get(col + 1).unwrap().clone());
let mut i = 2;
while (col + i) < row_data.len() {
if let Some(char) = row_data.get(col + i) {
if char.is_ascii_digit() {
data.push(char.clone())
} else {
break;
}
} else {
break;
}
i += 1;
}
}
}
if data.len() > 0 {
nums.push(data);
data = String::new()
}
}
let row_data = map.get(row).unwrap();
if col > 0 {
if row_data.get(col - 1).unwrap().is_ascii_digit() {
data.push(row_data.get(col - 1).unwrap().clone());
let mut i = 2;
while (col as i32 - i as i32) >= 0 {
if let Some(char) = row_data.get(col - i) {
if char.is_ascii_digit() {
data = char.to_string() + data.as_str();
} else {
break;
}
} else {
break;
}
i += 1;
}
}
}
if data.len() > 0 {
nums.push(data);
data = String::new()
}
if row_data.len() - 1 > col {
if row_data.get(col + 1).unwrap().is_ascii_digit() {
data.push(row_data.get(col + 1).unwrap().clone());
let mut i = 2;
while (col + i) < row_data.len() {
if let Some(char) = row_data.get(col + i) {
if char.is_ascii_digit() {
data.push(char.clone())
} else {
break;
}
} else {
break;
}
i += 1;
}
}
}
if data.len() > 0 {
nums.push(data);
data = String::new()
}
if map.len() - 1 > row {
let row_data = map.get(row + 1).unwrap();
let mut data = String::new();
if col > 0 {
if row_data.get(col - 1).unwrap().is_ascii_digit() {
data.push(row_data.get(col - 1).unwrap().clone());
let mut i = 2;
while (col as i32 - i as i32) >= 0 {
if let Some(char) = row_data.get(col - i) {
if char.is_ascii_digit() {
data = char.to_string() + data.as_str();
} else {
break;
}
} else {
break;
}
i += 1;
}
}
}
if row_data.get(col).unwrap().is_ascii_digit() {
data.push(row_data.get(col).unwrap().clone())
} else {
if data.len() > 0 {
nums.push(data);
data = String::new()
}
}
if row_data.len() - 1 > col {
if row_data.get(col + 1).unwrap().is_ascii_digit() {
data.push(row_data.get(col + 1).unwrap().clone());
let mut i = 2;
while (col + i) < row_data.len() {
if let Some(char) = row_data.get(col + i) {
if char.is_ascii_digit() {
data.push(char.clone())
} else {
break;
}
} else {
break;
}
i += 1;
}
}
}
if data.len() > 0 {
nums.push(data);
data = String::new()
}
}
return nums;
}
fn get_cords_around(row: usize, col: usize, max_row: usize, max_col: usize) -> Vec<(usize, usize)> {
let mut cords = vec![];
if row > 0 {
if col > 0 {
cords.push((row - 1, col - 1));
}
cords.push((row - 1, col));
if max_col - 1 > col {
cords.push((row - 1, col + 1));
}
}
if col > 0 {
cords.push((row, col - 1));
}
if max_col - 1 > col {
cords.push((row, col + 1));
}
if max_row - 1 > row {
if col > 0 {
cords.push((row + 1, col - 1));
}
cords.push((row + 1, col));
if max_col - 1 > col {
cords.push((row + 1, col + 1));
}
}
return cords;
}
fn parse_to_parts(map: Vec<Vec<char>>) -> (Vec<String>, Vec<String>) {
let mut machine_part = vec![];
let mut non_machine_part = vec![];
for (row_index, row) in map.iter().enumerate() {
let mut machine = String::new();
let mut is_machine = false;
for (col_index, char) in row.iter().enumerate() {
if !char.is_ascii_digit() {
if machine.len() == 0 {
continue
}
if is_machine {
machine_part.push(machine)
} else {
non_machine_part.push(machine)
}
machine = String::new();
is_machine = false;
continue
}
machine.push(char.clone());
if has_symbol_around(&map, row_index, col_index) {
is_machine = true
}
}
if machine.len() > 0 {
if is_machine {
machine_part.push(machine)
} else {
non_machine_part.push(machine)
}
}
}
return (machine_part, non_machine_part)
}
fn has_symbol_around(map: &Vec<Vec<char>>, row: usize, col: usize) -> bool {
if row > 0 {
let row_data = map.get(row - 1).unwrap();
if col > 0 {
let character = row_data.get(col - 1).unwrap();
if is_symbol(character) {
return true
}
}
let character = row_data.get(col).unwrap();
if is_symbol(character) {
return true
}
if row_data.len() - 1 > col {
let character = row_data.get(col + 1).unwrap();
if is_symbol(character) {
return true
}
}
}
let row_data = map.get(row).unwrap();
if col > 0 {
let character = row_data.get(col - 1).unwrap();
if is_symbol(character) {
return true
}
}
if row_data.len() - 1 > col {
let character = row_data.get(col + 1).unwrap();
if is_symbol(character) {
return true
}
}
if map.len() - 1 > row {
let row_data = map.get(row + 1).unwrap();
if col > 0 {
let character = row_data.get(col - 1).unwrap();
if is_symbol(character) {
return true
}
}
let character = row_data.get(col).unwrap();
if is_symbol(character) {
return true
}
if row_data.len() - 1 > col {
let character = row_data.get(col + 1).unwrap();
if is_symbol(character) {
return true
}
}
}
return false
}
pub fn is_symbol(character: &char) -> bool {
let result = match character {
'.' => false,
'0'..='9' => false,
_ => true
};
return result;
}
#[test]
fn test_solve_01() {
let test_input = r#"467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598.."#;
let solution = solve_01(test_input);
assert_eq!(4361, solution);
}
#[test]
fn test_solve_02() {
let test_input = r#"467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598.."#;
let solution = solve_02(test_input);
assert_eq!(467835, solution);
}
#[test]
fn test_solve_01_2() {
let test_input = r#".......
.12*34.
......."#;
let solution = solve_01(test_input);
assert_eq!(46, solution);
}
#[test]
fn test1() {
let data = vec![
vec!['.','.','.','#','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','#','.','#','.','1'],
vec!['.','1','.','.','2','.','#','3','.','.','4','.','.','5','.','6','.','.','7','#','.','8','.','.','9','.','2'],
vec!['.','.','.','.','.','.','.','.','.','#','.','.','.','#','.','.','#','.','.','.','.','.','.','.','.','.','3']
];
assert_eq!(false, has_symbol_around(&data, 1, 1));
assert_eq!(true, has_symbol_around(&data, 1, 4));
assert_eq!(true, has_symbol_around(&data, 1, 7));
assert_eq!(true, has_symbol_around(&data, 1, 10));
assert_eq!(true, has_symbol_around(&data, 1, 13));
assert_eq!(true, has_symbol_around(&data, 1, 15));
assert_eq!(true, has_symbol_around(&data, 1, 18));
assert_eq!(true, has_symbol_around(&data, 1, 21));
assert_eq!(true, has_symbol_around(&data, 1, 24));
assert_eq!(false, has_symbol_around(&data, 0, 26));
assert_eq!(false, has_symbol_around(&data, 1, 26));
assert_eq!(false, has_symbol_around(&data, 2, 26));
}

140
src/input/day03/input.txt Normal file
View File

@ -0,0 +1,140 @@
48.................501....33.....622..............763.........331.................161.683......................................980..........
...491.842.....948*..................338.....*......=...........-...309.......633*....*....................*990...706...452......*..+.......
...*...*....................426........*..408.........................*............659...............250.........&.......*.....767...403....
363.....961...959#.508*223......843.406..........690................%..479.....................398.../....*............458..................
......=.........................................*....@946........767.........907=.................@....158.850..+670..............10*790....
.......39.306...679.%113...............665....874....................597...................861.........................840...855............
.................*.........864.....154*....#..............1*......4..........341..................731*..........783.....#.......*204........
..919...........706.....*......891........840..%473.........379....*149........-..115..519............668..........*423.....................
...*.................398.190......$.............................#.......=.%530......$..*..............................................475...
.543...@.............................834..729.&.........146......789.189........511...17........455*308..662.............530................
.....142....437....138*755..&53.....*..........783........./.......................*......................................./....674..315....
..........-./.......................826...%.....................752........414......480.................276.952...................-...*.....
.......131.......*654..590.................819..........$..........-.347*..................294.....776...&...*........741.....830....230....
..586%.....968...........+..........319..............628......516............933..........&........+..........600.....*........*............
............*...................-..*.........................%........................726...876..........772..........99......145......785..
...........710...............352...842..941...802.....133............474.&235.................*.............*561..........#...........*.....
.......................................=...............*........=...................642.......774......*.........67..@921.374.......165.....
..250........................8..........................968...375........224...#.......*............497.833........-........................
...$.....................584..*978..107&....471...............................486....408......................................@241.-555.....
......662$........%......&.....................*..#186...550........=309............................76.......399............................
................281.........808....666.......207.........-...615.................59=....569.....197*...42...%...........572....+231...%.....
....758.$777................%.....*...............433................646.834.............@.............*.........543...................769..
....*................995...........628...............*..................*..............=..........341...583.112+...*.......74..=341.........
.....775.................................744...278.128.......................126...940.839....=.....*............385.499...*............302.
..........*177...574.............+.......+......*................561#...590.*......$.........231...586....................212...........#...
.......418....................954..433.......484...........526...........*..643......-......................&..778...............701........
.414..............493.....566.....*......=..................*....236...748..........543...593...*745.......57.........&.........*...........
....*................*.....*....46....659..%.&980..........66....................36...........87...................994.......218...443......
226.241.893%..........257..312............69........792............/...........+...................$257..................790........./...184
................................................854*.............909.139..*276.680..&.........432............580....248.................*...
....136*755...@.950......................173.............95..........+...............2.877.......*668......+..+.........918*.......62....571
............333.*....812...$.428.................*496..................903@...258&.............-........650.................281......+......
......500.......6....*...719.+......285.......955.....712*12..561.497..............667...&.843..777.................427................@....
.....&............506..................=..359................#....................*.....24.../.........544.436..........492=........272.....
.........377..........=............41-......+...........229........761...540....391..............*565.....*......449..........665.......#...
.887....*.....906..141.....357...$.................%......*.................#.............-..874......811...9.....*....$113......*.967...628
......406............../.......637.551....223....162.......251...........................702.*......%......*....&.905.........747...........
....*...........321.....801............=.....-.#...............390........845..12............399...126....283.566.....124*102......&........
..447..+....935...-..............41.68.87.92...105....178=....*..........*.......*..92..............................................21......
......505...................................*................614....948..237..512......108.58..162...454@...........395.......5.............
...........454..10.../............101........885.......................*..................*...*....................*........................
..........%....*....534......114.*................../.95......427.210..674.......371.903......81....................922.....231...346.......
...525........887...........#........162....211..166..@......*.....*.......281......*....................835.635............*.....*.....828.
.....*..............571...............+........-.............933.629./25..*...........977........*............*...........178.....749..*....
..993.........-................516/................394...56................477....545.*.......251.858......499....435.........&.........170.
.......967..73.....&.....35.........656....842.....*......*........................*...749............%..........#...........622............
......+...........366.86..*...........#.....=.....162...332.............-..........859............102..488.........................717......
347.........32........#....393.................*.................252...262....951.......823...................784...358.603.......*.....515.
......917..*.....$..........................642.394..195...*..................*....$......*.448.519................*......*....302..870.*...
...-..*...674.....780.547....287....................*....504...................803.688.831....*..*...........617......./..476........*...9..
.286..738................#.......-511..11.....581..268.....................439.............523..459...............69..373............887....
......................*...................555*.........45....638...964.......-...$..........................................88*.............
.........802...+805.77.648.....66.676........................&..../....26......708...+.-981...239%........953..................672..........
......./.....#.............11.+......*....=...........756.............*.....-......298..................=...*..751.......433.........727....
....553.......659....907...@......@..530.218.............*......259..821...716.980..........89..851...793.614.......943....*.....961*.......
........*339........$..........373...............................*...............$.....667..$......*..........149.....+..613................
..987..................*...................670...105.....347....383.605..................@....447...124..........*737...........562..15*445.
........-.....595*7..215.839.781.............*..*...........$.............810....*............*...........350....................*..........
.........929.............*......*...362@...890...975....#..........2....../...252.576.......78...........+........685.240......695..........
.................475......598.152........................841.&880..#......................&....693....=.............=.....28........&384....
............472..*.......................$..320.412*...................304...=.-.........395......=.113....601..............................
..324.738.........968...................755..*......101.......476..........90..342........................*.....638..182/...........704..139
.........*..610*......601...................356.../.......633*....971..........................806.....#...240...*...................../....
........683.....326.....*..@........749.........390.........................163@....800..483..*........792.....94...........................
...600................957..741...../....376............817..899.......194........50.....@....96.368..................@.......+600....-......
..........338%..........................*...............#....#.........*....304..+....*...........*...................236............284....
..423........................&763.............947.................371.187..*.......356.441......201.......................285...............
............75................................*....................*......696........................#...............................670....
...*..........*.........426.......635..-....449.758*.......314.......*..............................246.........*................58..*......
574.322........937.......*........#...116...........40.......*.@752..400........180....@......918.........195.49......983....559*.....82....
...........*.........527..842.............................817...................../.270...445..*............*...............................
........622.235..869*..........922*...............125*610..............*459...............&....455...........21.......964...................
...................................465..%542.........................47......272............+..........775.............*.....328.....@......
.................732*........51.&..........................853.222.........................672....148....+.........572..499.+....*....12....
....125..............581....*...909.787.......720*263......*....$..881%....623*891..515..........=..................*.........726.78........
.....%..........715......935.........*....650............874.......................*.........127....................923.....................
................$.............330*..444....$....653............%....345..915.....960.395...........823.706..734.341...............295...342.
.............................................99..-...........465...&......*.............*416......*..........@....*...@......630.....*......
.....@.........356.......................399.+......+.....................349..................542..............853.375.......=.......132...
....77...=719.@.......*...../662...........*...233...781..............................&687................%.................................
.....................791..................933.*..........931.....884*...........-...................../...112..%601..........857............
583.....*292...................................438...................424......603.416.........484...104.....................*.......706.....
.............................452.......549.............24...997....................*............*.........767...379..........192............
........413.................&.....#......*....*.......*.........523.........443.412.....546......859..138*........&..66.576.................
..................../...........348....816.106.391......................447.........../...*..895.......................*................595.
...........#......771.675..................................673............*........176...856..=...627*308..696...............658............
....489..51...174......%..676......$..........*........332.*.............373.350................-..........*.................*.....*........
181.*........*...............*822.136..888.642.202.......*..65....958........*........358........510.....*..504..747...993....339..764......
...........540........................................308...........@.895..942.506......*....791.......166......#...../..../...........668..
...................452............-....631..............................*........&.....253..../............897......*....602................
..748....249.24............21...847...&.....194...@...............@...865................................=....*.....550.....................
....*.......*.....658...................573*.......679.....760/..431.................481..........907....130.912...............240...998....
.605...............*.......25.......#..................................726..646....#..........607..*................698..........*...+......
...........301&.572........#.....101...................148........55...........*.122........./........953..........-.....602.321.929.....998
511.870...............@....................808.........*........*............257.......206............@........685..........*...............
.............903...398.....588......114.......*...990...677.....182.10$...........%.........%.................%........................2*...
................*...........*...490...........987....=......803.........98......485.493.868..945.805......-........89............=944....296
......640.......669.........630..*.......................-....&..582.......341.........*.........*.........939......*.......................
.........*34....................557.....130....265.....21........*...625..$...............*796..690................997...489....293.........
....917.......916............@.................*.................948..&...........148...67..................833........./..........-...943..
....$........*......849...4$..300..217........58..........................643.170...........................................................
.........511..853......*..............*...........355.............35-.....-....@..636.....=...595..131...496..........................=687..
............%........&..476......452...435.....%...*.....700..........391........*.....418.........*.........39*.&..........................
.16...=.............500.....................426...469.....*.............&.....#..522.............939..............395.660...................
...*.696.....*869........995.....413...................864.................991.......598......42.........................@..806..386........
337.......983.....610........104*.......100*......................-............$79....@...461*....*685...270................*...*........240
......................207...........311.....741..................337.........*.................381.......*........979.....712.374.879.......
.......@.....554.........*.....485.....................853.............*..276.230...........7...........740...755*..................-.......
....927.../.....#...#...46........*466...........=.................=.531...........593......*..%....................961.....@...............
..........65......294..........................30..339..856.....966...........904..........791..841..........438.......#...747..............
..419...................614................972.......*...............131.......=.......................716..........21......................
.....=.79..............................&....*......76.....243..............115.......757...364..870....&...890......+...840..........55.....
....................383....*48...571...124...749............*................-..&.....*......*...*........@.....................201./.......
......752.....470.......+..........*........................964.......452........776...413...9..791...257.......687.....84......*...........
.........*502.*......873..850..573..729....@.....197....................*...798.........................@......*........&.....65...200..357.
...............307.............*..........597.....*...473.....=.......332....*..311..861*399...................472..................*.../...
.820.....................65....754.............713......*.....739...........622...............768/..................................967.....
....*.......180..........*...*.........218...............260......................519...933..........271.....36....132.....147..............
.935..4......@..254...815..58.689.......*..........................159..............$...*..........*....&...$.......*......../.414*164......
.......-.388.......%....................284...........833...770...*...........705.....697.......466.239.......332....489....................
....#.....*...........@.......288............972.........-.*......723.....*....*................................*............=..........592.
...535..........*.267..193.......*..367.....*..............132............509..776.790..@....466..........715.758...*115.....608............
.............979..*..........8.353.....*...329...@544.913..........................*.....759.*......704..%............................712...
...................52..170...*......459................/...#.......161........813.510.........207......@..........581..................*....
............#...........*..978...........581.9*815..........450...............*.......................................604.......308...725...
............16.........269............../.............195........217*216......890......................534.....84......-......-.............
....339...........................459.......644..........*.........................636.....113...%154...*....................127.....#348...
349...*.............402...735......@................182.121...134........%255.276...%...&.../............460....#......79...................
....503....22.............#.....$............38.....+...........*....-11.......*......326...../....*853.........216......*.../180.....16....
..........*......92.............456..619......*..-...........=...103..........190..........353..982......25*........465.209.................
...485.350..........949...994.......*....379..3.22........972.........713.734.........262...........$.......846.......*.............333-....
....................*..../....*121.729..+............325.............*.......*..........*........374..............+..769.....811............
......-..............788...488....................................851....679......*......487.....................368.........*........*.....
....612..528...255.................364.....................130.................317..251...............................665.206..972.957.184..
..........*..../.......337..475*......&..391...347...795....*.........................*......722..666...............@.......................
........823.$.....+.../.........716......*........*....*.....247......329...........697......*.....*....%.....168....624.........592........
...98*.......916..915......245............277..&..353...719..........$.....................846.601.37...47......................*......519..
......91..............720..*........$985......976......................834...........461.........*...........................266....#...*...
68....................*....45..............&...........79*888.250*461.*.......%................574..........3*....408..380........383.192...
...........836......383...........557.....672..........................764.....944............................827...........................

View File

@ -1,9 +1,11 @@
mod day01;
mod utils;
mod day02;
mod day03;
const CONTENT01: &'static str = include_str!("input/day01/input.txt");
const CONTENT02: &'static str = include_str!("input/day02/input.txt");
const CONTENT03: &'static str = include_str!("input/day03/input.txt");
fn main() {
day01::execute_task01(CONTENT01);
@ -11,5 +13,7 @@ fn main() {
println!();
day02::execute_task01(CONTENT02);
day02::execute_task02(CONTENT02);
println!();
day03::execute_task01(CONTENT03);
day03::execute_task02(CONTENT03);
}