update day9

This commit is contained in:
Andrew Glaze 2023-12-18 11:50:21 -05:00
parent 831d97d5b4
commit 0645f39e51
5 changed files with 86 additions and 85 deletions

7
old/day09/Cargo.lock generated
View File

@ -1,7 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day09"
version = "0.1.0"

View File

@ -1,8 +0,0 @@
[package]
name = "day09"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -1,70 +0,0 @@
use std::fs;
fn main() {
let input = fs::read_to_string("input.txt").unwrap();
let input: Vec<_> = input.split('\n')
.map(|line| {
line.split_whitespace()
.map(|num| num.parse::<i64>().unwrap())
.collect::<Vec<_>>()
}).collect();
let mut total = 0;
for line in input {
let mut line_starters: Vec<i64> = vec![*line.first().unwrap()];
let mut cur_line: Vec<i64> = line;
while !cur_line.iter().all(|x| *x == 0) {
let mut next_line: Vec<i64> = vec![];
for i in 1..cur_line.len() {
let diff = cur_line[i] - cur_line[i-1];
next_line.push(diff)
}
line_starters.push(*next_line.first().unwrap());
cur_line = next_line;
}
let mut sum = 0;
for i in (1..line_starters.len()).rev() {
sum = line_starters[i - 1] - sum;
}
total += sum;
}
println!("{:?}", total)
}
// use std::fs;
// fn main() {
// let input = fs::read_to_string("input.txt").unwrap();
// let input: Vec<_> = input.split('\n')
// .map(|line| {
// line.split_whitespace()
// .map(|num| num.parse::<i64>().unwrap())
// .collect::<Vec<_>>()
// }).collect();
// let mut total = 0;
// for line in input {
// let mut line_enders: Vec<i64> = vec![*line.last().unwrap()];
// let mut cur_line: Vec<i64> = line;
// while !cur_line.iter().all(|x| *x == 0) {
// let mut next_line: Vec<i64> = vec![];
// for i in 1..cur_line.len() {
// let diff = cur_line[i] - cur_line[i-1];
// next_line.push(diff)
// }
// line_enders.push(*next_line.last().unwrap());
// cur_line = next_line;
// }
// let mut sum = 0;
// for i in (1..line_enders.len()).rev() {
// sum = sum + line_enders[i - 1];
// }
// total += sum;
// }
// println!("{:?}", total)
// }

85
src/day9.rs Normal file
View File

@ -0,0 +1,85 @@
use aoc_runner_derive::{aoc, aoc_generator};
#[aoc_generator(day9)]
fn parse(input: &str) -> Vec<Vec<i64>> {
let input: Vec<_> = input.split('\n')
.map(|line| {
line.split_whitespace()
.map(|num| num.parse::<i64>().unwrap())
.collect::<Vec<_>>()
}).collect();
input
}
#[aoc(day9, part1)]
fn part1(input: &Vec<Vec<i64>>) -> i64 {
let mut total = 0;
for line in input {
let mut line_enders: Vec<i64> = vec![*line.last().unwrap()];
let mut cur_line: Vec<i64> = line.to_vec();
while !cur_line.iter().all(|x| *x == 0) {
let mut next_line: Vec<i64> = vec![];
for i in 1..cur_line.len() {
let diff = cur_line[i] - cur_line[i-1];
next_line.push(diff)
}
line_enders.push(*next_line.last().unwrap());
cur_line = next_line;
}
let mut sum = 0;
for i in (1..line_enders.len()).rev() {
sum = sum + line_enders[i - 1];
}
total += sum;
}
total
}
#[aoc(day9, part2)]
fn part2(input: &Vec<Vec<i64>>) -> i64 {
let mut total = 0;
for line in input {
let mut line_starters: Vec<i64> = vec![*line.first().unwrap()];
let mut cur_line: Vec<i64> = line.to_vec();
while !cur_line.iter().all(|x| *x == 0) {
let mut next_line: Vec<i64> = vec![];
for i in 1..cur_line.len() {
let diff = cur_line[i] - cur_line[i-1];
next_line.push(diff)
}
line_starters.push(*next_line.first().unwrap());
cur_line = next_line;
}
let mut sum = 0;
for i in (1..line_starters.len()).rev() {
sum = line_starters[i - 1] - sum;
}
total += sum;
}
total
}
#[cfg(test)]
mod tests {
use super::*;
const EX: &str = r"0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45";
#[test]
fn part1_example() {
assert_eq!(part1(&parse(EX)), 114);
}
#[test]
fn part2_example() {
assert_eq!(part2(&parse(EX)), 2);
}
}

View File

@ -1,3 +1,4 @@
mod day9;
mod day8;
mod day7;
mod day6;