Day 1 solution in Rust
I have no idea what I'm doing in Rust, and I definitely don't understand borrowing yet, so this is a hideous solution on multiple fronts. I'm not sure that I'll be sticking with Rust as AoC doesn't feel like a conducive environment to learn it in.
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
45
.vscode/launch.json
vendored
Normal file
45
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,45 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug executable 'aoc2021'",
|
||||
"cargo": {
|
||||
"args": [
|
||||
"build",
|
||||
"--bin=aoc2021",
|
||||
"--package=aoc2021"
|
||||
],
|
||||
"filter": {
|
||||
"name": "aoc2021",
|
||||
"kind": "bin"
|
||||
}
|
||||
},
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}"
|
||||
},
|
||||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug unit tests in executable 'aoc2021'",
|
||||
"cargo": {
|
||||
"args": [
|
||||
"test",
|
||||
"--no-run",
|
||||
"--bin=aoc2021",
|
||||
"--package=aoc2021"
|
||||
],
|
||||
"filter": {
|
||||
"name": "aoc2021",
|
||||
"kind": "bin"
|
||||
}
|
||||
},
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}"
|
||||
}
|
||||
]
|
||||
}
|
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aoc2021"
|
||||
version = "0.1.0"
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "aoc2021"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
2000
inputs/01.txt
Normal file
2000
inputs/01.txt
Normal file
File diff suppressed because it is too large
Load Diff
63
src/main.rs
Normal file
63
src/main.rs
Normal file
@ -0,0 +1,63 @@
|
||||
use std::io::{self, BufRead};
|
||||
use std::fs::File;
|
||||
use std::path::Path;
|
||||
|
||||
fn main() {
|
||||
if let Ok(lines) = read_lines("inputs/01.txt") {
|
||||
part1(lines);
|
||||
}
|
||||
if let Ok(lines) = read_lines("inputs/01.txt") {
|
||||
part2(lines);
|
||||
}
|
||||
}
|
||||
|
||||
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||
where P: AsRef<Path>, {
|
||||
let file = File::open(filename)?;
|
||||
Ok(io::BufReader::new(file).lines())
|
||||
}
|
||||
|
||||
fn part1(lines: io::Lines<io::BufReader<File>>) {
|
||||
let mut last_depth: i64 = 0;
|
||||
let mut num_increased = -1;
|
||||
|
||||
for line in lines {
|
||||
if let Ok(depth) = line {
|
||||
let di: i64 = depth.parse().unwrap();
|
||||
if di > last_depth {
|
||||
num_increased = num_increased + 1;
|
||||
}
|
||||
last_depth = di;
|
||||
}
|
||||
}
|
||||
|
||||
println!("part1: increased: {:?}", num_increased);
|
||||
}
|
||||
|
||||
fn part2(lines: io::Lines<io::BufReader<File>>) {
|
||||
let mut last_total: i64 = 0;
|
||||
let mut num_increased = -1;
|
||||
let mut num1;
|
||||
let mut num2 = -1;
|
||||
let mut num3 = -1;
|
||||
|
||||
for line in lines {
|
||||
if let Ok(depth) = line {
|
||||
let di: i64 = depth.parse().unwrap();
|
||||
num1 = num2;
|
||||
num2 = num3;
|
||||
num3 = di;
|
||||
if num1 < 0 || num2 < 0 || num3 < 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
let total = num1 + num2 + num3;
|
||||
if total > last_total {
|
||||
num_increased = num_increased + 1;
|
||||
}
|
||||
last_total = total;
|
||||
}
|
||||
}
|
||||
|
||||
println!("part2: increased: {:?}", num_increased);
|
||||
}
|
Reference in New Issue
Block a user