-
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.
- Loading branch information
Showing
4 changed files
with
136 additions
and
9 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,6 +1,20 @@ | ||
# http://adventofcode.com/2017 | ||
|
||
[workspace] | ||
members = ["day1", "day2", "day8", "day10", "day11", "day13", "day14", "day15", "day16", "day17", "day18", "day23"] | ||
members = [ | ||
"day1", | ||
"day2", | ||
"day5", | ||
"day8", | ||
"day10", | ||
"day11", | ||
"day13", | ||
"day14", | ||
"day15", | ||
"day16", | ||
"day17", | ||
"day18", | ||
"day23", | ||
] | ||
|
||
resolver = "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,11 @@ | ||
[package] | ||
name = "day5" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
aoc = { path = "../../aoc" } | ||
|
||
[[bin]] | ||
name = "day5" | ||
path = "day5.rs" |
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,90 @@ | ||
//! [Day 5: A Maze of Twisty Trampolines, All Alike](https://adventofcode.com/2017/day/5) | ||
struct Puzzle { | ||
jumps: Vec<i32>, | ||
} | ||
|
||
impl Puzzle { | ||
fn new() -> Puzzle { | ||
Puzzle { jumps: vec![] } | ||
} | ||
|
||
/// Get the puzzle input. | ||
fn configure(&mut self, path: &str) { | ||
let data = std::fs::read_to_string(path).unwrap(); | ||
|
||
self.jumps = data.lines().map(|s| s.parse().unwrap()).collect(); | ||
} | ||
|
||
/// Solve part one. | ||
fn part1(&self) -> u32 { | ||
let mut jumps = self.jumps.clone(); | ||
let length = i32::try_from(jumps.len()).unwrap(); | ||
let mut offset = 0; | ||
let mut n = 0; | ||
|
||
while 0 <= offset && offset < length { | ||
let index = usize::try_from(offset).unwrap(); | ||
|
||
let jump = jumps[index]; | ||
jumps[index] += 1; | ||
|
||
offset += jump; | ||
n += 1; | ||
} | ||
|
||
n | ||
} | ||
|
||
/// Solve part two. | ||
fn part2(&self) -> u32 { | ||
let mut jumps = self.jumps.clone(); | ||
let length = i32::try_from(jumps.len()).unwrap(); | ||
let mut offset = 0; | ||
let mut n = 0; | ||
|
||
while 0 <= offset && offset < length { | ||
let index = usize::try_from(offset).unwrap(); | ||
|
||
let jump = jumps[index]; | ||
if jump >= 3 { | ||
jumps[index] -= 1; | ||
} else { | ||
jumps[index] += 1; | ||
} | ||
|
||
offset += jump; | ||
n += 1; | ||
} | ||
|
||
n | ||
} | ||
} | ||
|
||
fn main() { | ||
let args = aoc::parse_args(); | ||
let mut puzzle = Puzzle::new(); | ||
puzzle.configure(args.path.as_str()); | ||
println!("{}", puzzle.part1()); | ||
println!("{}", puzzle.part2()); | ||
} | ||
|
||
/// Test from puzzle input | ||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test01() { | ||
let mut puzzle = Puzzle::new(); | ||
puzzle.configure("test.txt"); | ||
assert_eq!(puzzle.part1(), 5); | ||
} | ||
|
||
#[test] | ||
fn test02() { | ||
let mut puzzle = Puzzle::new(); | ||
puzzle.configure("test.txt"); | ||
assert_eq!(puzzle.part2(), 10); | ||
} | ||
} |
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