1
1
mirror of https://github.com/theoludwig/advent_of_code_2023.git synced 2024-07-17 07:20:11 +02:00

feat: add day 3 - part 1

This commit is contained in:
Théo LUDWIG 2023-12-03 23:38:44 +01:00
parent 0ef3abfb3f
commit 96a0eb6d4d
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
9 changed files with 355 additions and 0 deletions

4
Cargo.lock generated
View File

@ -9,3 +9,7 @@ version = "1.0.0"
[[package]]
name = "day_2"
version = "1.0.0"
[[package]]
name = "day_3"
version = "1.0.0"

View File

@ -2,5 +2,6 @@
members = [
"day_1",
"day_2",
"day_3",
]
resolver = "2"

1
day_3/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

6
day_3/Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "day_3"
version = "1.0.0"
edition = "2021"
[dependencies]

77
day_3/README.md Normal file
View File

@ -0,0 +1,77 @@
# - Day 3: Gear Ratios -
Source: <https://adventofcode.com/2023/day/3>
## Instructions - Part 1
You and the Elf eventually reach a [gondola lift](https://en.wikipedia.org/wiki/Gondola_lift) station; he says the gondola lift will take you up to the **water source**, but this is as far as he can bring you. You go inside.
It doesn't take long to find the gondolas, but there seems to be a problem: they're not moving.
"Aaah!"
You turn around to see a slightly-greasy Elf with a wrench and a look of surprise. "Sorry, I wasn't expecting anyone! The gondola lift isn't working right now; it'll still be a while before I can fix it." You offer to help.
The engineer explains that an engine part seems to be missing from the engine, but nobody can figure out which one. If you can **add up all the part numbers** in the engine schematic, it should be easy to work out which part is missing.
The engine schematic (your puzzle input) consists of a visual representation of the engine. There are lots of numbers and symbols you don't really understand, but apparently **any number adjacent to a symbol**, even diagonally, is a "part number" and should be included in your sum. (Periods (`.`) do not count as a symbol.)
Here is an example engine schematic:
```txt
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
```
In this schematic, two numbers are **not** part numbers because they are not adjacent to a symbol: `114` (top right) and `58` (middle right). Every other number is adjacent to a symbol and so **is** a part number; their sum is **`4361`**.
Of course, the actual engine schematic is much larger. **What is the sum of all of the part numbers in the engine schematic?**
To begin, [get your puzzle input](./input.txt).
Your puzzle answer was `553079`.
**The first half of this puzzle is complete! It provides one gold star: `*`.**
## Instructions - Part 2
The engineer finds the missing part and installs it in the engine! As the engine springs to life, you jump in the closest gondola, finally ready to ascend to the water source.
You don't seem to be going very fast, though. Maybe something is still wrong? Fortunately, the gondola has a phone labeled "help", so you pick it up and the engineer answers.
Before you can explain the situation, she suggests that you look out the window. There stands the engineer, holding a phone in one hand and waving with the other. You're going so slowly that you haven't even left the station. You exit the gondola.
The missing part wasn't the only issue - one of the gears in the engine is wrong. A **gear** is any `*` symbol that is adjacent to **exactly two part numbers**. Its **gear ratio** is the result of multiplying those two numbers together.
This time, you need to find the gear ratio of every gear and add them all up so that the engineer can figure out which gear needs to be replaced.
Consider the same engine schematic again:
```txt
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
```
In this schematic, there are **two** gears. The first is in the top left; it has part numbers `467` and `35`, so its gear ratio is `16345`. The second gear is in the lower right; its gear ratio is `451490`. (The `*` adjacent to `617` is **not** a gear because it is only adjacent to one part number.) Adding up all of the gear ratios produces **`467835`**.
**What is the sum of all of the gear ratios in your engine schematic?**
Your puzzle answer was `answer`.
**Both parts of this puzzle are complete! They provide two gold stars: `**`.**

140
day_3/input.txt Normal file
View File

@ -0,0 +1,140 @@
...........................775.651...............887....79...946...921...493.....942..942.....151....155....................................
......240...................*.....-......................$..*...................*.......%.....+....................956.549.*290.......834...
.485...+............437......906......%..608.805.725..72.....242....745..61......440................................*..*.........515...*....
..........917.......&....146........790.....*......*....*..........*.....*...............207*......................796..116......../...924..
722...323.-................./...410.............72..748.442............384.708...............849..%............................470..........
.....*..........................*.....271..691....-...............4*...........388.................448......&........*....848&......751.....
....370..$....639.748.......*...467....*.....*........921*909.....................*...32.....................165..452.30....................
........984....*.....*...782........711.....50.172...............61&..........415.803.*.........524.......................203.......106*643.
...............57..433.........390....................&../.............*122...........674.........*.........................................
379..................../.........*........908..477...305.876..*..............297.415*......+.......94........../.....@378.......226..56*....
...*..-......82.......167...936.17..958...=....*...............196......+......*.......-...380.992.............989.........29......*....301.
..124..317.....*....................*..........839.......................191.882......231.........+......976.................+......469.....
............643................*........778............541........655..............*............*........./............*.................515
.....&............498#...&....726.774..............111....*378..................723.221.......473............752....523.638.......789...*...
.....353................518..........-.........../....*...............*88.........................*..@280..=..-....................-..297...
...........279...........................*816.....286.975.&319.....614......694.......799.......503.......861...569.............+...........
..474........*.228..............308......................................................*............207........................838..408...
...........226./....*................82.........................268/......814.........640.............../..489........719..=..........*.....
....83............88.544....250.......=...87..............199.........115*....................................*...377.@.....643........658..
........-...651.............*................&..............................*379.....411...686.397............202.*....../........217.......
.....781.......*897......630................330.....117.961..........61..543.....#...*.....*.......323.............996.603.......*..........
.......................................................*........473....*.....739.184.511..........+......43.833.................953...@.....
....110......@471...........*464....@.347......779................/.....205.*.......................681.....@.........................694...
.........................203......158.-....310./...449...726*982.............437.....+....259...................185......940*702............
.....465....*.......747....................*..........*...............................238..&......./..963...755....*........................
.....*....687..104..%.........645........959..497..732........988........733.....12...........417.176....$..........176.323....657..........
...566........*...........938*......418......*............925....*28..............-.156........*................................*..#........
.............933.......................*....988........../............................*........813.................868*674...375...839......
.....................804.......464..452...................................47......324.789..........692*288..................................
...959...........792....*10.....=......................*455.77...827.99....*.........................................166.......8...586......
.....*...607......*..............................12.764......*.......*..905....590.236...#.677...823......972..204............*......*......
..661....*........776....938..901.............@.*.........$...216...995...........*....382..*....#....546*.....#...........564........707...
......755................*.......*698......315..291.......964................-.......=.....865........................521........150........
........................323............./............973.........*35..........318.....204...........546..+667....214%..*..............973...
........143......&725...........619....149..............*235..650.........604..........................*..............195..............*....
................................*.................................926...................@240......+...433..302..672.........977....149..335.
.......880.......&...............301.512.$.....876......331.897...#..............#...............464......+...........938.........*.........
......@....942...603....&....879..........117..........*.....*.............562..905....377*862..............*56.........%........352..*165..
.726......*..........239....*.....915..............@...661...905..............*...................466....223.................730............
....*......97..839.......308.........*....115....512..........................497.392.....563........=...............448...........462*884..
....122.........*...471*...........52...&..=...................=..................@.........*...............................531.............
.........*..522...&.....274...........116......*572...........907..........................161..670@..@....................=........57*.....
.........28.*.....606.......................925...............................908...86...............78..............712.....813*......16...
....549.....512....................................825.....797.......224..660*........@.425*656.........=536..256.......*........599........
...*.............963..................842..704.............+....425..*..............................102.......$....565.179..774/........576.
...................*..........................*..............=...*....922........50...262....%............603......*........................
.......636.......349...&...224...$............899.........983..319.............#........&...663....*575..%.......95.....383*................
......=...............928........218................84.............964.........953..+...........212.........................857.............
............................179.......68......................&....*.................787.............92&.72....455..969...........846..40...
.993=..../..........110.407*...........%.............164..#....306.215...................552..............*....=...#.....69.602...#...*.....
......322..........#...........561..............968..*....913............%..........@586...=.330...........166.............*........863.....
............810...........157.*....557.....30.*.....774...................968.601*...........*....491..........557.....83*..................
.......*368.*.......842...*...435.-...........65.-................................680.....913....#.......793....*.........87.262....34.-....
....896......875.....*...........................581.957..758............$................................@..981..............#......*.844..
..................957...150.......503................@....*......89....59......261..........653....................................372......
...731...-129..+........*........-......................718.713....*.......197..*......30......+...476............872.......................
....*...........671....917.......................800.&......=....197..763....*...96......#.........*......%......*..........739......290....
.....234..............................814*251....../.755.............../...375.....................309.914..816...877..=.......$......$.....
.........543.378.....74.%608.25...755..........237..............834..................431..................../..........585..................
....408...&...*..............*................*.............214*.....................*.........89.475*.......................@..939..824....
...*..........526..........713..&.....132...899..108.75..#..................521....428....97../.......603.....248.........878...&...*.......
....383...............873......1....................*....850....862....%......#.=................533.........*.......................863....
.............852.........*................578%....................*..492.........905.145.927........*........437.107.....@....29............
......604.......#./531.179..&........56.........921..233.......943......................*..........174...........*.......271.*...32.........
........*..619.............588......*............*...*..............918*837.....120.128......508...........536...............558...*638.....
523....794...-....................451..544....648..365.778.....@.............@.....*............$..%414...*............453#.................
..................&......124*...........*...............*...259........696...424...........*...............322..534................*519.....
........201*.30...474........567.526..363............%.963......287+......*.........939.964.....994.............*.....-........287.......657
........................709*......=.........854...527................50..591...859....%..........*....196.......188..395..157...*...........
.281*....878................354.......*221...*............308....309...*.........$..@...72#......577.........................*...734.....481
.....9....*.............658........235......767..12..548...=...#.......585..830....609........................603.130.....884...........*...
........324........@...*........................*....*........628.............................................*...$...........=......658....
..................139.206....$.....768*........986....311..........967...15...174....100-.....7$..............962....=.........561..........
......521.....154.............318......622...................-.............%...@.................856.......60.........24..481.......520.772.
.825..#.......*.............................180.............228.11.....173.......128......428.........405.+...68...........=..882......*....
....*.......657...........27....798*460...................@............-..............31.*.....840...........*................@.............
.965...931............974...*...........................798.................455*.........856......*...809...97..934.740$..570...............
........*..511.$427....../..652..-297............296........$........552..........884..................*........%........*........128.......
..........-..........207............................=....641.........+..........-..*....43..........329................990...........*......
.10...................=..11....&............................./.143........875..732....&.......&............................349.....265...858
...*......222....495%...*...618..322.......%.........116...833....*........*.........776....288....760.........-............-...............
....291......*..........453.........*...811...398..............515........365..445....................*.....961.........433......743...$....
.............33..................543.........@..........................=...../.....793*...........266..............668...........#.....804.
................701....................17*.................449*741.....961......814..........................*245......*...809*.............
.......206......*....668..728$............529..........*93........................-...833.713..............69.......292........157...224*...
..........*585...969............810..522............866..............254.....570........*....*.....588..................+113.......-.....752
................................/......../......257.............119...-..844...*..979..442....169.....=.259......+.............752..401.....
...726...967........&.234.................326.....*....590*636...*............130.../.........................933.....652*439...*...........
.....*......*12..332...-...........500#..........944..............747.......$.....................203....513.......................%864.....
....389.&....................*...............938.......893....970.........864........538.755&.258..*........#...............717.96......+...
.65.....180...&.............864.....169.........*................*...390............*.........*....904.........................*...354...478
....620......472.726.931...............*.......150../.......@....960....................*928.812...........................428......*.......
...*................*.................463..........323.....478..........964..........175.........324%........=827......66.......193..303....
313.......................@...705.214.....174&........................-.....271........................818...............*.........+........
........171+..830...328..158...*..%..................17....721.....568......../...........949.............*.............608.....90......570.
....327..........*....*.......44......................*..................193-.......67.................970......966.503................#....
669*..........386..306....+............105............443...........................*........490..130...........*....$..........$62.........
......454...............&..954...38....*..........229....................643......388.671....@.......*...8......506....66*148.........$.....
......*................259.........*.................*756.......596.........*........................638..+....................%....854.....
...700......895..............................757................*.........847..............630*612..................854..606....38..........
...............*.....93.....573......274........*...952..336...................562..................+...637............*.*..................
945........../.814..=..........*....*...........38..=............-....&........*..................118......*.........745..855.........367...
......676..688..................604..153...............671......513...690....891.%.....101.728..........767......28...................../...
.........=.......700..222.443............114.....................................458...../....#...................*.........=...............
...........660.%...&.....*.....98.232.......*............308.......650..267/..........$............................417.....806.....502......
.......276*....500............@.............63.......621*.....520...-........913.....225.........620.597...709*632.............663*.........
...................232...149............65.......................&........................228.....%.....-................467............800.
..........118........+....*....@.........-.458...737.....-247.........425................/..................574......424*.........421..*....
......158.-....*........564.599......128......*...#..........................419.............764*156.............................*......996.
............577.563.....................*...777.......................508..............*827............................*465.....306.........
.........................*..............848......996*......688....154...*.......%...780......51...410......357.148..856.....................
...648.............835.713....*...609.......................&........*..885....494..........*..........661....*.............................
...-.....477./693..*........938.....*./...........642..149......465.502......$............=.523.........#.......892................=824.....
....................296...........44...707..........+.....*...................462......476........@...............*..........388............
.....58...321..............127................................446...............................965....446........114.........*.............
.........*........951...3....*.................623.263.............=.-....122..........................=....519*...........692.......%313...
........943.......*......$....990.......795..../......*..135.....815.483....*..937*.............................634..............771........
............460....757.....@......511/..*............564..-..............340.......109.206.......%785..148...21....................*....&...
.47.........&...............102........265......555..................182......12*.........................*...%.................788..200....
.../............=..................#82..........$.......207.............*863.....751.........251.....641.702.......141..640.................
..........23..902.......174...............*.............=.....314...........................*..........*.....*667...........*...............
.........*...............*.......*766..842.35.......243..........*......@..................57..435....501.285.............964.....*.........
..165...100.@512.........292.......................%......79..@...726....341.511*.............*...................573..........602.74.......
...*..................................596#...&...............769........................314.397..&31................%..855..................
...6.....673....................898..........836.......827.......+.....-..............................133*766..........*..............290...
..........*.........799........*.........629.......634........%.188.103.......594......464.799*295.............895...930.........-....$.....
.....908..504.......*......603..800..936...*......*.........768..........973...*.......*...............904*16.*................358..........
.....@............825..%......&.........*...963.294.74...........%........*....102...967...-343.................#..................94.698...
.810...................968..............429...........*....950..313..482..397...................33.........@..297..198...114.......*...*....
.....-............................554..................................-.......245.$...............991..282.........-...*....161..894..78...
..705.....177.....*..872@.285........*.../813.........235....729..........*287..*...472.788.645........................144..................
.............*..984.......*........722............496*..........*990....79......715........*............456...................812..470......
............518......@.....950.........712..............981...........................238....579.533........373.466...615/......$.....*907..
..716..907.......761..471..............*......../...........823..+............978*......=.....*.....#......&....*...........................
....*...............*........990.331...46....871...........*.....447.723...@...............594...............801......................#757..
...312...*.........211..........*.................90.860...970.........*...375...........*.....-....*779.........&..........................
.......692..............602.............................*.............595.............841...166..327......-444....154.......................
..............$....316..=.............+978.843........42....135/.+332.........454.........................................707.../......295..
.........$....743..../.......-................+........................*990..................343......#.....*...............*....684........
......651.....................644....................887.812........187.................783........749...928.291...........131...........293

10
day_3/input_example_1.txt Normal file
View File

@ -0,0 +1,10 @@
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..

108
day_3/src/lib.rs Normal file
View File

@ -0,0 +1,108 @@
#[derive(Debug, PartialEq)]
pub struct NumberPosition {
pub index_start: usize,
pub index_end: usize,
pub value: usize,
}
pub fn get_numbers_positions_from_line(line: &str) -> Vec<NumberPosition> {
let mut result = vec![];
let mut number_string = String::from("");
let mut index_start = 0;
let mut index_end = 0;
for (index_character, character) in line.chars().enumerate() {
if character.is_ascii_digit() {
if number_string.is_empty() {
index_start = index_character;
}
index_end = index_character;
number_string += &character.to_string();
} else if !number_string.is_empty() {
let value: usize = number_string.parse().expect("Should parse as a `usize`");
result.push(NumberPosition {
index_start,
index_end,
value,
});
number_string = String::from("");
index_start = 0;
index_end = 0;
}
}
if !number_string.is_empty() {
let value: usize = number_string.parse().expect("Should parse as a `usize`");
result.push(NumberPosition {
index_start,
index_end,
value,
});
}
result
}
pub fn is_symbol(character: char) -> bool {
!character.is_ascii_digit() && character != '.'
}
pub fn part_1(input: &str) -> usize {
let lines = input.lines().collect::<Vec<&str>>();
lines
.iter()
.enumerate()
.map(|(index_line, &line)| {
get_numbers_positions_from_line(line)
.iter()
.filter(|&number_position| {
let index_start = number_position.index_start.saturating_sub(1);
let index_end = if number_position.index_end + 1 >= line.len() {
line.len().saturating_sub(1)
} else {
number_position.index_end + 1
};
let has_symbol_on_the_left =
line.chars().nth(index_start).is_some_and(is_symbol);
let has_symbol_on_the_right =
line.chars().nth(index_end).is_some_and(is_symbol);
let has_symbol_on_the_top = lines
.get(index_line.saturating_sub(1))
.is_some_and(|&line| {
line.get(index_start..=index_end)
.is_some_and(|value| value.chars().any(is_symbol))
});
let has_symbol_on_the_bottom = lines.get(index_line + 1).is_some_and(|&line| {
line.get(index_start..=index_end)
.is_some_and(|value| value.chars().any(is_symbol))
});
has_symbol_on_the_left
|| has_symbol_on_the_right
|| has_symbol_on_the_top
|| has_symbol_on_the_bottom
})
.map(|matching_number| matching_number.value)
.sum::<usize>()
})
.sum()
}
// pub fn part_2(_input: &str) -> usize {
// 42
// }
#[cfg(test)]
mod day_3_tests {
use super::*;
#[test]
fn test_part_1_example() {
assert_eq!(part_1(include_str!("../input_example_1.txt")), 4361);
}
// #[test]
// fn test_part_2_example() {
// assert_eq!(part_2(include_str!("../input_example_1.txt")), 467835);
// }
}

8
day_3/src/main.rs Normal file
View File

@ -0,0 +1,8 @@
use day_3::part_1;
fn main() {
let input = include_str!("../input.txt");
println!("- Day 3: Gear Ratios -");
println!("Answer Part 1: {}", part_1(input));
// println!("Answer Part 2: {}", part_2(input));
}