2021 day 2 rust

This commit is contained in:
Maciej Jur 2022-11-27 17:02:25 +01:00
parent 4832842f24
commit 75fce67d69
9 changed files with 1115 additions and 10 deletions

35
2021/rust/Cargo.lock generated
View file

@ -2,6 +2,41 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "0.7.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac"
dependencies = [
"memchr",
]
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "regex"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
[[package]]
name = "rust"
version = "0.1.0"
dependencies = [
"regex",
]

View file

@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
regex = "1"

1000
2021/rust/inputs/day02.txt Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,6 @@
forward 5
down 5
forward 8
up 3
down 8
forward 2

View file

@ -3,5 +3,5 @@ mod solutions;
fn main() {
solutions::day01::run();
solutions::day02::run();
}

View file

@ -1,9 +1,9 @@
use crate::utils;
use crate::utils::Day;
use crate::utils::Source;
pub fn run() -> () {
let data = parse_data(utils::read_lines(Day(1)));
let data = parse_data(utils::read_lines(Source::Day(1)));
println!("Day 1");
println!("Part 1: {}", solve1(&data));
@ -40,4 +40,4 @@ fn parse_data(lines: Vec<String>) -> Vec<i32> {
.map(|x| x.parse())
.collect::<Result<Vec<i32>, _>>()
.expect("Failed to parse int")
}
}

View file

@ -0,0 +1,58 @@
use regex::{Captures, Regex};
use crate::utils;
use crate::utils::Source;
pub fn run() -> () {
let data = parse_data(utils::read_lines(Source::Day(2)));
println!("Day 2");
println!("Part 1: {}", solve1(&data));
println!("Part 2: {}", solve2(&data));
}
enum Command {
Forward(i32),
Down(i32),
Up(i32),
}
fn solve1(lines: &Vec<Command>) -> i32 {
let (x, depth) = lines.iter()
.fold((0, 0), |(x, depth), next| match next {
Command::Forward(val) => (x + val, depth),
Command::Down(val) => (x, depth + val),
Command::Up(val) => (x, depth - val),
});
x * depth
}
fn solve2(lines: &Vec<Command>) -> i32 {
let (x, depth, aim) = lines.iter()
.fold((0, 0, 0), |(x, depth, aim), next| match next {
Command::Forward(val) => (x + val, depth + (aim * val), aim),
Command::Down(val) => (x, depth, aim + val),
Command::Up(val) => (x, depth, aim - val),
});
x * depth
}
fn parse_data(lines: Vec<String>) -> Vec<Command> {
let re = Regex::new(r"^(\w+) (\d+)$").unwrap();
lines.iter()
.map(|line| re.captures(line).unwrap())
.map(to_command)
.collect::<Vec<_>>()
}
fn to_command(cap: Captures) -> Command {
let left = cap.get(1).unwrap().as_str();
let right = cap.get(2).unwrap().as_str().parse().unwrap();
match left {
"forward" => Command::Forward(right),
"up" => Command::Up(right),
"down" => Command::Down(right),
_ => panic!("Unknown command")
}
}

View file

@ -1 +1,2 @@
pub mod day01;
pub mod day02;

View file

@ -3,14 +3,18 @@ use std::io::{BufRead, BufReader};
use std::path::Path;
#[repr(transparent)]
pub struct Day(pub i32);
pub enum Source { Scratch, Day(i32) }
pub fn read_lines(day: Day) -> Vec<String> {
assert!(1 <= day.0 && day.0 < 25);
_read_lines(format!("inputs/day{:0>2}.txt", day.0))
.expect(format!("Failed to load day {}", day.0).as_str())
pub fn read_lines(source: Source) -> Vec<String> {
let path = match source {
Source::Scratch => "inputs/scratch.txt".to_string(),
Source::Day(day) => {
assert!(1 <= day && day < 25);
format!("inputs/day{:0>2}.txt", day)
}
};
_read_lines(&path).expect(format!("Failed to load from {}", &path).as_str())
}
fn _read_lines<P>(filename: P) -> std::io::Result<Vec<String>>