mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Implementation 2021 day 2
This commit is contained in:
1000
2021/inputs/02.txt
Normal file
1000
2021/inputs/02.txt
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,9 +1,90 @@
|
|||||||
|
use std::io::BufRead;
|
||||||
|
use std::io::BufReader;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
|
||||||
pub fn part1(_input: &mut dyn Read) -> String {
|
enum Dir {
|
||||||
todo!()
|
Up,
|
||||||
|
Down,
|
||||||
|
Forward,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part2(_input: &mut dyn Read) -> String {
|
fn parse_input(input: &mut dyn Read) -> Vec<(Dir, i32)> {
|
||||||
todo!()
|
let mut reader = BufReader::new(input);
|
||||||
|
let mut buffer = String::new();
|
||||||
|
|
||||||
|
let mut moves = Vec::new();
|
||||||
|
|
||||||
|
while matches!(reader.read_line(&mut buffer), Ok(n) if n > 0) {
|
||||||
|
let (dir, amount) = buffer.trim().split_once(" ").unwrap();
|
||||||
|
|
||||||
|
let dir = match dir {
|
||||||
|
"up" => Dir::Up,
|
||||||
|
"down" => Dir::Down,
|
||||||
|
"forward" => Dir::Forward,
|
||||||
|
_ => panic!("Invalid direction '{}'", dir),
|
||||||
|
};
|
||||||
|
|
||||||
|
moves.push((dir, amount.parse().unwrap()));
|
||||||
|
|
||||||
|
buffer.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
moves
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part1(input: &mut dyn Read) -> String {
|
||||||
|
let moves = parse_input(input);
|
||||||
|
|
||||||
|
let mut x = 0;
|
||||||
|
let mut depth = 0;
|
||||||
|
|
||||||
|
for (dir, amount) in moves {
|
||||||
|
match dir {
|
||||||
|
Dir::Up => depth -= amount,
|
||||||
|
Dir::Down => depth += amount,
|
||||||
|
Dir::Forward => x += amount,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(x * depth).to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part2(input: &mut dyn Read) -> String {
|
||||||
|
let moves = parse_input(input);
|
||||||
|
|
||||||
|
let mut x = 0;
|
||||||
|
let mut depth = 0;
|
||||||
|
let mut aim = 0;
|
||||||
|
|
||||||
|
for (dir, amount) in moves {
|
||||||
|
match dir {
|
||||||
|
Dir::Up => aim -= amount,
|
||||||
|
Dir::Down => aim += amount,
|
||||||
|
Dir::Forward => {
|
||||||
|
x += amount;
|
||||||
|
depth += aim * amount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(x * depth).to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::test_implementation;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
const SAMPLE: &[u8] = include_bytes!("samples/02.txt");
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sample_part1() {
|
||||||
|
test_implementation(part1, SAMPLE, 150);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sample_part2() {
|
||||||
|
test_implementation(part1, SAMPLE, 150);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
6
2021/src/samples/02.txt
Normal file
6
2021/src/samples/02.txt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
forward 5
|
||||||
|
down 5
|
||||||
|
forward 8
|
||||||
|
up 3
|
||||||
|
down 8
|
||||||
|
forward 2
|
||||||
Reference in New Issue
Block a user