Skip to content

Solved 2024-09* #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions 2024/09a/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "advent-of-code-2024-09a"
version = "0.1.0"
edition.workspace = true
rust-version.workspace = true
description = "Solution to Advent of Code 2024, problem 09a"
authors.workspace = true
repository.workspace = true
license.workspace = true
publish.workspace = true

[dependencies]
adventutil = { path = "../../adventutil" }

[lints]
workspace = true
54 changes: 54 additions & 0 deletions 2024/09a/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use adventutil::Input;
use std::collections::VecDeque;

fn solve(input: Input) -> u64 {
// List of Option<File ID> values
let mut blocks = VecDeque::new();
let s = input.read();
let mut is_file = true;
let mut i = 0u64;
for d in s.trim().chars() {
let size = usize::try_from(d.to_digit(10).unwrap()).unwrap();
if is_file {
blocks.extend(std::iter::repeat_n(Some(i), size));
} else {
blocks.extend(std::iter::repeat_n(None, size));
i += 1;
}
is_file = !is_file;
}
let mut checksum = 0;
let mut pos = 0u64;
'outer: while let Some(blk) = blocks.pop_front() {
if let Some(fid) = blk {
checksum += pos * fid;
} else {
let fid = loop {
let Some(blk) = blocks.pop_back() else {
break 'outer;

Check warning on line 28 in 2024/09a/src/main.rs

View check run for this annotation

Codecov / codecov/patch

2024/09a/src/main.rs#L28

Added line #L28 was not covered by tests
};
if let Some(fid) = blk {
break fid;
}
};
checksum += pos * fid;
}
pos += 1;
}
checksum
}

fn main() {
println!("{}", solve(Input::from_env()));
}

Check warning on line 43 in 2024/09a/src/main.rs

View check run for this annotation

Codecov / codecov/patch

2024/09a/src/main.rs#L41-L43

Added lines #L41 - L43 were not covered by tests

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_example() {
let input = Input::from("2333133121414131402");
assert_eq!(solve(input), 1928);
}
}
16 changes: 16 additions & 0 deletions 2024/09b/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "advent-of-code-2024-09b"
version = "0.1.0"
edition.workspace = true
rust-version.workspace = true
description = "Solution to Advent of Code 2024, problem 09a"
authors.workspace = true
repository.workspace = true
license.workspace = true
publish.workspace = true

[dependencies]
adventutil = { path = "../../adventutil" }

[lints]
workspace = true
104 changes: 104 additions & 0 deletions 2024/09b/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use adventutil::Input;
use std::collections::VecDeque;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct File {
position: u64,
id: u64,
size: u64,
}

impl File {
fn with_position(mut self, pos: u64) -> File {
self.position = pos;
self
}

fn checksum(&self) -> u64 {
(self.position..)
.map(|p| p * self.id)
.take(usize::try_from(self.size).unwrap())
.sum()
}
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct Free {
position: u64,
size: u64,
}

impl Free {
fn reduce(&mut self, size: u64) {
self.position += size;
self.size -= size;
}
}

fn solve(input: Input) -> u64 {
let mut files = VecDeque::new();
let mut free_space = Vec::new();
let s = input.read();
let mut is_file = true;
let mut file_id = 0u64;
let mut position = 0u64;
for d in s.trim().chars() {
let size = u64::from(d.to_digit(10).unwrap());
if is_file {
files.push_back(File {
position,
id: file_id,
size,
});
} else {
free_space.push(Free { position, size });
file_id += 1;
}
is_file = !is_file;
position += size;
}
let mut checksum = 0;
if let Some(f) = files.pop_front() {
checksum += f.checksum();
}
while let Some(f) = files.pop_back() {
let mut moved = false;
let mut del_index = None;
for (j, free) in free_space.iter_mut().enumerate() {
if free.position >= f.position {
break;
}
if free.size >= f.size {
checksum += f.with_position(free.position).checksum();
free.reduce(f.size);
if free.size == 0 {
del_index = Some(j);
}
moved = true;
break;
}
}
if !moved {
checksum += f.checksum();
}
if let Some(j) = del_index {
free_space.remove(j);
}
}
checksum
}

fn main() {
println!("{}", solve(Input::from_env()));
}

Check warning on line 93 in 2024/09b/src/main.rs

View check run for this annotation

Codecov / codecov/patch

2024/09b/src/main.rs#L91-L93

Added lines #L91 - L93 were not covered by tests

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_example() {
let input = Input::from("2333133121414131402");
assert_eq!(solve(input), 2858);
}
}
Binary file modified 2024/answers.csv
Binary file not shown.
Binary file added 2024/inputs/09.txt
Binary file not shown.
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.