Skip to content

Instantly share code, notes, and snippets.

@dcoles
Created December 1, 2021 19:51
Show Gist options
  • Save dcoles/dc094f950685d2f9edd5480b9271fede to your computer and use it in GitHub Desktop.
Save dcoles/dc094f950685d2f9edd5480b9271fede to your computer and use it in GitHub Desktop.
Advent of Code 2021: Day 1 (Draft vs. Final)
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);
}
/// 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