-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
72 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
pub mod part1; | ||
pub mod part2; | ||
mod row; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
fn main() { | ||
let text = include_str!("input.txt"); | ||
println!("{}", day9::part1::solve(text)); | ||
println!("{}", day9::part2::solve(text)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,15 @@ | ||
fn retain_deltas(mut values: Vec<i32>) -> Vec<i32> { | ||
for i in 1..values.len() { | ||
values[i - 1] = values[i] - values[i - 1]; | ||
} | ||
values.pop(); | ||
values | ||
} | ||
|
||
pub fn solve_line(line: &str) -> i32 { | ||
let mut values: Vec<i32> = line | ||
.split(' ') | ||
.map(|s| s.parse().expect("number")) | ||
.collect(); | ||
let mut lasts: Vec<i32> = vec![]; | ||
while !values.iter().all(|&value| value == 0) { | ||
lasts.push(*values.last().expect("loop to end when vec is empty")); | ||
values = retain_deltas(values); | ||
} | ||
lasts.into_iter().sum() | ||
} | ||
use crate::row::Row; | ||
|
||
pub fn solve(text: &str) -> i32 { | ||
text.lines().map(solve_line).sum() | ||
text.lines().map(Row::from_line).map(Row::solve).sum() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
const SAMPLE: &str = include_str!("sample.txt"); | ||
|
||
#[test] | ||
fn solve_line_sample() { | ||
for (line, want) in SAMPLE.lines().zip([18, 28, 68]) { | ||
assert_eq!(solve_line(line), want); | ||
} | ||
} | ||
#[test] | ||
fn sample() { | ||
assert_eq!(solve(SAMPLE), 114); | ||
assert_eq!(solve(include_str!("sample.txt")), 114); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use crate::row::Row; | ||
|
||
pub fn solve(text: &str) -> i32 { | ||
text.lines() | ||
.map(Row::from_line) | ||
.map(Row::backward) | ||
.map(Row::solve) | ||
.sum() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn sample() { | ||
assert_eq!(solve(include_str!("sample.txt")), 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
pub struct Row(Vec<i32>); | ||
|
||
impl Row { | ||
pub fn from_line(line: &str) -> Row { | ||
Row(line | ||
.split(' ') | ||
.map(|s| s.parse().expect("number")) | ||
.collect()) | ||
} | ||
|
||
pub fn backward(mut self) -> Row { | ||
self.0.reverse(); | ||
self | ||
} | ||
|
||
pub fn solve(mut self) -> i32 { | ||
let mut sum = 0; | ||
while !self.0.iter().all(|&value| value == 0) { | ||
for i in 1..self.0.len() { | ||
self.0[i - 1] = self.0[i] - self.0[i - 1]; | ||
} | ||
sum += self.0.pop().expect("loop to end if vec is empty"); | ||
} | ||
sum | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
const SAMPLE: &str = include_str!("sample.txt"); | ||
|
||
#[test] | ||
fn sample() { | ||
for (line, want) in SAMPLE.lines().zip([18, 28, 68]) { | ||
assert_eq!(Row::from_line(line).solve(), want); | ||
} | ||
} | ||
|
||
#[test] | ||
fn sample_backward() { | ||
for (line, want) in SAMPLE.lines().zip([-3, 0, 5]) { | ||
assert_eq!(Row::from_line(line).backward().solve(), want); | ||
} | ||
} | ||
} |