02-dive
This commit is contained in:
parent
658f3bfca5
commit
f897bd29e3
8 changed files with 2107 additions and 25 deletions
|
@ -1,7 +1,7 @@
|
|||
#![allow(dead_code, unused_imports)]
|
||||
use std::fs::File;
|
||||
use std::path::Path;
|
||||
use std::io::{self, BufRead};
|
||||
use std::path::Path;
|
||||
fn main() {
|
||||
let mut h = 0;
|
||||
let mut v = 0;
|
||||
|
@ -15,7 +15,10 @@ fn main() {
|
|||
let cmd = val[0];
|
||||
let n = val[1].parse::<u32>().unwrap();
|
||||
match cmd {
|
||||
"forward" => {h += n; v += n * aim },
|
||||
"forward" => {
|
||||
h += n;
|
||||
v += n * aim
|
||||
}
|
||||
"down" => aim += n,
|
||||
"up" => aim -= n,
|
||||
_ => println!("not a command"),
|
||||
|
@ -23,10 +26,12 @@ fn main() {
|
|||
}
|
||||
}
|
||||
}
|
||||
println!("h: {}, v: {}, final position: {}", h, v, h * v );
|
||||
println!("h: {}, v: {}, final position: {}", h, v, h * v);
|
||||
}
|
||||
|
||||
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>> where P: AsRef<Path>,
|
||||
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())
|
||||
|
|
1000
2021/data/02.directions
Normal file
1000
2021/data/02.directions
Normal file
File diff suppressed because it is too large
Load diff
1000
2021/data/03.diagnostics
Normal file
1000
2021/data/03.diagnostics
Normal file
File diff suppressed because it is too large
Load diff
|
@ -1,11 +1,8 @@
|
|||
#![allow(dead_code, unused_imports)]
|
||||
use anyhow::Result;
|
||||
use itertools::Itertools;
|
||||
|
||||
use std::{io::{stdin, Read}, str::FromStr};
|
||||
|
||||
fn descending_window(size: usize) -> Result<usize> {
|
||||
Ok(aoc::read_one_per_line::<u32>("./data/1.depths")?
|
||||
Ok(aoc::read_one_per_line::<u32>("./data/01.depths")?
|
||||
.windows(size)
|
||||
.filter(|win| win[0] < win[size - 1])
|
||||
.collect_vec()
|
||||
|
|
77
2021/src/bin/02-dive.rs
Normal file
77
2021/src/bin/02-dive.rs
Normal file
|
@ -0,0 +1,77 @@
|
|||
use anyhow::Result;
|
||||
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Direction {
|
||||
Forward(u64),
|
||||
Down(u64),
|
||||
Up(u64),
|
||||
}
|
||||
|
||||
impl FromStr for Direction {
|
||||
type Err = anyhow::Error;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if let Some((direction, distance)) = s.split_once(" ") {
|
||||
let distance = distance.parse()?;
|
||||
|
||||
Ok(match direction {
|
||||
"forward" => Direction::Forward(distance),
|
||||
"down" => Direction::Down(distance),
|
||||
"up" => Direction::Up(distance),
|
||||
_ => panic!("not a command"),
|
||||
})
|
||||
} else {
|
||||
Err(anyhow::format_err!("could not split direction"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn part_1() -> Result<u64> {
|
||||
let cmds = aoc::read_one_per_line::<Direction>("./data/02.directions")?;
|
||||
// horizontal, depth
|
||||
let loc = cmds.iter().fold((0, 0), |mut loc, dir| match dir {
|
||||
Direction::Forward(dis) => {
|
||||
loc.0 += dis;
|
||||
loc
|
||||
}
|
||||
Direction::Down(dis) => {
|
||||
loc.1 += dis;
|
||||
loc
|
||||
}
|
||||
Direction::Up(dis) => {
|
||||
loc.1 -= dis;
|
||||
loc
|
||||
}
|
||||
});
|
||||
Ok(loc.0 * loc.1)
|
||||
}
|
||||
|
||||
fn part_2() -> Result<u64> {
|
||||
let cmds = aoc::read_one_per_line::<Direction>("./data/02.directions")?;
|
||||
// horizontal, depth, aim
|
||||
let loc = cmds.iter().fold((0, 0, 0), |mut loc, dir| match dir {
|
||||
Direction::Forward(dis) => {
|
||||
loc.0 += dis;
|
||||
loc.1 += dis * loc.2;
|
||||
loc
|
||||
}
|
||||
Direction::Down(dis) => {
|
||||
loc.2 += dis;
|
||||
loc
|
||||
}
|
||||
Direction::Up(dis) => {
|
||||
loc.2 -= dis;
|
||||
loc
|
||||
}
|
||||
});
|
||||
Ok(loc.0 * loc.1)
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
println!("Part 1: {}", part_1()?);
|
||||
println!("Part 2: {}", part_2()?);
|
||||
|
||||
Ok(())
|
||||
}
|
3
2021/src/bin/03-binary-diagnostic.rs
Normal file
3
2021/src/bin/03-binary-diagnostic.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
fn main() -> anyhow::Result<()> {
|
||||
let lines = aoc::read_vec_per_line("./data/03.diagnostics", |c| char::to_digit(c, 2)?);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue