Created
December 1, 2021 19:51
-
-
Save dcoles/dc094f950685d2f9edd5480b9271fede to your computer and use it in GitHub Desktop.
Advent of Code 2021: Day 1 (Draft vs. Final)
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
fn main() { | |
let input = read_input_from_file("day01/input.txt").unwrap(); | |
// Part 1 | |
let mut count = 0; | |
let mut last = -1; | |
for &n in &input { | |
if n > last { | |
count += 1; | |
} | |
last = n; | |
} | |
// NOTE: Subtract 1 from this!!! | |
println!("Part 1: {}", count); | |
} |
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
/// Advent of Code 2021: Day 1 | |
/// https://adventofcode.com/2021/day/1 | |
fn main() -> anyhow::Result<()> { | |
let input = read_input_from_file("day01/input.txt")?; | |
// Part 1 | |
println!("Part 1: {}", part1(&input)); | |
} | |
fn part1(input: &[i32]) -> usize { | |
input.windows(2) | |
.filter(|w| w[1] > w[0]) | |
.count() | |
} | |
#[cfg(test)] | |
mod test { | |
use super::*; | |
static INPUT: [i32; 10] = [199, 200, 208, 210, 200, 207, 240, 269, 260, 263]; | |
#[test] | |
fn test_part1() { | |
assert_eq!(part1(&INPUT), 7); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment