Add day 15 part 1
This commit is contained in:
parent
b38c53b886
commit
07539273dc
|
@ -0,0 +1,100 @@
|
|||
3885193879879881648295721863446819761149766587914443899626972913439119995971841919742454431896714994
|
||||
7771129199159639477916156846991956691722436614294489569877288313857938998569854848811311873297851994
|
||||
5397898789999499191819517198785943798491221139523487792596493985724999919868993675269878171629998699
|
||||
8959899713991722957643961289419687998443291919429187159951653349827977821257719997961312718268193198
|
||||
2846219932569842879891692234614311812799724893633989588399819579153814261516987262948716511752796311
|
||||
6725199579118269699472336812618911979957922329399797959986179939314145983243295469598939327129574198
|
||||
6818219696128568181789718885928896191549477191193599939262477914196391139665811994294494912838651859
|
||||
2221716296945891267779128161299579949218877525184884798227965899389488291774899969293987797832516878
|
||||
9213844231433499999179428692791378242173289513286965996976983442779617964787586326697988447265876996
|
||||
6313993561615886999167182992899141648998552369794394923931327323397566885891787478878722831777571486
|
||||
8713991289549818763589939991895849521891966988198382597778921227699759737659939699671923799995284356
|
||||
9591399317958271818963894572368319763963548141193644118635854987471693848981917779146394281461959595
|
||||
9371563863819447298489391362565198489677387981171916875818269275785921792592869889719184537255926941
|
||||
9759413213254611555382917156331878594951999289273329999795947169439981616213999777916131384672945781
|
||||
3679696586894978847711411569255844845182781854737976795191261169589939159794718699997493872499236216
|
||||
8494291884994125877784376799158842599959994569992371213981397915549319462895179958195199263973799942
|
||||
2735796245119192168162841995749994338197856789958799128998179176929129769798961229228259986315798618
|
||||
3919549626995852791812998455881197733986917951189161796996622918479591289192199822989316196969797761
|
||||
9221811969262729999666392749549395194914927623938931277683992712289972984156294168596935911994723579
|
||||
9259839967959351183699987369122784879628595673587616317281991469967675676149919714698676133489158258
|
||||
1871658432298942253177598476366881898185855558899669482116863111799951999125874191992364757787788358
|
||||
8739852825782614519891679845942972188264816197178738927924889282235769279955911364987337173691463948
|
||||
8299121513497179625559826719786159935487869889948649939416556967585528149132799993281891864295925273
|
||||
9667999482981521598853938199191994937627975958963142955995573785779998768777953536129524791999139921
|
||||
8565711677873119991423986336939891313758995995761498979518974799162693272998776826559842372285298158
|
||||
6822737359889336999591591889475198863399198181192341869964891549839939496253238918989492776598168948
|
||||
1932768376317118329349221931269281281579761312419134999386689181799263851995829415737796344479779161
|
||||
1146699126764988361859799888677969955699566981916991732711866985948137842221879714999415127978969556
|
||||
2716826999776621568995483149915971956954949792131367142893274976282898295419519294961429727779179618
|
||||
9819689511469361769815986275122539755735877514917521784678889599769389571241738999916766875682319681
|
||||
1739898836468191968269759173841389779923491989982797492258199917378161399777715818949121291398318935
|
||||
8991681277139272161888388992817669861998471779697922729191795771917917339123894529985942176275989194
|
||||
8788879213844991256944695116539936618798879147682219917761678773156192616821535733828489999993567517
|
||||
9792112419978643891198386999273939716785777389699733728699741312154995954284998916346921276226192923
|
||||
8936179399498567787146928338481217274594929625685299262145795358129817176299499988115561439969169721
|
||||
3711996665399999667439695218315649989919198455461991968853148989594896318519998541221179229418248199
|
||||
1291656132987641899759511819979169622814748741595934916983531975799629118915179362474688489529695892
|
||||
8714922154618318979688217298952681635899934279199225395668557918594721827676544381719997629385251241
|
||||
9593279399413929493478294797355334766829491318569163644378429481549576659858687835137187849471951446
|
||||
5854979532981199699169869431926898918194919286675941191958919822798687999927843765176497122196781784
|
||||
9997819331993888314786377865356591696892611198299964968995887152722272842273694726639232942699932183
|
||||
1896184172814918918624961195543395551748983681949128898432976291935921971198195382973914172929297364
|
||||
5837941661987651896287486979997298931652327946749692822583754186238849886262949839188999896727858939
|
||||
4461864711682831868916812799119298619958698189619694951249686988389189729672287957439646596874979222
|
||||
9992395918711165753212128992797846899956889991997831181969254119177984939821658789949262131839363344
|
||||
9913929894915753891516982919449993561972671531999989189396939298993831699699189992855785911896267529
|
||||
5977191539399768769691985346853389983329338128811976829923188998957757698766999482989529913351836217
|
||||
5869199599389289736799521175141562189996727899189998168814918991864194179395421179268799759179886971
|
||||
4995393573286591999148456775893729271716999944792426197991892963522993979581797912398388197438929879
|
||||
8175985762297792193449825891451669173639831696686977185968298848778111519969913991892852196913658589
|
||||
7551823929217592981312974557685142328463465848839872122469199897971199199199999869473895171251997563
|
||||
1487931558946968829951763519656937487546792895476992624978852921947139983499286385266138258935193694
|
||||
8195371149998678396987884326188152573756198921899135197653384711783648729791328851979354497969392986
|
||||
9179155119423919779667686698689126113994825739951498172517199198989915292457344126929486295984699195
|
||||
8692999656696952931996533623896191984859965893774589592193836681957292599821971991739911926636959666
|
||||
9985998695572727293692296815985734817249613192327999997928979519414463322843958491689891537727548424
|
||||
6785915139891383111991993528714959958913992938996589981164194172299991578319949554372972869927718986
|
||||
6812969848431999313262899194267519731924982239497181155761348191171378191195481196281927177836119913
|
||||
9241449944689196375316993727965949184951599169899984428189812459955788198778389997529996294911755145
|
||||
8263933869291918953191161435562974124985817581189466197959111943314796298198689894947297889469378986
|
||||
4993564891474912898412755199548395176295488917317973937991914952763769731425198161936449638498547989
|
||||
3919725846368347989866832637399995924176992897196895631935386266839399384868371191995999792294157491
|
||||
6721931979356517372592778489839423879114997193842669196756218474195487978729966911652681225999411831
|
||||
9597499799876611196188943929378999389985498674958963168888777783921914243997878195898999269889294551
|
||||
7897535911127197896722972984867282697846153651471124481587828169258769897847822878831383595997369791
|
||||
5831297848191951191794896484123496673799414573379198153884568889918681971924789669965852999915958216
|
||||
7823991369999344428821749717198796788139384181691959399667159925814883764695397857939481345499559511
|
||||
9836733614869491139178987881497398239923811838118399887762713912361489159279391783489832826615994916
|
||||
5877773698989894889438919996977776999993296984984712292923899479697593879272739579864981289939932771
|
||||
2542185648228941381995651795998445875943469118314769118387799816194799838958843997735743139876711369
|
||||
7882123912881669791399598994599691151814649879989998698859687888628677316516841799215998992997293761
|
||||
7767319886981189839889892188919899791985112499817113117779879369341826441898281556786828873779689187
|
||||
6517976954449996987552989793838655741268494369149928959518173199587537996199483924421127337969771577
|
||||
1678653257398811536189799659999587998996928746191167927896289997861386532556616499279932951899917642
|
||||
9572579385451723885561779217789536117629994941561764419872276282572795264367221933191167982181957692
|
||||
6315852299992498418936196529371999229568595139535889371219877199885619437539359128266112177998914116
|
||||
2985881397958192379949795596996995193595668153648864957619593653435123842865151846253963551826892972
|
||||
9181619666251279936984265494857188682368994967895269896197989182176863694399432137388934122967614868
|
||||
9593114153726456769958948828218389342288723549641819941784998231817667699584449492849222498974496136
|
||||
9916299479892917793359999793199917957999777682139987212993989599198969329559479959128381894449114895
|
||||
4921471293815869819213937928199292981798364834953986973773897966731399149773957385692948461798953879
|
||||
7939219727388581199654888522599499194589989324361564926162951286688874312293589785941177395118942618
|
||||
8437633823193749987229493992993524679593478997117499862387295887499449937233698762911944962872393932
|
||||
1957595152944134991298986921999686429196188856825582891848975799829298278988951176691983617917489212
|
||||
8139893189759536719371198967498834639886725963146991186976281274983731942436781991256754884914659449
|
||||
4822925997999862149916429996168888845197975949578265942598274998974448539673169651999879289189841361
|
||||
5241531889998799394875217768389396418913985512418883112994376949161819917922624829789318615483896826
|
||||
6952433478181299923958981383758182467119587745189738489917881951247839114135919899874698935397678939
|
||||
9998178281168138219688629715164393989381385362839223828189989111183769799437851885123913879668776872
|
||||
3479299968682178666263899849126199949521388659643946294899888287792876178914598796793938289488163984
|
||||
1225626697911721927373893967969993763233461954269119238318594463694999168939951368891298529984588448
|
||||
7889492445296989153714423965492969819915271485178757588997394798779977871918771629125225796727359893
|
||||
5778897199718433999153132896886712358389157788434824924928192158771497689159318619912199459651238768
|
||||
1795836197799479917189998598989489967373995198279891918988899853122171424496868123951671992389679399
|
||||
6529853939912918791899868397879535727748279386788285494854993938674449895593868599789269647149499765
|
||||
1878818925186485199559539319973289118966119867769847635447481798718159348669599911752797688859935711
|
||||
6236419319741929511583546874658811117988969497296985889278883242999599946847999979139915449889988998
|
||||
1198678683349613936877939891939118912969187857959968963634732792198997359438989686371429988735182698
|
||||
1998223474469228586568189895871742229825728879174631691151921998877937199256189155647765161238919158
|
||||
6432548917454758948968687351996995554975421965763752579179713997554661699137621894836694779175686366
|
|
@ -0,0 +1,69 @@
|
|||
#![feature(drain_filter)]
|
||||
#![feature(test)]
|
||||
extern crate test;
|
||||
use aoc2021::{
|
||||
common::*,
|
||||
grid::{Grid, PositionND, VecGrid},
|
||||
};
|
||||
use itertools::Itertools;
|
||||
|
||||
const DAY: usize = 15;
|
||||
type Parsed = VecGrid<u8>;
|
||||
|
||||
fn parse_input(raw: &str) -> Parsed {
|
||||
VecGrid::from_bytes_2d(raw, |b| b - b'0')
|
||||
}
|
||||
|
||||
// I win the award for slowest solution, at least.
|
||||
fn part1(parsed: &Parsed, grid_size: usize) -> usize {
|
||||
let mut paths = vec![(PositionND::from([0, 0]), 0)];
|
||||
let end = PositionND::from([grid_size - 1, grid_size - 1]);
|
||||
let mut max_risk = 0usize;
|
||||
loop {
|
||||
let min_index = paths.iter().position_min_by_key(|(_, c)| c).unwrap();
|
||||
let (next_candidate, risk_so_far) = paths.swap_remove(min_index);
|
||||
for (risk, position) in next_candidate.neighbors_no_diagonals().into_iter().filter_map(|p| parsed.get(&p).zip(Some(p))) {
|
||||
let new_risk = risk_so_far + *risk as usize;
|
||||
max_risk = max_risk.max(new_risk);
|
||||
paths.push((position, new_risk));
|
||||
}
|
||||
paths.sort_unstable_by_key(|(p, _)| p.points[0] as usize * grid_size + p.points[1] as usize);
|
||||
paths = paths.into_iter().coalesce(|(p, r), (p2, r2)| if p == p2 { Ok((p, r.min(r2))) } else { Err(((p, r), (p2, r2))) }).collect();
|
||||
if let Some((_, risk)) = paths.iter().filter(|(p, _)| p == &end).min_by_key(|(_, r)| r) {
|
||||
return *risk;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn part2(parsed: &Parsed) -> usize {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let input = parse_input(&read_file(DAY));
|
||||
println!("Part 1: {}", part1(&input, 100));
|
||||
println!("Part 2: {}", part2(&input));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use aoc2021::*;
|
||||
|
||||
const TEST_INPUT: &str = "1163751742
|
||||
1381373672
|
||||
2136511328
|
||||
3694931569
|
||||
7463417111
|
||||
1319128137
|
||||
1359912421
|
||||
3125421639
|
||||
1293138521
|
||||
2311944581";
|
||||
|
||||
test!(part1(10) == 40);
|
||||
test!(part2() == 0);
|
||||
bench!(part1(100) == 656);
|
||||
bench!(part2() == 0);
|
||||
bench_input!(VecGrid::len => 100);
|
||||
}
|
|
@ -49,6 +49,32 @@ impl<T: Default, const D: usize> std::iter::FromIterator<(PositionND<D>, T)> for
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct VecGrid<T> {
|
||||
pub fields: Vec<Vec<T>>,
|
||||
}
|
||||
|
||||
impl<T> Grid<T, 2> for VecGrid<T> {
|
||||
fn get(&self, pos: &PositionND<2>) -> Option<&T> {
|
||||
self.fields.get(pos.points[0] as usize)?.get(pos.points[1] as usize)
|
||||
}
|
||||
|
||||
fn insert<Pos: Into<PositionND<2>>>(&mut self, pos: Pos, element: T) {
|
||||
let PositionND { points: [x, y] } = pos.into();
|
||||
self.fields[x as usize][y as usize] = element;
|
||||
}
|
||||
|
||||
fn len(&self) -> usize {
|
||||
self.fields.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy> VecGrid<T> {
|
||||
pub fn from_bytes_2d<F: FnMut(u8) -> T + Copy>(raw: &str, mut f: F) -> VecGrid<T> {
|
||||
VecGrid { fields: raw.lines().map(|l| l.bytes().map(|c| f(c)).collect()).collect() }
|
||||
}
|
||||
}
|
||||
|
||||
struct Boundaries {
|
||||
x_min: i64,
|
||||
x_max: i64,
|
||||
|
|
|
@ -67,6 +67,18 @@ impl<const DIMS: usize> PositionND<DIMS> {
|
|||
}
|
||||
}
|
||||
|
||||
impl PositionND<2> {
|
||||
pub fn neighbors_no_diagonals_only_positive(&self) -> [PositionND<2>; 2] {
|
||||
let PositionND::<2> { points: [x, y] } = *self;
|
||||
[[x + 1, y].into(), [x, y + 1].into()]
|
||||
}
|
||||
|
||||
pub fn neighbors_no_diagonals(&self) -> [PositionND<2>; 4] {
|
||||
let PositionND::<2> { points: [x, y] } = *self;
|
||||
[[x + 1, y].into(), [x, y + 1].into(), [x - 1, y].into(), [x, y - 1].into()]
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! dim {
|
||||
($d: expr) => {{
|
||||
|
|
Loading…
Reference in New Issue
Block a user