2017/05
This commit is contained in:
4
2017/05/Cargo.lock
generated
Normal file
4
2017/05/Cargo.lock
generated
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
[[package]]
|
||||||
|
name = "day-05"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
6
2017/05/Cargo.toml
Normal file
6
2017/05/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day-05"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
1092
2017/05/input.txt
Normal file
1092
2017/05/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
87
2017/05/src/main.rs
Normal file
87
2017/05/src/main.rs
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
use std::io::{self, Read};
|
||||||
|
|
||||||
|
|
||||||
|
fn process_one(mut data: Vec<i64>) -> usize {
|
||||||
|
let mut index: i64 = 0;
|
||||||
|
let mut steps = 0;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let addr = data[index as usize];
|
||||||
|
|
||||||
|
data[index as usize] += 1;
|
||||||
|
|
||||||
|
index += addr;
|
||||||
|
steps += 1;
|
||||||
|
|
||||||
|
if index < 0 || index as usize >= data.len() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
steps
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process_two(mut data: Vec<i64>) -> usize {
|
||||||
|
let mut index: i64 = 0;
|
||||||
|
let mut steps = 0;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
if index < 0 || index as usize >= data.len() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
let addr = data[index as usize];
|
||||||
|
|
||||||
|
data[index as usize] += if addr < 3 { 1 } else { -1 };
|
||||||
|
|
||||||
|
index += addr;
|
||||||
|
steps += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
steps
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse(input: &str) -> Vec<i64> {
|
||||||
|
input.lines().filter_map(|line| line.parse().ok()).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut input = String::new();
|
||||||
|
|
||||||
|
io::stdin()
|
||||||
|
.read_to_string(&mut input)
|
||||||
|
.expect("faild to read input");
|
||||||
|
|
||||||
|
let data = parse(&input);
|
||||||
|
|
||||||
|
println!("part_one={}", process_one(data.clone()));
|
||||||
|
println!("part_two={}", process_two(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse() {
|
||||||
|
let input = "0\n\
|
||||||
|
3\n\
|
||||||
|
0\n\
|
||||||
|
1\n\
|
||||||
|
-3";
|
||||||
|
|
||||||
|
assert_eq!(parse(input), vec![0, 3, 0, 1, -3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_process_one() {
|
||||||
|
assert_eq!(process_one(vec![0, 3, 0, 1, -3]), 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_process_two() {
|
||||||
|
assert_eq!(process_two(vec![0, 3, 0, 1, -3]), 10);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user