소스 검색

day03 +fix typo in day02

wullewutz 2 년 전
부모
커밋
78048a7399
5개의 변경된 파일397개의 추가작업 그리고 1개의 파일을 삭제
  1. 1 1
      day_02/src/main.rs
  2. 54 0
      day_03/Cargo.lock
  3. 9 0
      day_03/Cargo.toml
  4. 140 0
      day_03/src/input.txt
  5. 193 0
      day_03/src/main.rs

+ 1 - 1
day_02/src/main.rs

@@ -63,7 +63,7 @@ fn main() {
 }
 
 #[cfg(test)]
-mod day1_examples {
+mod day2_examples {
     #[test]
     fn example_1st_part() {
         let example = "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green

+ 54 - 0
day_03/Cargo.lock

@@ -0,0 +1,54 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+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]]
+name = "day_03"
+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"

+ 9 - 0
day_03/Cargo.toml

@@ -0,0 +1,9 @@
+[package]
+name = "day_03"
+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 - 0
day_03/src/input.txt

@@ -0,0 +1,140 @@
+.......................153..988....502..842.........588.....441.468......481..........314...715.57............................163..992..512.
+............805............*......#.............%...............*........=......%......................#......*.............-....#....*.....
+........914.........#...617..201.........271.....671......52..898................847..........*230..215......393..%751....537...............
+..........#......361..........*...........*............-4.............165..609........922..133...........706..................*....552*127..
+490*..........................350...*...664........806................../..*...514.31...=........../25....%.................83..............
+....245...............805...........467.......449...+..............313*....115....*.......611.343............$...237..229...................
+........................*.....150.............*..............8.........511...................*........#.....837.*......*....*...............
+435.................688.8..............$......330.................474.......9.......736...........*...787........336...245...446.....916....
+...*.*....920........%................666..90.....786$..221.......*.....173.%.......*........@..25.98.....186#.......................*......
+.718.120..@.............$.931................*...........*....15...10....*.........649....&.542.....................-.......@.......106.....
+.............-.......794...................486.........209....*..2........252..........517...................250.519......274...............
+...636.....500........................878.......106.........487..*....819........353.........678.........../..-...............539...........
+.............................43......*...........$.......44.....881....*...662*...%................407#...612.......452@.286................
+..................................195..543.../.......75..............887.......43...396..................................=.............813..
+........$.735......590.998.............=...862............228......................*................+...%...........979.....500=......*.....
+......597..........=...+.........61..................878../....596......*........542../815....567.834..672.=378.....*...&.........#..981....
+...................................*618..998.........*...............716..........................................495.682..62..945..........
+.......915..............156.................$.......298........479........170............985............+90..../.............@.....&7.......
+...800*.............246*.........370..............................-.........*....639....*.....362.368.......818................687..........
+.........$.173............383=......*........389......................273....785.....514....=...*....*...........@..............*....446*833
+.433...227...*...-.-...............770.988.................738.523...*....................397....599..499...&..385.........*199.35..........
+...%........40.437..300../....740.........*...................*...........................................192......&....272............*....
+........#.................305.......318.608..609....*..............448......@...959*604....16*...../..............131............=..325.215.
+.980...85....*48......883.............@..............663.................128..................944.420...203.....................381.........
+..........887............*....434........725................958......367......178..........................@.779.........................986
+.......................591...*......964.*....713.140..........*..698....*......................772.......................914........223.....
+...........960.....*....................80.....#..%....456..824....*...138...939.........362*......488...572........663..&..........*.......
+.......+.....*...645.............%12.....................*.........692...............922.....902..@.................*........27..542........
+...792..242.702.........256..668..............613*479...329...................134#....*...................*545...375........................
+.......................&......*.............&..........................298.........696.................844.....=....................184.....
+......886=..................129....187.......850..*.....&................*...............358...130............500................@.....*....
+.............*54..112.............*..............791..215...787.........437..889..............*........584........../226......366...483.....
+.......47..........*....826.500..48......665..................*.................*.377....#...49.......$.....................................
+.196...*..........815......*..............*.....*...........571........297*...153.*....416...............+.....73...126.............111.....
+.......164............547#................69.503.743............108...............22......../70....36.....470................516....*.......
+....................................634................&.357......*...................275.........+................592......%....52.204.....
+....#943....843............544.......-.....96*139...640........694................@.........558......................*...........*.......104
+..............*....731.......*...382...................................../...%557..857......*....-................716..638./165...338.......
+......926....342../..........861............/......................#...768...................569..192...................*...................
+..272....................500.......*.........784......558..38.....786..........238.932%................/...827....841............76.........
+.....*25....=..780.74...........457....%..%.......806........*........&........*...........+..........53..*..........%.............*........
+..99.....697...$....+................601.757.600...........751........801......795..998....362...........431......&............797.860......
+....*...................50.579...............&...-.878.....................586.....*............587..216.......321...783....................
+...859.................@.....+.317/............34../...499.....904.991.......$....938......337@...*..#.................*.............130*312
+............707.266.....................156..9............*.......*..............................880.......787.........102..................
+.........*.....*................938*743...*.*..............195.........425..........869*.....448..........*.....73.689.....48..469..........
+......568.164....938........................964.......899......641.....*........930.............*...891....104........*...&.......*719......
+....................*..................#...........+..-...381......36.749...744..*......659...935......+..............694.....370...........
+........174........460............+..435........893........%................+.....500......$.............#......397.............*...549.677.
+....981.*.....&355.....960.......963..................543#.........................................971...483....%............428.....=......
+354..*................*.....752..................69........676..54.......531*......78....877..-950....%............346...........699........
+......265.....#......959.................846.......*...93............263.....974....=.......*............&........................*.........
+............857...........527............../.......29....@.846.......*.....................768..134&...571..........302...........512.......
+....$254.........397......*............./....................%....128.........*984.....693..................991...+...*.@....@...........228
+............335...*.....751......626.699............$118..............991..............$....................*...327.93..508.301......348....
+....387........@.266..............*.......................-...........*.......%...................260....756................................
+....*.....817..................726...391.....112..........273..997...197....667......./......932*....$........290...............711......560
+...488.....&....961.....170.........*..........*................*.........*........883..825......71..............&...215...........*.766....
+...............+.........#....$.....600.&.......674.42.................730.998.........*..................139.........*.........461....=....
+...#97...........817........86..........155..............759.....200.............827...163..........................499.....................
+.....................#.........................793*722...........................$..............&....725&......................442..........
+.............812....613.....892..........................464......430..%.....462.....%.......599.............555..204....219......*637......
+...544........./............&...............................*.......*.840.......*...994.797........52.........*....*..........739...........
+....=..................................713.304...161........186.................824.......-....=....*.......118.844..113......*.....872.....
+......................75.963......244.....*.....*.......466..........590....................208....363...............*.....872..............
+.............391.....*...............*.......843......#..........184..-.................................441......448.597....................
+979......416......114................306.%.........719.....792......$...........................#..........*..............603.989.914*......
+.........-..................975..670.....890...621...........*..........145..659...369=.@978....935........938...............*........107...
+................259..%632....=...................*......173...871........*...................4..........*..........&108.........*281........
+...............*................326.....732...528.../....#.............698.............316..*........188.888.................918............
+..512........507...............*...........*........440.........................938......./.254..313...............................422......
+....*............300.........430..354$.....884.701.......%....368............../....@480...........*..142.............305...145.61......106.
+.363..939.639..................................$...../....704.....835@...-776....................655.........645......*......*....*892......
+.........*..........*338................../........741........................113..895...............%..403.....*..40..992..2...............
+.................986...........927..%449.17............................182....*.....=......870......899........665.#.................949.189
+...841.........................*.......................................*...849...........=....*295.........420..............441.882.........
+......$..+.............124...993..........*...........*................365..........79.364................*.............414*.....-.....360..
+........159...........*...........457$....199......960.967...&...................................+......181.....+.......................*...
+.....................859...................................962.............912....#......330.....168...........904.....958*....213*583..49..
+........%.........*..........421..340*196..892.........409.................*....488.645...*..........#......................................
+.....802........116...........*...........................*...227........791.........$....203.....963..418................599+..........744.
+..............#.....-966.23.94...58..283...........476....722.......730...........................................@..700.......%788.........
+240%..........844..................*.-.........486............307............#49...288........&.329.............61..*.....................95
+.........#............148..446...........402/..........256.......................&..........573..*...................319.......763..........
+......936..............*...%.................................-....709...371...955..352............553.629....445..................*.........
+.121......445.347...455.......=....#....92..................367..*......*............@........881........*..*....267/.....-89......843.*782.
+......519.=.....*.........401.996.452...-.......-387.886.........982...149..910%..........81..-.........653..407......819.......@...........
+.....*.......950...448#....*...............&691......$......................................*....538..............681*.........560..........
+.....25....................60............*......................304*863...../.............103......*.........811..........923.......426.....
+.800..................../................974..............255..............958....738..............465.........*......181...+........./.....
+....*.....-......824....782........90...........+505.......*....903...................544.712.826...........800..........*..................
+...408..123...-.........................................557.......*....................@.....*....561.743............310..642...97*475......
+...............900.#494.....915.624............../..............508.....431.*500.........986...../......=....187........*...................
+...........................*......*....#741.@957.979.*................/..%................%.............................218.805......505*...
+...........................59.....595.................948...+........885.....*808...554.........70......827=.............................690
+...95........+......455.....................................776...........795......=..............&...$........349...............621*80.....
+...*....../.793..$...#.....................842..........898.........729...................698.......273...704...*.......451..%..............
+.......340......911......865.....876..........-...........*...%......*....676*498..424...-.......=.........*........241..#...967.......323..
+........................-...........*271..578...........734..8..692..435....................698.548.................@...................*...
+.....=......*..................131...........=..=..............*.............523.659...221...+........................468..263..395.408..757
+.....236....661...565*...........#...............228............287...448.......*......&......................=.........#.+........*........
+......................982.................$..........403*317.............*103.................454...#...311....588...................105....
+...72...............................246..991...../...........12................%.........271...=....668.................................*...
+.......348...259......$..../384.221...............873.......*.....560....&...686............*..................241............763........884
+........*...+..........848.................=........................*.....76......*....889..417.404...............&...538.663..=......61....
+.......446....948..805.....615.....303+.372..........893.......945...849.........861....*........*...187.980...&....../...........838.+.....
+...453...............*....*.....................357...../.........+....................506....129..@.......*..633.822............*..........
+......*188........495....732............635*.....$............219........65....475................811...231..........*..332%...212..........
+.................................*..........575.....-....591...*...420.........*...............................897.317.............165...762
+.............764..410.............438....*..........588....*.245..%......*.....468..........32..-714...456.........................*....*...
+..............*.......................516.552...366......416........166...552.................*.............../..126.........396.386....921.
+.......&.............67&........468................*.............................403.221.......433.....411..299..........457*...............
+.....539.......*287.............*............657/.457..............614........34*......*.....$......12*.........693.........................
+............908......@602..176..358..778...............72...-..............$..........161.579..................*................571*602.....
+...........................*............*799...........*.....118........788....689................$....../702..493.......117............%...
+.................337..496..504.......................519............#..........=....744........782...................625.*.......499.....576
+.......459..%..............................................28.....398...............*..............*19....#935.........$..845......#........
+.............707.....................992........289...481+...*..................3..234....822$.......................................#......
+..513.914*.........161......664.....*..........@..............922......$.........*......................330..............@...........798....
+..........378.................*..258...............219+.313.........836.....=226.566.....748....&...147*......872.......786..538.446........
+.....*827...........907....844.............684..........@..................................*..263............../....8..............=...*....
+835........$...88......*.........308.........*.....161.....621*327..-184....333.........@.473.....205..................................872..
+...*....340.............744........-.........961....../........................*449...740........*................867.427..251..............
+..988........@...................................................765..........................743......228...............*........942.......
+.............125..#...........406.........%.....837.................*.....609*.........%..........644...=..465*643....880..509...*.......453
+....544..........480.............=.........274.....*..148..583...821..........880.....492....535...@.......................*............*...
+.....$.......306................................287....#..*...........647.....................$........-......166.714....942..675.....657...
+................*....................673...780...........620..........*.........&..........=......997.465..........$.............*..........
+.456........920..855.913..............*.../.....325*316.......6.......208....486........715.....=...%.............................67........
+......*178..+.............686.59....871.................$......*.............................149..............238........634................
+....56.........385...648*...............*968..&.......732.....921.467........*.........479.........695*284.../..........*...................
+............56.....$.....675..746#.............384.................*......451.709................................%..939..265.........290*891
+................501.......................620.............900.......825.................258...387......21*40...273...*......................
+.......532..................................*....*....956....=....................711.........&.....................415.307....411......9...
+..............................314../......692.214.718.............762*461.....844*.....&.............973.675...80.................*143......
+...........365.......460.329.@......476....................................*..........926.....799...*.............-.........................
+..............*.297...*...*.........................753........700......473.765..............*....967........111.629.932....125.............
+.......+...261...&.......786.283....695.....486.....*......565.+................536...../....380....../124..%........*.........%............
+......339..............*..............*....*.......996.......-.....+..............*......752......................@...............-.........
+.......................716...........551.631........................279..555.....373...................691......114.215............515......

+ 193 - 0
day_03/src/main.rs

@@ -0,0 +1,193 @@
+use regex::Regex;
+
+fn first_part(input: &str) -> usize {
+    let mut sum = 0;
+    if !input.is_ascii() {
+        panic!()
+    }
+
+    let re_num = Regex::new(r"([0-9]+)").unwrap();
+
+    let lines = input.lines().collect::<Vec<_>>();
+    let y_max = lines.len();
+    let x_max = lines.first().unwrap().len();
+
+    for i in 0..y_max {
+        print!("{i}: {} ->", lines[i]);
+
+        for hit in re_num.captures_iter(lines[i]).map(|c| c.get(0)) {
+            let start = hit.unwrap().start();
+            let end = hit.unwrap().end();
+            let num = hit.unwrap().as_str().parse::<usize>().unwrap();
+
+            let from_y = if i == 0 { 0 } else { i - 1 };
+            let to_y = if i == y_max - 1 { i } else { i + 1 };
+
+            for y in from_y..=to_y {
+                let from_x = if start == 0 { 0 } else { start - 1 };
+                let to_x = if end == x_max - 1 { end } else { end + 1 };
+                for x in from_x..to_x {
+                    if y >= y_max || x >= x_max {
+                        continue;
+                    }
+                    match lines[y].chars().nth(x).unwrap() {
+                        '0'..='9' => (),
+                        '.' => (),
+                        symbol => {
+                            sum += num;
+                            print!("{symbol} at {y}:{x} => adding {num}, ")
+                        }
+                    }
+                }
+            }
+        }
+        println!();
+    }
+    sum
+}
+
+fn second_part(input: &str) -> usize {
+    let mut sum = 0;
+    if !input.is_ascii() {
+        panic!()
+    }
+
+    let lines = input.lines().collect::<Vec<_>>();
+    let y_max = lines.len();
+
+    for y in 0..y_max {
+        let stars: Vec<_> = lines[y].match_indices("*").collect();
+        for (x, _) in stars {
+            if let Some(adj) = adjacent_numbers(lines.clone(), y, x) {
+                if adj.len() == 2 {
+                    sum += adj.iter().product::<usize>();
+                }
+            }
+        }
+    }
+    sum
+}
+
+fn adjacent_numbers(lines: Vec<&str>, y: usize, x: usize) -> Option<Vec<usize>> {
+    let mut results: Vec<usize> = vec![];
+    let re_num = Regex::new(r"([0-9]+)").unwrap();
+    let x_max = lines.first().unwrap().len();
+    let y_max = lines.len();
+
+    let from_y = if y == 0 { 0 } else { y - 1 };
+    let to_y = if y == y_max - 1 { y } else { y + 1 };
+
+    for line in &lines[from_y..=to_y] {
+        for hit in re_num.captures_iter(line).map(|c| c.get(0)) {
+            let num_start = hit.unwrap().start();
+            let num_end = hit.unwrap().end();
+            let num_value = hit.unwrap().as_str().parse::<usize>().unwrap();
+            let from_x = if num_start == 0 { 0 } else { num_start - 1 };
+            let to_x = if num_end == x_max - 1 {
+                num_end - 1
+            } else {
+                num_end
+            };
+
+            if x >= from_x && x <= to_x {
+                results.push(num_value);
+            }
+        }
+    }
+    if results.len() == 0 {
+        None
+    } else {
+        Some(results)
+    }
+}
+
+//        for hit in re_num.captures_iter(lines[i]).map(|c| c.get(0)) {
+//            let start = hit.unwrap().start();
+//            let end = hit.unwrap().end();
+//            let num = hit.unwrap().as_str().parse::<usize>().unwrap();
+//
+//            let from_y = if i == 0 { 0 } else { i - 1 };
+//            let to_y = if i == y_max - 1 { i } else { i + 1 };
+//
+//            for y in from_y..=to_y {
+//                let from_x = if start == 0 { 0 } else { start - 1 };
+//                let to_x = if end == x_max - 1 { end } else { end + 1 };
+//                for x in from_x..to_x {
+//                    if y >= y_max || x >= x_max {
+//                        continue;
+//                    }
+//                    match lines[y].chars().nth(x).unwrap() {
+//                        '0'..='9' => (),
+//                        '.' => (),
+//                        symbol => {
+//                            sum += num;
+//                            print!("{symbol} at {y}:{x} => adding {num}, ")
+//                        }
+//                    }
+//                }
+//            }
+//        }
+//        println!();
+//    }
+//    sum
+//}
+
+fn main() {
+    let input = include_str!("input.txt");
+    println!("First part: {}", first_part(input));
+    println!("Second part: {}", second_part(input));
+}
+
+#[cfg(test)]
+mod day3_examples {
+    #[test]
+    fn example_1st_part() {
+        let example = "467..114..
+...*......
+..35..633.
+......#...
+617*......
+.....+.58.
+..592.....
+......755.
+...$.*....
+.664.598..";
+        assert_eq!(crate::first_part(example), 4361);
+    }
+
+    #[test]
+    fn example_2nd_part() {
+        let example = "467..114..
+...*......
+..35..633.
+......#...
+617*......
+.....+.58.
+..592.....
+......755.
+...$.*....
+.664.598..";
+        assert_eq!(crate::second_part(example), 467835);
+    }
+    #[test]
+    fn test_end_2nd_part() {
+        let example = "......2...
+......*...
+.......222";
+        assert_eq!(crate::second_part(example), 444);
+    }
+    #[test]
+    fn test_start_2nd_part() {
+        let example = "222.......
+...*......
+555......5";
+        assert_eq!(crate::second_part(example), 123210);
+    }
+    #[test]
+    fn multigears_2nd_part() {
+        let example = "2........2
+*........*
+5........5";
+        assert_eq!(crate::second_part(example), 20);
+    }
+}