Day 15
continuous-integration/drone/push Build is passing Details

main
Julius 2021-12-15 14:25:05 +01:00
parent 3a00251b3c
commit 073a0dbcdc
Signed by: j00lz
GPG Key ID: AF241B0AA237BBA2
3 changed files with 240 additions and 1 deletions

100
input/day15 Normal file
View File

@ -0,0 +1,100 @@
1631196814251372911576579235812119112999813211934811373149623868144186389623722791119539949522144313
9188121321122851414231854163348238311917148974121419323871752541929227519231198852351293513134914473
4215676183741213527339331353288981132197947331219528111783619598331913296928619184942471785158367454
2251217171971152724177163921253115122983577112942474115151352465226121141697681766839691161112219324
1961611243289419266932117156684919673116135119671489326291133816113132512372721931413936141348491174
7269114374391533415635121743625566592554111117711241378517141422713695418216125897637975945511115522
1144123216126218521233215427542393912717645925127237985932114949434192124652161499471135959814578213
7796911551446529195636181229998815359954454794223223462717188113621233432281961212942721262217614613
6118139111941491415311113193921413851521589111893147725216195198122397111413115311366121921627452518
1331264861134181111918113115248139311614354942194412221294329416731932123911261981493213891761232278
2221144593111111912992873641826161238641422623111741211211518357366249184344191148548614212244354734
1211213371213148294142153457533451931866873944444194664128116531396333843195292119371231891868991141
5831917921213162781451341196354111153139261622831671142244121611561361418691252224135365111329836345
4138219222532973518122416261247534313154771248749165139128311153131378273119913353321121189987111218
7592334519894651934117182368717748728349225314316818631357329917913899122222317415829211918984472224
3529175122394833837311556412119135519619726712474999794918232711732216596127762564141171871619424237
3811393894214611963837112311561131414275467797111295142659481511391222811435211645124732872247121173
3173296527331637499232361144799131431129827112551361111233567454814221419531332911113541333811611161
4163891857422177592289991612336735419295845427316194531541411784621119116379142173129441111899274152
3517131642652188283112778215811244153141686651213636112545392472121582491151322156419586312874359791
9693566118818185918862125826722128313318227114928267863142179212514689219713693391879641397611742624
5289313922222131322321759341211211143123969192112173141216911115384347112431363387719111515937362788
4966821841158112276911292252282531318266126917161665323441152195177525617843989725926419592293915841
9427715674347511131183159319724911513177723332277514816219785137417122459113591113117371922242432141
3812391991319161863571741619317494259138312498283218186119962111111322933111156912129311211361299732
2748225685171923963542619357241266329195292813733186931124542181194142127691111743421423373615624321
9531298911291715167822699611223153831851157425916669512228121999812232324191599893781224113431448161
2891228355912333913182991329511965117911624224497739482524533516533972914514141111218425284321377721
2392681536132118225296242231917682711533193153139453227362514131852189661144942131282863945252521391
1567442934182128674727141212129359121112721119322782334881816914225919132921993219112491538371321794
1851121361571352321135771114434418715428231469375354434831131216711193961712699818226124312321791192
8568221155322245167244326491552112112164341815539523535938114762199171462119197912229874259422126928
3862153285471615891361912115213789126413194374124242361839224236133476292148411914153928149414444961
7461614147763131314729141991937121713418823199979558936646289212684938128839126168981842499124699948
8265421321298783511889124781934115116514712658827292419163265374319848122623162121741431126189413714
3615591491111213129361911857716219134853312281219569912692312373777116174347161129929917714271572417
6996142241978141451142834339345359211195945132194281949428218333124239517519991621939221729612195587
7453424594171241919916135939617918917947229726136326874465141215221112516526966767711981138874159363
8911951264661289485146575817119114851221117236412371499921316173657127246829496192218847127895111161
1123113419449955451846755127382394821613317934848159212715151895246299242523852391672211289319121519
1924196861511342262243845221421135163244657711452121724125897613437319364233319122615551448271111164
5133231123722341158315177892111565627191791992137785793928261141113113262115795828419452218914216358
6124387731112225351316112312688523814529497441968217944126564998118211519995819214119615918164534426
1752525515747712421697274222191991237551581414218172634946215191917539153568822923272748412323139411
2112812138149164595422722712157321996235689515527117418614112267271914125366621674567541835813451514
8881742352481419174936517146533211326331215444141333893455566214323112221133221661152911221612195135
1216834911351513628171992327145922222663911497229324134248271459321851128734217482592397354583193185
1291317339119291928212177981324772453591113724462116172287361912212197161657352133525999442332211435
3241429379615293177949614321731115131297249712558943263311133226641824698994911181211115187245121131
1111791929711162413141911151841443924411295154671313821798331173411268575123129423122594397973441239
1197377389494849184613145929718181471961256221988946117736829117611948593538119914121429227149218381
5919724778141943693113368181432495841334864461446156641179542144199299975134339334113161641687211229
3123532448566613219791348281111792388295617589813585624726312556993982983544216418749532871175742351
2721129162454965116371352539341912916122114721859396192192416112214431134136556386791411622568215392
3621799113274151192616819931112911147619316186119372987129612314239572118322252941181618629623232699
9813192482642252181172211559131471999285917394523134534147475497951519638431362458263239611498363158
8245535728991769628414412379188511213889882936422421248351947446189912618547748486694324323211141918
1998221314212511122326922816119413531121523933744762819792113629611871834569713577636726416223335212
2154542143191182921154392113111871292423221329599553726289311132542312952111489893241248115161567223
9138913446711888732529173545317299684127473312332811269573217313321953996892576331162211532212813221
2151168218121466631119368341318421172383118447324377166399558315399115128122789117818417261199114111
4286912117121989561772311238351374997915786221491316523117114135961662452712222135991211415912123311
6892743381397586311122737621971129191744621933753413515159315522421711221831813929121371123532474384
8251122735497191174933721352919147616119974453271591619175998133499253258915413899231185951891166151
9512331713273258623161489559275654252516919131719973713211251342434631579819179932589194739622111123
1339115831121535522715134341326763591969241321211951111419653491777532942139935416431856169184181911
8322442891391119221542435923155688223131522411294254334992646331862111958284176126126113861121286191
9177812874423371265452679291625523653984424976279214931134541123739664876887172539131641311252382191
3424121119669565423616749471611641366819942742997819411572369143113914174477341811182562133171462325
1711994313313428253367791521138393591361116122659181592329296195223531524391111563912212721711214728
1551437228341828984252161112181117116121191535261751311235151172627518271345724945232134134294212151
1122656251334226522535362472411411379179911313531197776215191314395332193417399695732937722328973111
8374378619266683323461212711912323919327471139171677141648833836911131112221236115111818114612111364
4362921324721973324932282319973622734233219221114176719141373111198221114269333313749398513849113191
1669421864982478658119633929211112129168923647927144168171145713411128916594418125154829211124963118
2949811735984154723511132731584112935392815811136711272189211318851677185871486193852872719921646713
1128752752399328511232543151549836485324972624231329896492237681265499215321411842128112371721724977
2226137319681917161531571219347264471595923321549164311417718969716955963195871224461118161394785545
4235441925421112237112832171332119221772315716945793126212523111471281229225613454946241136518328311
2936619615411418279664261341121688111121288644899384993123737471186416166457382381114393592188534153
5129929912133411894269551832522215258229221923912693112853741343169472221148214616343391822595187541
9221939111322211316111121551955416212818291329181514244391314421113241989512195432649299174611111173
1318427121511251499322211112262571511935271537519192818191218419133637821163992942716223222171936322
3351814483393439321921981116395757476153117332328984292921912163569165191285725321281423157631377119
1121714151135462121192214364317499151979417195931241591223117715112111668861911521164289191398422731
1818141213311687121119187893592411421941811359181531328135325819537132612118544245111121481811815255
4413194121141633562993511215161912854153341311278591182266346447977312132431971319162251689431471259
7811143121185311162259114151212489914141186372746512312518177425952111213141594171363416214262351514
5113517628143491126213221834835792972575931421969294189289398522929992132146178582336732941118113323
9882859948156919699392156992959495423891191358577595239228294112424199416175617341191289593564311277
5131112911239152319458266235225243429292655981113412112281443489361299162146238153227264331444211111
9128886482518115142371937326194212122198264415241946792349243111681127571967126412714925161618934571
2692917729427716331421922131398233794729447868491611691512342966151512542917291364421311219416233342
5291125973868995179656318544811113699511461511129791112281716699116231821319391859114713132397369713
1359251887142159956112811532511675311111998139353632119924367113356324976241273941662882229642431231
8194297291181931224931292129559462427245813212177618314214187642393229269815518151141722124932591124
1954581652511649212131131371211956691675612331421991137928234749695132513122331441114117129619798254
4289741424715911236217263189784412578418861111216178695219191847216835122285633211921454732479313712
9252146448218238875939619191967421281428148665211911718952111182181114219711127632131551563327591916
4519233214164141882743882142992884172832615414912312373153212281212219298923129375514264315424683991

136
src/day15.rs Normal file
View File

@ -0,0 +1,136 @@
use std::cmp::max;
use std::collections::{HashMap, HashSet};
use crate::Day;
type Coordinate = (usize, usize);
pub struct Day15(HashMap<Coordinate, u32>, usize, usize);
impl Day for Day15 {
fn init(content: String) -> anyhow::Result<Self> {
let mut m = HashMap::new();
let mut y_max = 0;
let mut x_max = 0;
for (y, line) in content.lines().enumerate() {
for (x, ch) in line.chars().enumerate() {
m.insert((x, y), ch.to_digit(10).ok_or_else(|| anyhow::Error::msg("Could not parse digit as number"))?);
x_max = max(x, x_max);
}
y_max = max(y, y_max);
}
Ok(Self(m, x_max, y_max))
}
fn part1(&self) -> anyhow::Result<String> {
let r = do_part_1(&self.0, (self.1, self.1));
Ok(format!("{}", r))
}
fn part2(&self) -> anyhow::Result<String> {
let mut m = HashMap::new();
let len = self.1 + 1;
for y in 0..len {
for x in 0..len {
let og = (x, y);
let orig_value = *self.0.get(&og).unwrap();
for ry in 0..5 {
for rx in 0..5 {
let coord = (rx * len + x, ry * len + y);
// println!("{:?}", coord);
let e = m.entry(coord).or_insert(0);
*e = orig_value + rx as u32 + ry as u32;
if *e > 9 {
*e -= 9;
}
}
}
}
}
Ok(format!("{}", do_part_1(&m, (len * 5 - 1, len * 5 - 1))))
}
}
fn neighbours((x, y): Coordinate, (tx, ty): Coordinate) -> impl Iterator<Item=Coordinate> {
[
x.checked_sub(1).map(|r| (r, y)),
y.checked_sub(1).map(|c| (x, c)),
(x < tx).then(|| (x + 1, y)),
(y < ty).then(|| (x, y + 1)),
].into_iter().flatten()
}
fn do_part_1(map: &HashMap<Coordinate, u32>, target: Coordinate) -> u32 {
let mut distance = HashMap::new();
let mut seen = HashSet::new();
let mut heap = vec![(0, 0)];
distance.insert((0, 0), 0);
while !heap.is_empty() {
heap.sort_unstable_by(|a, b| distance.get(b).unwrap().cmp(distance.get(a).unwrap()));
let nearest = heap.pop().unwrap();
if nearest == target {
break;
}
seen.insert(nearest);
for neighbour in neighbours(nearest, target) {
if !seen.contains(&neighbour) && !heap.contains(&neighbour) {
heap.push(neighbour);
let risk = distance.get(&nearest).unwrap() + map.get(&neighbour).unwrap();
if risk < *distance.get(&neighbour).unwrap_or(&u32::MAX) {
distance.insert(neighbour, risk);
}
}
}
}
*distance.get(&target).unwrap()
}
#[cfg(test)]
mod tests {
use crate::Day;
use crate::day15::Day15;
const INPUT: &str = r"1163751742
1381373672
2136511328
3694931569
7463417111
1319128137
1359912421
3125421639
1293138521
2311944581";
#[test]
fn part1_test() -> anyhow::Result<()> {
let d = Day15::init(INPUT.to_string())?;
assert_eq!("40", d.part1()?);
Ok(())
}
#[test]
fn part2_test() -> anyhow::Result<()> {
let d = Day15::init(INPUT.to_string())?;
assert_eq!("315", d.part2()?);
Ok(())
}
// Woo slow and i again don't feel like fixing it :p
// #[test]
// fn part1_real() -> anyhow::Result<()> {
// let d = Day15::init(crate::load_input("15")?)?;
// assert_eq!("388", d.part1()?);
// Ok(())
// }
//
// #[test]
// fn part2_real() -> anyhow::Result<()> {
// let d = Day15::init(crate::load_input("15")?)?;
// assert_eq!("2819", d.part2()?);
// Ok(())
// }
}

View File

@ -17,6 +17,7 @@ use crate::day11::Day11;
use crate::day12::Day12;
use crate::day13::Day13;
use crate::day14::Day14;
use crate::day15::Day15;
mod day;
mod day01;
@ -33,6 +34,7 @@ mod day11;
mod day12;
mod day13;
mod day14;
mod day15;
fn load_input(day: &str) -> Result<String> {
read_to_string(format!("./input/day{}", day)).map_err(|x| x.into())
@ -81,7 +83,8 @@ fn main() -> anyhow::Result<()> {
Box::new(Day11::init(load_input("11")?)?),
Box::new(Day12::init(load_input("12")?)?),
Box::new(Day13::init(load_input("13")?)?),
Box::new(Day14::init(load_input("14")?)?),];
Box::new(Day14::init(load_input("14")?)?),
Box::new(Day15::init(load_input("15")?)?),];
let _verbosity = matches.occurrences_of("v");
if matches.is_present("all") {