diff options
| author | nekineki <nekineki@nekineki.net> | 2023-12-03 09:41:53 +0100 |
|---|---|---|
| committer | nekineki <nekineki@nekineki.net> | 2023-12-03 09:41:53 +0100 |
| commit | e687c9347ea3e018d41f9c45e8bb52791b2267db (patch) | |
| tree | d3c4c1bd62d0d0f722830fbbd65b3f098754ef76 /2023 | |
| parent | 2398d0a0170db048dccb3397f9e89d95003bb37f (diff) | |
day03
Diffstat (limited to '2023')
| -rw-r--r-- | 2023/day03.rs | 168 | ||||
| -rw-r--r-- | 2023/in/day03.pzl | 140 | ||||
| -rw-r--r-- | 2023/in/day03.ref | 10 |
3 files changed, 318 insertions, 0 deletions
diff --git a/2023/day03.rs b/2023/day03.rs new file mode 100644 index 0000000..be02594 --- /dev/null +++ b/2023/day03.rs @@ -0,0 +1,168 @@ +#![allow(dead_code)] +#![allow(unused_variables)] +#![allow(unused_mut)] +use std::collections::HashMap; +use std::fs::File; +use std::io::Read; +// use std::cmp::max; +// use std::cmp::min; + +fn is_symbol(c: char) -> bool { + if c.is_digit(10) || c == '.' { + false + } else { + true + } +} + +fn is_valid(x: usize, y: usize, m: &Vec<Vec<char>>) -> bool { + for dy in -1i32..2 { + for dx in -1i32..2 { + let mut xx: usize; + let mut yy: usize; + if x == 0 && dx == -1 { + xx = x; + } else if x == m[0].len() - 1 && dx == 1 { + xx = x; + } else { + xx = ((x as i32) + dx) as usize; + } + + if y == 0 && dy == -1 { + yy = y; + } else if y == m.len() - 1 && dy == 1 { + yy = y; + } else { + yy = ((y as i32) + dy) as usize; + } + // println!("{} {} {} {} {} {}", x, y, dx, dy, xx, yy); + // print!("{}", m[y][x]); + + if is_symbol(m[yy][xx]) { + return true; + } + } + } + return false; +} + +fn main() { + // let filename = "in/day03.ref"; + let filename = "in/day03.pzl"; + + let mut f = File::open(filename).expect("cannot open file"); + let mut content = String::new(); + f.read_to_string(&mut content).expect("cannot read file"); + let lines: Vec<_> = content.trim_end().split('\n').collect(); + // println!("{:?}", lines); + + let mut res1 = 0; + let mut res2 = 0; + + let mut m: Vec<Vec<char>> = Vec::new(); + for line in &lines { + let mut l: Vec<char> = Vec::new(); + for c in line.bytes() { + l.push(c as char) + } + m.push(l) + } + + let mut hm: HashMap<(usize, usize), (u32, u32)> = HashMap::new(); + let mut id: u32 = 0; + for y in 0..m.len() { + let mut have_num = false; + let mut num_valid = false; + let mut start_x = 0; + for x in 0..m[0].len() { + if have_num { + if m[y][x].is_digit(10) == true && x == (m[0].len() - 1) { + if num_valid { + let num: u32 = String::from_iter(m[y][start_x..=x].iter()).parse().unwrap(); + for xx in start_x..=x { + hm.insert((xx, y), (id, num)); + } + id += 1; + res1 += num; + } + have_num = false; + num_valid = false; + } else if m[y][x].is_digit(10) == false { + if num_valid { + let num: u32 = String::from_iter(m[y][start_x..x].iter()).parse().unwrap(); + for xx in start_x..x { + hm.insert((xx, y), (id, num)); + } + id += 1; + res1 += num; + } + have_num = false; + num_valid = false; + } + } else if m[y][x].is_digit(10) { + have_num = true; + start_x = x; + } + + if have_num { + num_valid = num_valid || is_valid(x, y, &m); + } + } + } + + println!("{:?}", hm); + + for y in 0..m.len() { + for x in 0..m[0].len() { + if m[y][x] == '*' { + // println!("{} {}", y+1, x+1); + let mut neighbour: HashMap<u32, u32> = HashMap::new(); + for dy in -1i32..2 { + for dx in -1i32..2 { + let mut xx: usize; + let mut yy: usize; + if x == 0 && dx == -1 { + xx = x; + } else if x == m[0].len() - 1 && dx == 1 { + xx = x; + } else { + xx = ((x as i32) + dx) as usize; + } + + if y == 0 && dy == -1 { + yy = y; + } else if y == m.len() - 1 && dy == 1 { + yy = y; + } else { + yy = ((y as i32) + dy) as usize; + } + if let Some((id, val)) = hm.get(&(xx, yy)) { + neighbour.insert(*id, *val); + } + } + } + + if neighbour.len() == 2 { + let mut mul: u32 = 1; + for (key, val) in neighbour.iter() { + mul *= val; + } + // res2 += neighbour.iter().map(|(_, val)| val).reduce(|a,b| a*b).unwrap(); + println!("{}", mul); + res2 += mul; + } + // let a: u32 = neighbour.iter() + // .inspect(|x| println!("asdf {:?}", x)) + // .map(|(_, val)| val) + // .reduce(|a,b| a*b); + // println!("{:?}", a); + + // println!("{:?}", neighbour); + // asdf + } + } + } + + println!("res1: {}", res1); + println!("res2: {}", res2); +} diff --git a/2023/in/day03.pzl b/2023/in/day03.pzl new file mode 100644 index 0000000..0182e0a --- /dev/null +++ b/2023/in/day03.pzl @@ -0,0 +1,140 @@ +311...672...34...391.....591......828.......................738....................223....803..472..................................714.840. +.......*...........*.....*...........*........631%...703.......*..12....652.................*.$............368.769*148.................*.... +....411...........2....837.121........511.745...........*.48.422.@.........@.............311........887......*................457........595 +........*328...............&..........................144.*...................138............48.......*......682.........@...*.......777.... +.....144.....+........170...................207............813..../.&....139..*.....346........*..147..143.+.....78....536..79........*..... +...........828...559.................181...%..........613.......10...928...*...993.+.........758.*.........471...#../...............573..... +....................*164...132..........*........=.......*.................47.........186.........313..............411...................... +...............342............+..823.....533....519.....899...310................@........325...........15....................407.....#..... +515......916......*.@...........*.............................*......961.........827......*.......=.........567....=238...874*.......420.... +...=.......*...207...882......719....455.973...................369...*.....*913........978...%..720..........-.............................. +........306................................................182.....534......................229........744.........+.....=..........918..... +303...........745...........361..............223..243.129.....=.................830.....%.................%......493....106............&.... +.................*209..17.........494.910................*90....496.....709......&....896................................................... +....%..562............*.....@...........*..528.......321...........*673......................887..%231...............700..............116... +.988..=....944...596........806...24..519./..........*......../...............146...........*...................554..*...................... +..........$.......................................822...621..771..151.504.......*.628*343..34...+.&..............*..329.303..641..678..+.... +........................361.....347*524..538...........*.........*.....-......790.............933..724......699...........*....*........262. +.716..517...+..........*................*......955$..544.....238..593.....399.......#..=...................*...........241.....930.......... +...%.+.......744....550..........#131....964.............234..*.............*.....856..809..450....%....332.....419..................389.... +........264...............798......................816....%....228.......501..224........../.......645.............*499.....*.......*....... +.......*.......939........*.....@..............795*..............................*........................789................907.....647.... +.....612..........*.612..291..592........#.................567...................391.........................*....387............64......... +................24.....*.................814......=........$......./....346................444............833....*..........845...*......... +...........303.........605..326.....108.......%..56..842=.....*....387...*..478-..272=....*..........#........778.....299=..@...722......... +......484=..=.....753..........$......+.....449...............559.......144.............695.........675..................................253 +...-...............*.........*................................................................987.......200......*......445.......124...*... +....825............603....544.634........................432...=875...738...............731..*.............&..488.363..*.....663.........876 +........755.................................#............-............/.............*....*....470..314.145..............277...@.....234-.... +....323.........221..............892...284..473.....44......342.622.........707....413...984..........*....../.............................. +..............$....*..............*...&........................*.......351%...*.............................226..........155....973+........ +...............177.991..........268.............532=......./.....277........183......*935.........917....*........825.....-................. +.....+.%................#...413........304...............384....*.................748........./....*...955.......#...........885.970........ +...44...627..........368.../...........=.........................314..293...................102..108............................*........... +......=.........................=........733..433...128..............*.......889...........................661.........-358...$....281...... +.....35..........$...........834.../699...../.-......................461.......-........*...................*..................374....=.#412 +............763..201..$............................392........341........96......723.340.........327.736...1..$897.....471.................. +.....*512..*.........221.............125$....257.....@.....................*.277.*................*...*................................@.... +..301......694..............54.............../.........611...............596.../..100.......164.801....596..490....&361.........415....412.. +..................243...927*.........29*293........645*.......862*2...........................=............*.....................*..$....... +............$......&.........655.906...........500....................125.613..844.........@......435......984....725-..531.....711..79..... +...817...450.....................*........431...%........................*..........957.....739..*.....................*.................... +.....#........237......122.66.942............*.........548..................506.435...............73.883............644...101............... +.863.............*......&..*..................98..283........532......750..............*........#.......+....594*........................... +....*.........451.........623.....*....*...........&............%..50*.......44.....123......951.................62.943......365...995...... +.639..............827.265......132..354.127...........350....................*..........................11..........*....%...*.............. +........41..#175....@...*..*.....................+......*..247.353...#.....44.....912-......757...............144..481.693.271.........993.. +.........*.............993.707...857*...........375.....90...@...*..386.......403.............*..................*..............208....=.... +.......343.437....391..........&.....117....637.....345.........43.......*662..@...........908.........956.......908.............&.......... +389.............&...........670.............$...42.....*...=..........564................................%...............+.................. +...*............435....*.............913..............574..149.......................962....................953...77..641....514............ +....448.....783.....892..381.....593.*.........................403*149................%..863*954....373-..+........*...........*.+...663.... +906........................#......*...922.........950......................993...3.........................379....450.......877..804.#...... +...............768.......*......-.198.........160*............820......382...*......169.........*555..257................................... +260..............*.....68.....281.......871...................*...980......875.......*.......169........*.............531......@............ +....*......@.....587...................#......48..251..316@...848..................303................931..132..............790............. +.....205...572................696........&.....@.....@............709..611.............=.......270........*....165......783.......790....... +......................965.........709..826....................882*......%..775.........391....*......985.5........*179.....*47.......&...... +.....451............@................#..................804..................$..161...........837..............*................$904....176. +720.................70..........899.....868..............=.......357.252.......*..................707.......898.139.....704............*.... +....................................307*....510..................*.....*.......17..501.............*................@....*..........855..... +.............745............451................*..689..980..@670.594.716..........*.................565......595..622.....793............... +.....890......$...73...422...*...977&.......324..%.......@................29.......945....*.............298-..................#.........#642 +........*........*....%......................................$645......-...*...150.....434.259..609*560....../............266..524..../..... +.....203..........133.....596.619.................*...............676.107..811...*...........................115...........*.........178.... +............................=..*...............157.682.......262..*............196......$..........707................14..386............... +.........871............605....721...........................*...867.................685..866.......*.............602...............628..... +......#.....*....413.......@.......380....872................372.......277......277.........*.......166...........@..........656....*....748 +.....841..664.......*.422............................945...........%............*.........959.863.........187........299.......*.....887.... +.708..............311....+..296*497.....751...639....*..........269..........875....%.........................-.................36.......... +.........834..............................@...........708...........................87....298..*471.....$.....463........................... +............+...739..........701........................................779................=.........265..642............................... +......504..........*...............871*314....127..36..639.196..48..................96......../785.........*..854=.....607=...542........... +...73............254.591.........................*..$.....*......*.........35......*...@................150...................-........340.. +........224............/....*214...=..........949..............455.........*.......377.88....................823......582.............*..... +.......*.......643.......901........191..267.......293...................194..266.................987.......*........&....+.........545..... +....604...583.....*............*742........*.........*........$..................*..365............*....=...880.55......540.520#............ +............*..272.....843..201.....*....&.128..+....433.......563......281*77.540.+.......979&..134....514.......*..............46......... +...966.....67.......................19.934......224..........#....................................................693............*.....525.. +....*.........188.....904......*..............................210....*958.........811...............574....965................288..20.$..... +..213.........*...137*.......325.....628..................406......34..............*.......406...$.*.........*.........867..........*....... +...........810............................*.....926.........*..............372.....372.....*...266.716........985......*.............1...... +...............399*126.....*.......*...379..729.+........245.........893....-..866.....101.299..........8.544........784.................... +954......................63.501.529........-.......695/.................................*..........320....................211.55...533...... +............457..........................=.......................375#..........961*......720......*...............542........*.....+...327.. +........396...*.476.774.680.189@.438......669............936.......................195.......405.926.213.....254.....*.................*.... +...-.......-.......*.....&...........$........22....732.*.......$121.200...............-.......*........*.............522.825..77........... +....272..............#............925.....378*......&....53.............*....605.....329.116..501..=.765......537.680......*..*.........#... +817........109....209...........................690...................573...&............/........48...........*.....*..169...581.......537. +...=..747.............171.70.........990........*..............529.............482.....................383......531.53.............#211..... +......*.....470........*...*.812.664.=.......141.....82...888...*.................@.312....9*860...........529............./...952.......... +.....906...*....810...966....*.....*.....................*.......616........327-.....*...............................80.655................. +323......459......@.........185.....894..891..........254.................................938.........262........602*....................... +.............110.........................$......*55..........155..................248*600.....66........*...............*......75..880...... +.....459...........928...............................*900.....@.....27............................526....434.402.....840.816.......*........ +......*........241....*...70.465...668....110=....905...........252..*..............%..........74.............*.................817...290... +.540...224........*.42......*........*.................705.......*..128...&.758=.512...884.....*..648*.......259.......486.627.......*...... +....*..........467................462..321....296*211.*........283......488...........%......796......774.............%.......+.200.87...... +...663..............$961..579...........................*...................189..........71................420..57...............*.......... +...........617.645...........*........700..886...806.581.610.....132.379.......$......+....-..941$.....................504....589........528 +......377.......+.....290...221..768.....*....&....&................*.................556..............132.............*.................... +......................+.............*152.448............903.....339.....=.......709............991.......*.............865.................. +....-.....$717.............552................40..458....*.....*......365.......-......762.732...*.....690.266.....222............122....... +.414...........496...+.712..*...=....785.........*.......79...884.991................/....*.....844..-........@....*...782...........*627... +.......$........%..560..*...873..739..#........368.991......*.....*.....27..........951..............864.........206...*...878.............. +.....231..995...........865................602......*....379.609.935...+......643................952.....@...........884...*................ +.............$....726........360..........=.................................................*990.+........959............932................ +.....................+.508.....=...194.......*30........./...........487......171...243..709........495.........995.464.......997........... +......292..............$.............*....253.........*.554...........*......../......................*.........*....%......*....*582.446... +...........802.....237.....*32...286.259............132.....236......683.....&.......................705...$855.37.....=...476.........*.... +.....418=.............*.664........*..........*11............$.....=.....70.380....138.....416.609..................452........43...156..260 +....................20..........512........303.....-................491...*.......*.........*..&..........&962..........*......#........-... +426.646....833...........821....................348.......537...668........92.....295.....620...................147=.753.................... +......*.....*..................210.681...................$..........937.......350......................533............................+..... +.65...22..656...#........*44...*.......178*.........446.........655..............*592.....952-.....680..*...........=..305#............704.. +...*.............422..835......126.........552.......*....543..........-....444.......995.........=.....771........480.........782.......... +............28............983$...........5............953.&.......216..749....=..........-....................232........................... +.....................878................*.........426...............*.........................$........792%..*....898......264...39......... +...480.....674..149+...................740......#....*251..920.......552......................713...........639.......917.........*......... +...+........+...................$951..........*..968.......*............................................750.......479............770.959.... +...............424..54*489..+...........14..355...........782...50................900.770..........639.....&.942.....*39.....477........+... +....888.....................412................................*...%123...579...../....&.....315.....#..........*..............*..66........ +......*..........................793..402.......*807...854@...115........................=...*...204.......208..928..........121............ +...........880=.818......982......*......*...923.......................................809.962...*.........&................................ +...957*511.........*436..@......704.......................-......../....84/...369.496..........917.....................464.................. +............................812.........785*...............848..506..................*116................=..#............................... +..........694..257..876.....................15.........176.............................................593...168...914.230-.200.........500. +...427...&.....*..........................................*.............=...%....233..616&....299*.................-....................=... +......&......598............571/....60.........897.......911....34....106...713.@.............................933..........288.............. +..314...............563...&...........*...........*............*......................198........................*............*591....246... +....*......274.875.....+.114...........105.763..319...................*730.....158.....................487.......505........................ +.439..........*......................*........*.......595.190.173..653............*...882.812.....*230..-............522....=.......192..... +................554...825...845...797.572..814.......*.......*............*....167.......*.....581...................-.......330.......#.... +.......82......*......$....#.........................318...............326.924......880....288..........*.....44.....................+...... +.....-...-....526.........................=981...........959....................*.............*......915.116.*....$...=..............577.... +..130..............................@330..............414.*....679........999.344.611.......432..................690....502.................. +.............476...#.........................................&.............%...........303.....731.........681.............................. +....@791....*....152....397....*.....975............904................225...............*.......$........&.....169.207..................... +..........995...........&...558.857.......141..803.%....-........199.....*......573..63..315................*...........519................. +......................................158*....*........737.........%....399....*..................#47....100................574...#333...... +..........56............822..................665............................563..383........................................................ diff --git a/2023/in/day03.ref b/2023/in/day03.ref new file mode 100644 index 0000000..b20187f --- /dev/null +++ b/2023/in/day03.ref @@ -0,0 +1,10 @@ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. |
