update day9
This commit is contained in:
parent
831d97d5b4
commit
0645f39e51
7
old/day09/Cargo.lock
generated
7
old/day09/Cargo.lock
generated
@ -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"
|
@ -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]
|
@ -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
85
src/day9.rs
Normal 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);
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
mod day9;
|
||||
mod day8;
|
||||
mod day7;
|
||||
mod day6;
|
||||
|
Loading…
Reference in New Issue
Block a user