This commit is contained in:
Alie 2023-12-03 14:52:49 +01:00
parent 92b606a9ff
commit a590bd2037
6 changed files with 333 additions and 8 deletions

51
Cargo.lock generated
View File

@ -2,6 +2,15 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "aho-corasick"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "day01" name = "day01"
version = "0.1.0" version = "0.1.0"
@ -10,6 +19,48 @@ version = "0.1.0"
name = "day02" name = "day02"
version = "0.1.0" version = "0.1.0"
[[package]]
name = "day03"
version = "0.1.0"
dependencies = [
"regex",
]
[[package]]
name = "memchr"
version = "2.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
[[package]]
name = "regex"
version = "1.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]] [[package]]
name = "template" name = "template"
version = "0.1.0" version = "0.1.0"

View File

@ -4,7 +4,7 @@ members = [
"template", "template",
"day01", "day01",
"day02", "day02",
# "day03", "day03",
# "day04", # "day04",
# "day05", # "day05",
# "day06", # "day06",

9
day03/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "day03"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
regex = "1.10.2"

140
day03/input.txt Normal file
View File

@ -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

125
day03/src/main.rs Normal file
View File

@ -0,0 +1,125 @@
use regex::Regex;
use std::{
collections::{HashMap, HashSet},
fs,
};
fn main() {
const FILE_PATH: &str = "input.txt";
println!(
"Welcome to AOC2023 day 3, first we will read the file {}",
FILE_PATH
);
let contents = fs::read_to_string(FILE_PATH).expect("Should have been able to read the file");
println!("And we parse the file!");
let data = parse(&contents);
println!("First thing: {}", calculate_1(&data));
println!("Second thing: {}", calculate_2(&data));
}
#[derive(Debug)]
struct Part {
symbol: char,
value: Vec<i32>,
}
fn parse(input: &str) -> Vec<Part> {
let renum = Regex::new(r"(?<number>\d+)").expect("Invalid regex");
let resym = Regex::new(r"(?<symbol>[^\d\.]+)").expect("Invalid regex");
let mut number_map: HashMap<(usize, usize), (i32, Vec<_>)> = HashMap::new();
let mut symbol_map: HashMap<(usize, usize), &str> = HashMap::new();
for (line_number, line) in input.lines().enumerate() {
for cap in renum.captures_iter(line) {
let capture = cap.name("number").unwrap();
for col in capture.range() {
number_map.insert(
(line_number, col),
(
capture.as_str().parse().expect("NAN"),
capture.range().collect(),
),
);
}
}
for cap in resym.captures_iter(line) {
let capture = cap.name("symbol").unwrap();
for col in capture.range() {
symbol_map.insert((line_number, col), capture.as_str());
}
}
}
let mut list_parts: Vec<Part> = Vec::new();
for ((x, y), symbol) in &symbol_map {
let mut list_nums: Vec<i32> = Vec::new();
for i in -1..=1 {
let i = (*x as isize + i) as usize;
for j in -1..=1 {
let j = (*y as isize + j) as usize;
if let Some(number) = number_map.get(&(i, j)) {
list_nums.push(number.0);
let numbers = &number.1.clone();
for num in numbers {
number_map.remove(&(i, *num));
}
}
}
}
// Add the part with all the adjacent numbers at the end so we can do part 2
list_parts.push(Part {
symbol: symbol.chars().next().expect("char must exist"),
value: list_nums,
})
}
// So sad it was not usefull in the end
let number_set: HashSet<_> = number_map.values().collect();
for number in number_set {
list_parts.push(Part {
symbol: '.',
value: vec![number.0],
});
}
// Return
list_parts
}
fn calculate_1(parts: &Vec<Part>) -> i32 {
parts
.iter()
.filter(|part| part.symbol != '.')
.map(|part| part.value.iter().sum::<i32>())
.sum()
}
fn calculate_2(parts: &Vec<Part>) -> i32 {
parts
.iter()
.filter(|part| part.symbol == '*' && part.value.len() == 2)
.map(|part| part.value[0] * part.value[1])
.sum()
}
#[cfg(test)]
mod test {
const INPUT: &str = r#"467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598.."#;
use super::*;
#[test]
fn test1() {
let tst = parse(INPUT);
assert_eq!(calculate_1(&tst), 4361);
}
#[test]
fn test2() {
let tst = parse(INPUT);
assert_eq!(calculate_2(&tst), 467835);
}
}

View File

@ -7,9 +7,9 @@ fn main() {
.expect("Should have been able to read the file"); .expect("Should have been able to read the file");
println!("And we parse the file!"); println!("And we parse the file!");
let mut data = parse(&contents); //let mut data = parse(&contents);
println!("First thing: {}", calculate_1(&data)); //println!("First thing: {}", calculate_1(&data));
println!("Second thing: {}", calculate_2(&data)); //println!("Second thing: {}", calculate_2(&data));
} }
@ -19,13 +19,13 @@ mod test {
const INPUT: &str = r#""#; const INPUT: &str = r#""#;
use super::*; use super::*;
#[test] #[test]
fn test1() { /* fn test1() {
let tst = parse(INPUT); let tst = parse(INPUT);
assert_eq!(calculate_1(&tst), 24000); assert_eq!(calculate_1(&tst), 24000);
} } */
#[test] /* #[test]
fn test2() { fn test2() {
let tst = parse(INPUT); let tst = parse(INPUT);
assert_eq!(calculate_2(&tst), 24000); assert_eq!(calculate_2(&tst), 24000);
} } */
} }