Add solution for 2022 day 08
This commit is contained in:
parent
2ec8b3934e
commit
4eb67b02d0
4
2022/Cargo.lock
generated
4
2022/Cargo.lock
generated
@ -36,6 +36,10 @@ dependencies = [
|
|||||||
name = "aoc-2022-07"
|
name = "aoc-2022-07"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aoc-2022-08"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "either"
|
name = "either"
|
||||||
version = "1.8.0"
|
version = "1.8.0"
|
||||||
|
@ -7,6 +7,7 @@ members = [
|
|||||||
"./day-05",
|
"./day-05",
|
||||||
"./day-06",
|
"./day-06",
|
||||||
"./day-07",
|
"./day-07",
|
||||||
|
"./day-08",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
| 05 | ✓ | ✓ | [Rust] |
|
| 05 | ✓ | ✓ | [Rust] |
|
||||||
| 06 | ✓ | ✓ | [Rust] |
|
| 06 | ✓ | ✓ | [Rust] |
|
||||||
| 07 | ✓ | ✓ | [Rust] |
|
| 07 | ✓ | ✓ | [Rust] |
|
||||||
| 08 | | | |
|
| 08 | ✓ | ✓ | [Rust] |
|
||||||
| 09 | | | |
|
| 09 | | | |
|
||||||
| 10 | | | |
|
| 10 | | | |
|
||||||
| 11 | | | |
|
| 11 | | | |
|
||||||
|
12
2022/day-08/Cargo.toml
Normal file
12
2022/day-08/Cargo.toml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
[package]
|
||||||
|
name = "aoc-2022-08"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
test = false
|
||||||
|
doctest = false
|
||||||
|
|
||||||
|
|
||||||
|
[dependencies]
|
5
2022/day-08/Justfile
Normal file
5
2022/day-08/Justfile
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
@part PART INPUT_FILE="inputs/puzzle.txt":
|
||||||
|
cargo --quiet run --bin part_{{PART}} -- {{INPUT_FILE}}
|
||||||
|
|
||||||
|
clean:
|
||||||
|
cargo clean
|
99
2022/day-08/inputs/puzzle.txt
Normal file
99
2022/day-08/inputs/puzzle.txt
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
213301311230020143114320444110403320013225525311631030063110421431345121304222223004301412232100001
|
||||||
|
032203111100043323010213335301511304130605406233153314443635022154110215552041255340234040130323222
|
||||||
|
211033104233224304235521203100252423434120553365402362266533500152442121543412024410024400242202302
|
||||||
|
013233443223404100042450150455321226633161540105245332600255253210530355343333525022032244210410030
|
||||||
|
100023412423313250410335224414340304541343205513226532163144253316101245551432014201003142341223120
|
||||||
|
232241013411335302431133421201615210501105064501562020244216516152310063135512221455045300342211403
|
||||||
|
001211403144420455554423216332215200446124050665565420055553546105000034160513122231050524220140011
|
||||||
|
010222034210203122402014343566262222246103065613373733360061263604223104106135234020024543242111403
|
||||||
|
133113440330510405504554625242222200403366266153277146434533626152126510506334524322514123130210430
|
||||||
|
101113041432001410142511513526503002532162171436233673475525363642630611162522443352502205024124331
|
||||||
|
324312213553041030426555020660523457632523764416232512767555566551645346253661665442120340412302333
|
||||||
|
222123425201320323442604100535316531114411567157656317576577367225171531643603563142005225003430304
|
||||||
|
230404233442530203464653423224331773327325127722375144767252343327447164151244036263205440245102240
|
||||||
|
020311403313133205655466214461271263366243355275525631512333123546621214666605403224540121240310404
|
||||||
|
223432305440325556610201021457211262246351424434658664512646355413752541213342520233125105551331330
|
||||||
|
410041131121534131115352632731175264753542438584525556827443273227227341353644032545140104543024000
|
||||||
|
423453033330221431012636112324171513474458778337333686836267386757554467754342542115255232403454502
|
||||||
|
141503311314266450662537113455732217424265537246342583573854773454123656425412332165232141011402443
|
||||||
|
234300432213662423462523257572767147464533826775238485872738873855313755143164616151012552443215022
|
||||||
|
033241031332110563643465311621477762732537565784247833454662525727847541531227547400151244235203533
|
||||||
|
240224003236231611317717416272324253823854672343372244362284832853474756317737336366405432515551141
|
||||||
|
044253414432535641343531616777683583443747683557473845555627346482283442342143325272142654451141515
|
||||||
|
414053450103324521235112631345676573684558268948763749867453426652857573524661772775625535603534203
|
||||||
|
452331543214300655576765671548836233443573348365895488958888986552454663627672751663623063544153145
|
||||||
|
213123262132446231133475424568643584667349958744556395357354533357757384556355745247423355145351432
|
||||||
|
342221165500622631522633663654256837955886839433536678696786657748837377774671514433313266661623225
|
||||||
|
130525523663122464775524878454547596977765643878496844435439637547575786557583612611151265003051021
|
||||||
|
052340513053426716457138866685633365633897943973977354954887846375952874745888233531473341566254321
|
||||||
|
551302462051112655343688535254486759649794957694399376794599363586956664557346654412445441323166111
|
||||||
|
423314110434112256221674555238787433834639693845964995577597377494776955386853327646756442555444510
|
||||||
|
011552365334113123648585377225387834796343948497694499984846743648856748248673875362436356643131030
|
||||||
|
020256210453324157237867264233786435595376465578894967846665837389745665523842772735511735230401305
|
||||||
|
414103611044274157467628724497537843895695848765959754656769555753737934946468683576713214360041123
|
||||||
|
433526245602544722152833257988587439889445445556677697744745565449795769495384242545477273223656031
|
||||||
|
535042251546455362245227649985889898476674985559449558958898867548978656953754247384375772356045111
|
||||||
|
323611233267761557486537664663454997974544898976574687679659946846478948476457434665442317312022000
|
||||||
|
316230521014145628676243456554785557548446999847699997896955585649999577677524437423612355506115351
|
||||||
|
233431152247465456337854474647793964448444896895598667778778977886586858366824426252253276231246145
|
||||||
|
404014103521614562682335866979974948446875556785969767656758585669745354988763786574741512533625562
|
||||||
|
556325261143135338658723684954548748574497796665868666555699769757857835743973646256523654252240304
|
||||||
|
266422162456222134742357585545497865656898785565975797777687675798668987868793755627527272716412640
|
||||||
|
455002563563223122565674686979694669768589897756676776589979994889496595734434837783346346143450216
|
||||||
|
423623414476221783674588469588594648549897978796997876765586667888788799455377766468736364172434104
|
||||||
|
461015352326313587764337768946558646676685575698789678659997955774574676447688826876753362163336656
|
||||||
|
454301143535414652353245365369994964967576769566899779689689997774557795374734563262787562775060012
|
||||||
|
653222122731245568566556776985848547968556787679686669879569789644864455634454383364432375535243521
|
||||||
|
342316353424453765457789959854867655565788786777767789769587758955585444849988572535856114662250155
|
||||||
|
250055446366322362645884695995797555897869878978866789879755795875895966677947756776764266155654244
|
||||||
|
355444312633526744883699577599657855885557866766789996876599887759779647979599732827875247133603306
|
||||||
|
355162377751474644575435897745996464769988698987886888687666856555545976965644336524766776743736625
|
||||||
|
254133626515278463466897589667596746698997878997878697797768557598696668363844637544382211555636416
|
||||||
|
360253472354274732737486847668779778565699779896887679689978676579598846458534776876223325777613344
|
||||||
|
366252652724126345554794996866795676999857566667778779778577866995589777749549487475787272211122413
|
||||||
|
431342467147246836362775364375899585598957998976787989869955888769495445683934936635824416757641010
|
||||||
|
146634042466136236774369894774864768596766587699767886867877869796986964585967784682527733236415223
|
||||||
|
156304456734165226274783349494798495455769775689668799688775958559896465983366587486585214546510053
|
||||||
|
605206622655775234243663487486755585458887997787688876799655859888594564838739982853787622517046650
|
||||||
|
423630024631261432424833633564887777468876679966768898695978957564669463675476746456267163626043120
|
||||||
|
416652052662467458722777939356947478968767765657999676685879795995878796976469668655854466754261205
|
||||||
|
220024367637577658627779495636364487645958666565757789856885784694646544987687453532241513327314431
|
||||||
|
001205127227723722658235495649657985496688668778766957998998798794864455548548428355817465523414166
|
||||||
|
025161664544242365866263435484535744784969979677987979756856584797657359539943238646534427543545433
|
||||||
|
244045631473162278554572386595375669975856555597887558858964647474989998785563783734436666571035624
|
||||||
|
353335045651153142532442577843953876787689468686959685568475794765699446555738548335432232450554121
|
||||||
|
534351561042516364445286348344476866564776585989956875745847495865559949864377745388672675423056350
|
||||||
|
540405145026441333666346536966747774764948686678756744954767654888755588373534364785112116636432366
|
||||||
|
310352124627123114353328539488485988645766859746585887776555674793469866749353577236524625202164415
|
||||||
|
302556262406343647252785226737766869695447799556645996986594999884557567783867558422423434634241154
|
||||||
|
211616662653431774753648337273939739669445597598598567499698999634858586533835878556437361125533615
|
||||||
|
213322310145376773757677875437498844399795974564596995697555949834733955347632738712147720025645345
|
||||||
|
541321441544454414357337667684975575996485879944954889455796373396388797572654885262161174164114144
|
||||||
|
210116521136412425163348474242489536463548693688568577695359465578777582266853876663474766131111432
|
||||||
|
440444421253572763622188785355529648358638647573673687439788966944953762338575361334246603164356300
|
||||||
|
054351113510556462163317846522547487438935688785489478376663949834482363723855432634571000412534200
|
||||||
|
415304004006217366116355647365825399757479337377973788354755373575483332857751272645472164332645502
|
||||||
|
525435240504040365515335242682485535453593777959944374675953567553247848654865615214462532062624325
|
||||||
|
421513461342233446252413525637885564683877795686776666737637754842856378347452214626360461550624343
|
||||||
|
445211110022304542377331711668252868465579847656559543637984976636772273847434447777513040455425001
|
||||||
|
302124154303411402267242643588322823363568229589888949467266487378624266732545514654524630362511225
|
||||||
|
434012041224431221666125342437865823243772555453745443857363576672867874747661743241420321212130504
|
||||||
|
100134115561650365223124662615754286283234445334825852647834765375247667746624712416412652603245504
|
||||||
|
132253131224342151537452754232162483446373772738888327822756583445567161173342212362212631430242244
|
||||||
|
141002023046411154261327625245617625735662275264772823842687844832822441456725161115444254441235253
|
||||||
|
200553032020156206063203436454516443664846483233468486433667666766457415762323462062450463344313222
|
||||||
|
201010451225251563362461722113361342618764328454374782643642448326624276154240645532300534152314133
|
||||||
|
444032143444456452225426623113762543374213652282765487482433537313361316346564006642411354445335302
|
||||||
|
020342143213005041512255604774164472633331333366228437463563712421513134547351014225565411402514242
|
||||||
|
422332412253335446146523426227763642635272527343272516342561634646544572211313465325345004130011204
|
||||||
|
440123035240040516464102063022655255262623463165725261752452217412716246215333530160420454541132140
|
||||||
|
143323423014024055541430440445462316717323336261624421453346176657765065655202543223211535243321002
|
||||||
|
133332221053010114200220465260340352674371352121611744231126156721603415356035634351251443041000331
|
||||||
|
404032402110401515330522443316116601151652142553741456176754127651434134661511225255414401412224430
|
||||||
|
300223104202155300140023253011351612643321427612315624461765340336455620510150244014335414004342223
|
||||||
|
230303331103324102513523351221602260544055166522131313540411061216126232512021403023242041242141113
|
||||||
|
101421341122145002224050506061436405231312566403515452555542525100113200314454334451233442312303301
|
||||||
|
303031334122345315210233432302103062261562324500250254554303502015306535302122202415335332113422223
|
||||||
|
323333442324333342200411433124165341334121455446602633255250161540440000210002155250512324214002123
|
||||||
|
020000401400224230303002512321356354406540161122666225140252401423502424351314403352020313231212111
|
||||||
|
000130010133444334124411122525303126205322331530363021562050544410330230535525023331433124342133030
|
5
2022/day-08/inputs/test.txt
Normal file
5
2022/day-08/inputs/test.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
30373
|
||||||
|
25512
|
||||||
|
65332
|
||||||
|
33549
|
||||||
|
35390
|
66
2022/day-08/src/bin/part_one.rs
Normal file
66
2022/day-08/src/bin/part_one.rs
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
use std::{env, io};
|
||||||
|
|
||||||
|
use aoc_2022_08::{parse_file, Tree, Trees};
|
||||||
|
|
||||||
|
fn solve(path: &str) -> io::Result<usize> {
|
||||||
|
let trees = parse_file(path)?;
|
||||||
|
|
||||||
|
let mut count = 0;
|
||||||
|
|
||||||
|
for (x, tree_row) in trees.iter().enumerate() {
|
||||||
|
for (y, tree) in tree_row.iter().enumerate() {
|
||||||
|
if !is_hidden(&trees, tree, x, y) {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(count)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_hidden(trees: &Trees, tree: &Tree, x: usize, y: usize) -> bool {
|
||||||
|
// Edges are never hidden
|
||||||
|
if x == 0 || x == trees.len() - 1 || y == 0 || y == trees[x].len() - 1 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let above_hidden = trees[..x].iter().any(|t| &t[y] >= tree);
|
||||||
|
let below_hidden = trees[x + 1..].iter().any(|t| &t[y] >= tree);
|
||||||
|
let left_hidden = trees[x][..y].iter().any(|t| t >= tree);
|
||||||
|
let right_hidden = trees[x][y + 1..].iter().any(|t| t >= tree);
|
||||||
|
|
||||||
|
above_hidden && below_hidden && left_hidden && right_hidden
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
let infile_path = env::args().nth(1).expect("input file");
|
||||||
|
|
||||||
|
let result = solve(&infile_path)?;
|
||||||
|
|
||||||
|
println!("{result}");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sample() -> io::Result<()> {
|
||||||
|
let result = solve("inputs/test.txt")?;
|
||||||
|
|
||||||
|
assert_eq!(21, result);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn puzzle() -> io::Result<()> {
|
||||||
|
let result = solve("inputs/puzzle.txt")?;
|
||||||
|
|
||||||
|
assert_eq!(1679, result);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
85
2022/day-08/src/bin/part_two.rs
Normal file
85
2022/day-08/src/bin/part_two.rs
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
use std::{env, io};
|
||||||
|
|
||||||
|
use aoc_2022_08::{parse_file, Tree, Trees};
|
||||||
|
|
||||||
|
fn solve(path: &str) -> io::Result<usize> {
|
||||||
|
let trees = parse_file(path)?;
|
||||||
|
|
||||||
|
let mut higest_score = 0;
|
||||||
|
|
||||||
|
for (x, tree_row) in trees.iter().enumerate() {
|
||||||
|
for (y, tree) in tree_row.iter().enumerate() {
|
||||||
|
let score = scenic_score(&trees, tree, x, y);
|
||||||
|
|
||||||
|
if score > higest_score {
|
||||||
|
higest_score = score;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(higest_score)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn distance_to_highest_tree<'a>(
|
||||||
|
trees: impl IntoIterator<Item = &'a Tree>,
|
||||||
|
origin_height: &Tree,
|
||||||
|
) -> usize {
|
||||||
|
let mut score = 0;
|
||||||
|
|
||||||
|
for distant_height in trees {
|
||||||
|
score += 1;
|
||||||
|
if distant_height >= origin_height {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
score
|
||||||
|
}
|
||||||
|
|
||||||
|
fn scenic_score(trees: &Trees, tree: &Tree, x: usize, y: usize) -> usize {
|
||||||
|
// Edges always score 0
|
||||||
|
if x == 0 || x == trees.len() - 1 || y == 0 || y == trees[x].len() - 1 {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let score_above = distance_to_highest_tree(trees[..x].iter().rev().map(|t| &t[y]), &tree);
|
||||||
|
let score_below = distance_to_highest_tree(trees[x + 1..].iter().map(|t| &t[y]), &tree);
|
||||||
|
let score_left = distance_to_highest_tree(trees[x][..y].iter().rev(), &tree);
|
||||||
|
let score_right = distance_to_highest_tree(&trees[x][y + 1..], &tree);
|
||||||
|
|
||||||
|
let score = score_above * score_below * score_left * score_right;
|
||||||
|
|
||||||
|
score
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
let infile_path = env::args().nth(1).expect("input file");
|
||||||
|
let result = solve(&infile_path)?;
|
||||||
|
|
||||||
|
println!("{result}");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sample() -> io::Result<()> {
|
||||||
|
let result = solve("inputs/test.txt")?;
|
||||||
|
|
||||||
|
assert_eq!(8, result);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn puzzle() -> io::Result<()> {
|
||||||
|
let result = solve("inputs/puzzle.txt")?;
|
||||||
|
|
||||||
|
assert_eq!(536625, result);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
27
2022/day-08/src/lib.rs
Normal file
27
2022/day-08/src/lib.rs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
use std::{
|
||||||
|
fs::File,
|
||||||
|
io::{self, BufRead, BufReader},
|
||||||
|
path::Path,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub type Tree = u8;
|
||||||
|
pub type Trees = Vec<Vec<Tree>>;
|
||||||
|
|
||||||
|
pub fn parse_file<P>(path: P) -> io::Result<Trees>
|
||||||
|
where
|
||||||
|
P: AsRef<Path>,
|
||||||
|
{
|
||||||
|
let mut trees = Trees::new();
|
||||||
|
|
||||||
|
let file = File::open(path)?;
|
||||||
|
for line in BufReader::new(file).lines() {
|
||||||
|
let line = line?;
|
||||||
|
let tree_row = line
|
||||||
|
.chars()
|
||||||
|
.map(|tree| tree.to_digit(10).unwrap() as u8)
|
||||||
|
.collect();
|
||||||
|
trees.push(tree_row);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(trees)
|
||||||
|
}
|
@ -9,4 +9,4 @@
|
|||||||
- [2019](2019/README.md) (0% completed)
|
- [2019](2019/README.md) (0% completed)
|
||||||
- [2020](2020/README.md) (20% completed)
|
- [2020](2020/README.md) (20% completed)
|
||||||
- [2021](2021/README.md) (68% completed)
|
- [2021](2021/README.md) (68% completed)
|
||||||
- [2022](2022/README.md) (28% completed)
|
- [2022](2022/README.md) (32% completed)
|
||||||
|
Loading…
Reference in New Issue
Block a user