Skip to content

Commit

Permalink
Year 2018: Day 08
Browse files Browse the repository at this point in the history
  • Loading branch information
joshleaves committed Dec 20, 2024
1 parent 60c362e commit 20b35db
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 4 deletions.
10 changes: 8 additions & 2 deletions .vscode/advent-rs.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@
"scope": "rust",
"prefix": "create_day_",
"body": [
"pub fn ${2:day_number}_v1(input: impl Into<String>) -> ${1:return_type} { }",
"pub fn ${2:day_number}_v2(input: impl Into<String>) -> ${1:return_type} { }",
"pub fn ${2:day_number}_v1(input: impl Into<String>) -> ${1:return_type} {",
" unimplemented!()",
"}",
"",
"pub fn ${2:day_number}_v2(input: impl Into<String>) -> ${1:return_type} {",
" unimplemented!()",
"}",
"",
"solvable!(${2:day_number}, ${2:day_number}_v1, ${2:day_number}_v2, ${1:return_type});",
"",
"#[cfg(test)]",
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ Of note:
- The changelog 2015.5.2 has been rewritten from each commit content.
- This file may be amended entirely in the future to adhere to the [GNU Changelog style](https://www.gnu.org/prep/standards/html_node/Style-of-Change-Logs.html#Style-of-Change-Logs)

## [2018.8.1]
### Added
- Solved [exercice for 2018, day 08](src/year_2018/08.rs).
### Changed
- Added `unimplemented!()` to VSCode snippets.

## [2018.7.1]
### Added
- Solved [exercice for 2018, day 07](src/year_2018/07.rs).
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[package]
name = "advent-rs"
version = "2018.7.1"
version = "2018.8.1"
edition = "2021"
authors = ["Arnaud 'red' Rouyer"]
readme = "README.md"
Expand Down
6 changes: 6 additions & 0 deletions NOTES_2018.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ At first, I felt like expanding each area one by one, until I realised that just
# Day 07: The Sum of Its Parts

I hate everything about time-advancing series calculations...

# Day 08: Memory Maneuver

Trees are one of those things I never know how to handle properly, traverse, balance, etc... I just love good old arrays.

In that case however, despite my first five minutes being puzzled by the absence of a "length" value for each node of the tree, I quickly found out a solution and actually liked the exercise.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ I'm also adding notes that may be useful if you're (like me) discovering Rust:
- [2015, complete!](NOTES_2015.md)
- [2016, complete!](NOTES_2016.md)
- [2017, complete!](NOTES_2017.md)
- [2018, up to day 07](NOTES_2018.md)
- [2018, up to day 08](NOTES_2018.md)

# Regarding style rules
I'm gonna use a mix of what `cargo fmt` does, with some stuff that feels more natural to me.
Expand Down
1 change: 1 addition & 0 deletions inputs/year_2018/day_08_input

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/year_2018.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod day_04;
pub mod day_05;
pub mod day_06;
pub mod day_07;
pub mod day_08;

pub fn solve(day: u8, part: u8, input: impl Into<String>) -> Option<String> {
if part > 2 {
Expand All @@ -23,6 +24,7 @@ pub fn solve(day: u8, part: u8, input: impl Into<String>) -> Option<String> {
5 => Some(day_05::day_05(part, input).to_string()),
6 => Some(day_06::day_06(part, input).to_string()),
7 => Some(day_07::day_07(part, input).to_string()),
8 => Some(day_08::day_08(part, input).to_string()),
_ => None,
}
}
Expand Down Expand Up @@ -79,4 +81,11 @@ mod tests {
assert_eq!(day_07::day_07_v1(input), "BCEFLDMQTXHZGKIASVJYORPUWN");
assert_eq!(day_07::day_07_v2(input), "987");
}

#[test]
fn day_08() {
let input = include_str!("../inputs/year_2018/day_08_input");
assert_eq!(day_08::day_08_v1(input), 41_849);
assert_eq!(day_08::day_08_v2(input), 32_487);
}
}
107 changes: 107 additions & 0 deletions src/year_2018/day_08.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#[derive(Default)]
struct ParseCounter {
input_values: Vec<u8>,
counter: usize,
}

impl ParseCounter {
fn new(input: &str) -> Self {
let values = input
.split_whitespace()
.map(|f| f.parse::<u8>().unwrap())
.collect();
ParseCounter {
input_values: values,
counter: 0,
}
}

fn next(&mut self) -> Option<u8> {
let retval = self.input_values.get(self.counter);
self.counter += 1;
retval.copied()
}
}

#[derive(Debug)]
struct Node {
child_nodes: Vec<Node>,
metadatas: Vec<u8>,
}

impl Node {
fn new(pc: &mut ParseCounter) -> Self {
let child_nodes_size = pc.next().expect("- Child nodes size");
let metadatas_size = pc.next().expect("- Metadatas size");

let mut child_nodes = Vec::<Node>::new();
for _ in 0..child_nodes_size {
child_nodes.push(Node::new(pc));
}

let mut metadatas = Vec::<u8>::new();
for _ in 0..metadatas_size {
metadatas.push(pc.next().expect("Should exist"));
}

Node {
child_nodes,
metadatas,
}
}

fn metadatas_v1(self) -> u32 {
let mut metadatas_total = 0;
for child_node in self.child_nodes {
metadatas_total += child_node.metadatas_v1();
}
for metadata in self.metadatas {
metadatas_total += metadata as u32;
}
metadatas_total
}

fn metadatas_v2(&self) -> u32 {
if self.child_nodes.is_empty() {
return self.metadatas.iter().fold(0u32, |acc, x| acc + *x as u32);
}
let mut metadatas_total = 0;
for metadata in self.metadatas.clone().into_iter() {
if let Some(child_node) = self.child_nodes.get(metadata as usize - 1) {
metadatas_total += child_node.metadatas_v2();
}
}
metadatas_total
}
}

pub fn day_08_v1(input: impl Into<String>) -> u32 {
let mut parser = ParseCounter::new(&input.into());
let root_node = Node::new(&mut parser);
root_node.metadatas_v1()
}

pub fn day_08_v2(input: impl Into<String>) -> u32 {
let mut parser = ParseCounter::new(&input.into());
let root_node = Node::new(&mut parser);
root_node.metadatas_v2()
}

solvable!(day_08, day_08_v1, day_08_v2, u32);

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

const SAMPLE: &str = "2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2";

#[test]
fn works_with_samples_v1() {
assert_eq!(day_08_v1(SAMPLE), 138);
}

#[test]
fn works_with_samples_v2() {
assert_eq!(day_08_v2(SAMPLE), 66);
}
}

0 comments on commit 20b35db

Please sign in to comment.