This commit is contained in:
Smaug123
2021-05-09 11:21:43 +01:00
parent bd3443068a
commit 6537f253e0
6 changed files with 1186 additions and 2 deletions

9
day_5/Cargo.toml Normal file
View File

@@ -0,0 +1,9 @@
[package]
name = "day_5"
version = "0.1.0"
authors = ["Smaug123 <patrick+github@patrickstevens.co.uk>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

1092
day_5/input.txt Normal file

File diff suppressed because it is too large Load Diff

78
day_5/src/main.rs Normal file
View File

@@ -0,0 +1,78 @@
use std::convert::TryFrom;
fn input() -> Vec<i32> {
let input = include_str!("../input.txt");
input
.lines()
.map(|l| {
l.parse()
.unwrap_or_else(|_| panic!("{} wasn't a valid u32", l))
})
.collect::<Vec<i32>>()
}
pub fn part_1(v: &mut Vec<i32>) -> u32 {
let mut count = 0;
let mut index: usize = 0;
while index < v.len() {
let bounce = v[index];
v[index] = bounce + 1;
let test = i32::try_from(index).unwrap() + bounce;
if test < 0 {
return count;
}
index = usize::try_from(test).unwrap();
count += 1;
}
count
}
pub fn part_2(v: &mut Vec<i32>) -> u32 {
let mut count = 0;
let mut index: usize = 0;
while index < v.len() {
let bounce = v[index];
if bounce >= 3 {
v[index] = bounce - 1;
} else {
v[index] = bounce + 1;
}
let test = i32::try_from(index).unwrap() + bounce;
if test < 0 {
return count;
}
index = usize::try_from(test).unwrap();
count += 1;
}
count
}
fn main() {
let input = input();
println!("part 1 => {}", part_1(&mut input.clone()));
let mut input = input;
println!("part 2 => {}", part_2(&mut input));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn part1_known() {
assert_eq!(part_1(&mut vec![0, 3, 0, 1, -3]), 5);
}
#[test]
fn part2_known() {
assert_eq!(part_2(&mut vec![0, 3, 0, 1, -3]), 10);
}
#[test]
fn test_day_5() {
let input = input();
assert_eq!(part_1(&mut input.clone()), 391540);
let mut input = input;
assert_eq!(part_2(&mut input), 30513679);
}
}