This commit is contained in:
Stefan Schwarz 2020-04-19 01:56:59 +02:00
commit 1df50835be
7 changed files with 251 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/input*
/target/

5
Cargo.lock generated Normal file
View file

@ -0,0 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "aoc2019"
version = "0.1.0"

21
Cargo.toml Normal file
View file

@ -0,0 +1,21 @@
[package]
name = "aoc2019"
version = "0.1.0"
authors = ["Stefan Schwarz <stefan@f2o.io>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[[bin]]
name = "day1"
path = "src/day1/main.rs"
[[bin]]
name = "day2"
path = "src/day2/main.rs"
[[bin]]
name = "day3"
path = "src/day3/main.rs"

24
src/day1/main.rs Normal file
View file

@ -0,0 +1,24 @@
use std::io::{self, BufRead};
fn main() -> io::Result<()> {
let mut total: i64 = 0;
io::stdin()
.lock()
.lines()
.filter_map(|l| l.ok())
.filter_map(|l| l.parse::<i64>().ok())
.for_each(|line| {
let mut cost = fuel_cost(line);
while cost > 0 {
total += cost;
cost = fuel_cost(cost);
}
});
println!("total fuel needed: {}", total);
Ok(())
}
fn fuel_cost(mass: i64) -> i64 {
mass / 3 - 2
}

102
src/day2/main.rs Normal file
View file

@ -0,0 +1,102 @@
use std::io::{self, BufRead};
enum Opcode {
Add,
Multiply,
Halt,
}
impl From<u64> for Opcode {
fn from(i: u64) -> Self {
match i {
1 => Opcode::Add,
2 => Opcode::Multiply,
99 => Opcode::Halt,
_ => panic!("invalid opcode {}", i),
}
}
}
fn main() -> io::Result<()> {
let stdin = io::stdin();
let mut program: Vec<u64> = stdin
.lock()
.split(b',')
.map(|o| {
o.expect("")
.iter()
.filter(|i| **i >= b'0' && **i <= b'9')
.fold(0, |s, i| (i - b'0' + 10 * s))
})
.map(|i| i as u64)
.collect();
program[1] = 12;
program[2] = 2;
let result = run(&mut program.clone());
println!("result: {}", result);
for a in 0..99 {
for b in 0..99 {
program[1] = a;
program[2] = b;
let result = run(&mut program.clone());
if result == 19690720 {
println!("a: {}, b: {}", a, b);
return Ok(());
}
}
}
Ok(())
}
fn run(program: &mut Vec<u64>) -> u64 {
let mut instruction_pointer: usize = 0;
loop {
let instruction = program[instruction_pointer];
let advance = match instruction.into() {
Opcode::Add => add(program, instruction_pointer),
Opcode::Multiply => multiply(program, instruction_pointer),
Opcode::Halt => return program[0],
};
instruction_pointer += advance;
}
}
fn add(program: &mut Vec<u64>, pos: usize) -> usize {
let a_pos = program[pos + 1] as usize;
let b_pos = program[pos + 2] as usize;
let c_pos = program[pos + 3] as usize;
program[c_pos] = program[a_pos] + program[b_pos];
4
}
fn multiply(program: &mut Vec<u64>, pos: usize) -> usize {
let a_pos = program[pos + 1] as usize;
let b_pos = program[pos + 2] as usize;
let c_pos = program[pos + 3] as usize;
program[c_pos] = program[a_pos] * program[b_pos];
4
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn example1() {
let mut program: Vec<u64> = vec![1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50];
assert_eq!(run(&mut program), 3500)
}
#[test]
fn example2() {
let mut program: Vec<u64> = vec![1, 0, 0, 0, 99];
assert_eq!(run(&mut program), 2)
}
#[test]
fn example3() {
let mut program: Vec<u64> = vec![1, 1, 1, 4, 99, 5, 6, 0, 99];
assert_eq!(run(&mut program), 30)
}
}

94
src/day3/main.rs Normal file
View file

@ -0,0 +1,94 @@
use std::collections::HashSet;
use std::io::{self, BufRead};
#[derive(Debug)]
enum Step {
Up(usize),
Down(usize),
Left(usize),
Right(usize),
}
impl Step {
fn draw(&self, map: &mut HashSet<Point>, start: &mut Point) {
let mover: (i64, i64, &usize) = match self {
Step::Up(s) => (0, 1, s),
Step::Down(s) => (0, -1, s),
Step::Left(s) => (-1, 0, s),
Step::Right(s) => (1, 0, s),
};
let (x, y, steps) = mover;
map.reserve(*steps);
for _ in 0..*steps {
start.x += x;
start.y += y;
map.insert(start.clone());
}
}
}
impl From<&[u8]> for Step {
fn from(b: &[u8]) -> Self {
let steps: usize = b[0..]
.iter()
.filter(|i| **i >= b'0' && **i <= b'9')
.fold(0, |s, i| ((i - b'0') as usize + 10 * s));
match b[0] {
b'U' => Step::Up(steps),
b'D' => Step::Down(steps),
b'L' => Step::Left(steps),
b'R' => Step::Right(steps),
_ => panic!("unknown directions"),
}
}
}
#[derive(Eq, PartialEq, Hash, Clone, Copy, Debug)]
struct Point {
x: i64,
y: i64,
}
impl Point {
fn dist_to_00(&self) -> u64 {
(self.x.abs() + self.y.abs()) as u64
}
}
impl From<(i64, i64)> for Point {
fn from(xy: (i64, i64)) -> Self {
Point { x: xy.0, y: xy.1 }
}
}
fn main() -> io::Result<()> {
let commands = load();
let mut wires: Vec<HashSet<Point>> = vec![];
commands.iter().enumerate().for_each(|(_, wire)| {
let mut map = HashSet::new();
let mut p: Point = (0, 0).into();
wire.iter().for_each(|step| {
step.draw(&mut map, &mut p);
});
wires.push(map);
});
let closest = wires[0].intersection(&wires[1]).fold(None, |min, point| {
let dist = point.dist_to_00();
match min {
None => Some(dist),
Some(min) => Some(if dist < min { dist } else { min }),
}
});
println!("closest: {}", closest.unwrap());
Ok(())
}
fn load() -> Vec<Vec<Step>> {
let stdin = io::stdin();
stdin
.lock()
.split(b'\n')
.filter_map(|i| i.ok())
.map(|line| line.split(|b| *b == b',').map(|i| Step::from(i)).collect())
.collect()
}

3
src/main.rs Normal file
View file

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}