Adds day 9
This commit is contained in:
1 parent
bc0aa70ac3
commit
8c116cb59a
4 files changed
+156
-1
No files matched your search
@@ -0,0 +1 @@
|
||||
9953877292941648772616264775767428658883838268139271603033278826197512792138493841982683804757568110199215215571317977709849759733243569153182826948453096391192839170636590177283483639322341355356571986364617949975397540918247773979215998528235644578989248914549383222856258361161819260156064179996391961266912604423901066942216914217854276303630422886332436181925374685244140386687415960519939986071571752695323769824328476131628683469846953449356965554115590163216555863101814371821622059234754412952107359623159457288984076219639102672923788441086209687475033947873589869404644549648288825655198836028588849758065833038712860453278561629128194451431258781766519411112346859714318866328287132359827888716365493428476151089579915842478564939451742488886252065218125523183159190857623387862114038404875807456711075467031177026179750205411979310986675644388976287333467536853431970802022963588477642723084159186887657821874314618571866853797513937864514587980968044956444686467732298472170894497766918323313989886969526469633529546661027279823624833113451138628707972506178886396324927519720802441301629508356486017323180388623763989948880162984381152646523226343867258512950529356692920347519284216878459724924704970186844211512936492958969145515253550988722274195372951367390245879138326936518286787828968198545239675439493148123988429237919414017567595294484665559448842391210657615181447494553523776446532267054817592458089155662846972145085738154837254704780255233265839539332494366266516902612606710195278588341413778857094184455522295619322905549774623698189699719511648125899108615882115342478737810709114741866896212249015397547621569226132706216304760808527241479861767228540931024172157842126458576776380683512211257649443707337273380648643107092704128803048729763582791679854826974436392553365598596857637863553273786221684145827145730238920421773943241333959406553673780988853669665632969609245884147325675741263837125399283621726149153411123642513793771858546857241277844831758533096187329191192361899778775713766998925943964184060264161763929733129582060575776484674601481529293815518873595751299526753664798962691879371805960298650334921172837319162182739246199967977214958347582443988559040643576693613983710696599846618146310779935379427431572672232268710655256633119349722255254203244955318837479425818595058883159522172125123477046519623366023145950406420354021749856829987964757726770415929142616243825957194497512509467596299714567534620735696408398914927186575213628658975237577677626323098658718648480865153104585133043937441179934613447178932735183479837326461991514446224691285712816505421541188864390611231927199591053914773621381565619193490195121385563574974937319557723857518526440808767948253626897581294468744535957758397546675114268117396104199435561738635794559332173994456165773288285741399281892454219551981493991442413833474294486626217544268366367187026302572665585866062523091628551188032541345842830979462141866933637901250962086336185168431312716258429489520567518252710745963708049963539775895149137364332434462726826231991396378361974721851949847672075589859766537584973547169256840567516434560894380499411878764195355193035302887129050241641201250961293975638785637187254574782124164129680894281469996816781699791178788244086854725936594812845931719195684974841658662601927629977874565221515386218324555108186774423385493241557644638839648745875638947398840123565984453571979909757926026289488665321107437543533396842773526625812213616892173728395363984803489224172528070385657677840357753157114318886445125641565586266477412135619594119137841696238254810805831352342848580527035329868161013883669199278821722521150934216691484199172157412282530101185386123414891593078398445927193841635702990427485337758105257733624627347669857817741308281681138852620595044127944544789191465553956964159491666471443563074428589697914352241831984786723241080278698647317928130309765124361472562874661222045471149487043475720541326123514815943282820348967742558178412412414135353178865331357643149844765803666617637783611704835558719607962508717646147752059281064953822469035371530632815485687555634191623366675503964863051775824444147585169378779551097444690316481411899565016451780724170808454921319401678696177347666251347934156702386771942763348868028172776348376221253179521337346113247956732527952655817561927434210448881992966445210673859576794468665191678689247808451405275545236352187538233843619665694706337239238629011374993369793569671716258663085762456827453912170611066353248999297411819916182797156237051184841992592339435212385474955625835921165998283901542687939615294923238201528233713886046981860586775496659582487675458216668363267653677537891276083451310652735394592355427138739756320976992291532507217847550451795661114242814384594891669237013966088185015482234495358866928591321924652371181186059891815317081896037791581977716361599558822304391387735483664237887341241354720476735327889843575636129711374991549899215849997423991464614514032494340901Line truncated
|
||||
@@ -0,0 +1,149 @@
|
||||
use core::fmt;
|
||||
use std::path::Path;
|
||||
|
||||
use advent_derive::advent_day;
|
||||
|
||||
use crate::{error::AdventError, input::get_input};
|
||||
use super::*;
|
||||
|
||||
#[derive(PartialEq, Copy, Clone)]
|
||||
enum Sector {
|
||||
Empty,
|
||||
Chunk(u32)
|
||||
}
|
||||
|
||||
impl fmt::Debug for Sector {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Empty => write!(f, "(.)"),
|
||||
Self::Chunk(arg0) => write!(f, "({})", arg0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Part1{
|
||||
drive: Vec<Sector>
|
||||
}
|
||||
type Part2 = Part1;
|
||||
|
||||
impl Part1 {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
drive: vec![]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn read_input_part1(part1: &mut Part1, path: &Path) -> Result<(), AdventError> {
|
||||
let line = get_input(path);
|
||||
let mut reading_file = true;
|
||||
let mut current_file_id = 0;
|
||||
for c in line.chars() {
|
||||
let i = c.to_digit(10).unwrap() as u8;
|
||||
if reading_file {
|
||||
for _ in 0..i {
|
||||
part1.drive.push(Sector::Chunk(current_file_id))
|
||||
}
|
||||
current_file_id += 1;
|
||||
} else {
|
||||
for _ in 0..i {
|
||||
part1.drive.push(Sector::Empty);
|
||||
}
|
||||
}
|
||||
reading_file = !reading_file
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn checksum(drive: &Vec<Sector>) -> usize {
|
||||
drive.iter().enumerate().map(|(i, sector)| {
|
||||
match sector {
|
||||
Sector::Empty => 0,
|
||||
Sector::Chunk(f) => i * (*f as usize),
|
||||
}
|
||||
}).sum()
|
||||
}
|
||||
|
||||
fn solve_part1(part1: &mut Part1) -> Result<String, AdventError> {
|
||||
let mut empty_index = 0;
|
||||
let mut rev_file_index = 0;
|
||||
loop {
|
||||
empty_index += part1.drive.iter().skip(empty_index).position(|s| { *s == Sector::Empty}).unwrap();
|
||||
rev_file_index += part1.drive.iter().rev().skip(rev_file_index).position(|s| { *s != Sector::Empty}).unwrap();
|
||||
let file_index = part1.drive.len() - 1 - rev_file_index;
|
||||
|
||||
if empty_index > file_index {
|
||||
break;
|
||||
}
|
||||
|
||||
part1.drive[empty_index] = part1.drive[file_index];
|
||||
part1.drive[file_index] = Sector::Empty;
|
||||
}
|
||||
|
||||
let checksum: usize = checksum(&part1.drive);
|
||||
Ok(checksum.to_string())
|
||||
}
|
||||
|
||||
fn read_input_part2(part2: &mut Part2, path: &Path) -> Result<(), AdventError> {
|
||||
read_input_part1(part2, path)
|
||||
}
|
||||
|
||||
fn highest_file_id(part2: &Part2) -> u32 {
|
||||
match part2.drive.iter().rev().find(|s| **s != Sector::Empty).unwrap() {
|
||||
Sector::Empty => panic!("Should not happen"),
|
||||
Sector::Chunk(f) => *f,
|
||||
}
|
||||
}
|
||||
|
||||
fn find_pos_and_len(part2: &Part2, id: u32) -> (usize, usize) {
|
||||
let pos = part2.drive.iter().position(|s| match s {
|
||||
Sector::Empty => false,
|
||||
Sector::Chunk(cid) => *cid == id,
|
||||
}).unwrap();
|
||||
|
||||
for j in pos..part2.drive.len() {
|
||||
if part2.drive[j] != Sector::Chunk(id){
|
||||
return (pos, j - pos)
|
||||
}
|
||||
}
|
||||
return (pos, part2.drive.len() - pos)
|
||||
}
|
||||
|
||||
fn find_empty_slice(part2: &Part2, len: usize) -> usize {
|
||||
let mut pos = 0;
|
||||
loop {
|
||||
pos += part2.drive.iter().skip(pos).position(|s| *s == Sector::Empty).unwrap();
|
||||
if pos + len >= part2.drive.len() {
|
||||
return part2.drive.len();
|
||||
}
|
||||
if part2.drive[pos..pos+len].iter().all(|s| *s == Sector::Empty) {
|
||||
return pos;
|
||||
}
|
||||
pos += 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn solve_part2(part2: &mut Part2) -> Result<String, AdventError> {
|
||||
let highest_file_id = highest_file_id(part2);
|
||||
for i in (1..highest_file_id + 1).rev() {
|
||||
let (pos, len) = find_pos_and_len(part2, i);
|
||||
let empty_pos = find_empty_slice(part2, len);
|
||||
|
||||
if empty_pos > pos {
|
||||
continue;
|
||||
}
|
||||
for j in empty_pos..empty_pos+len {
|
||||
part2.drive[j] = Sector::Chunk(i);
|
||||
}
|
||||
for j in pos..pos+len {
|
||||
part2.drive[j] = Sector::Empty;
|
||||
}
|
||||
}
|
||||
|
||||
let checksum: usize = checksum(&part2.drive);
|
||||
Ok(checksum.to_string())
|
||||
}
|
||||
|
||||
#[advent_day(Part1, Part2)]
|
||||
struct Day9;
|
||||
@@ -23,13 +23,17 @@ pub mod day5;
|
||||
pub mod day6;
|
||||
pub mod day7;
|
||||
pub mod day8;
|
||||
pub mod day9;
|
||||
|
||||
pub mod day14;
|
||||
|
||||
/*
|
||||
use std::path::Path;
|
||||
|
||||
use advent_derive::advent_day;
|
||||
|
||||
use crate::error::AdventError;
|
||||
use super::*;
|
||||
|
||||
|
||||
struct Part1{}
|
||||
|
||||
+2
-1
@@ -12,6 +12,7 @@ use day5::Day5;
|
||||
use day6::Day6;
|
||||
use day7::Day7;
|
||||
use day8::Day8;
|
||||
use day9::Day9;
|
||||
use day14::Day14;
|
||||
|
||||
|
||||
@@ -49,7 +50,7 @@ fn main() -> Result<(), AdventError> {
|
||||
Box::new(Day6::new()),
|
||||
Box::new(Day7::new()),
|
||||
Box::new(Day8::new()),
|
||||
Box::new(DummyDay::new()),
|
||||
Box::new(Day9::new()),
|
||||
Box::new(DummyDay::new()),
|
||||
Box::new(DummyDay::new()),
|
||||
Box::new(DummyDay::new()),
|
||||
|
||||
Reference in new issue
Block a user