Compare commits

..

3 Commits

Author SHA1 Message Date
Johannes
eb7467f615 day12 task 1 2019-12-12 16:27:27 +01:00
Johannes
8849191a6b day11 task 2 2019-12-12 11:40:05 +01:00
Johannes
009d1b8f86 day11 task 1 2019-12-12 11:32:38 +01:00
8 changed files with 265 additions and 1 deletions

49
Cargo.lock generated
View File

@@ -1,10 +1,19 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]]
name = "aho-corasick"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "aoc_2019" name = "aoc_2019"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@@ -20,6 +29,46 @@ dependencies = [
"either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)", "either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "memchr"
version = "2.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "regex"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.6.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "thread_local"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata] [metadata]
"checksum aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58fb5e95d83b38284460a5fda7d6470aa0b8844d283a0b614b8535e880800d2d"
"checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" "checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3"
"checksum itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484" "checksum itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484"
"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
"checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e"
"checksum regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dc220bd33bdce8f093101afe22a037b8eb0e5af33592e6a9caafff0d4cb81cbd"
"checksum regex-syntax 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "11a7e20d1cce64ef2fed88b66d347f88bd9babb82845b2b858f3edbf59a4f716"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"

View File

@@ -8,3 +8,4 @@ edition = "2018"
[dependencies] [dependencies]
itertools = "0.8.2" itertools = "0.8.2"
regex = "1.3.1"

1
input/day11.txt Normal file
View File

@@ -0,0 +1 @@
3,8,1005,8,361,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,1001,8,0,28,2,1104,18,10,1006,0,65,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,1001,8,0,57,1,1101,5,10,2,108,15,10,2,102,12,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,102,1,8,91,2,1005,4,10,2,1107,10,10,1006,0,16,2,109,19,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,101,0,8,129,1,104,3,10,1,1008,9,10,1006,0,65,1,104,5,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,165,1,1106,11,10,1,1106,18,10,1,8,11,10,1,4,11,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,1001,8,0,203,2,1003,11,10,1,1105,13,10,1,101,13,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,101,0,8,237,2,7,4,10,1006,0,73,1,1003,7,10,1006,0,44,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,101,0,8,273,2,108,14,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,102,1,8,299,1,1107,6,10,1006,0,85,1,1107,20,10,1,1008,18,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1001,8,0,337,2,107,18,10,101,1,9,9,1007,9,951,10,1005,10,15,99,109,683,104,0,104,1,21102,1,825594852248,1,21101,378,0,0,1105,1,482,21101,0,387240006552,1,21101,0,389,0,1106,0,482,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21101,0,29032025091,1,21101,436,0,0,1106,0,482,21101,29033143299,0,1,21102,1,447,0,1105,1,482,3,10,104,0,104,0,3,10,104,0,104,0,21101,988669698916,0,1,21101,0,470,0,1106,0,482,21101,0,709052072804,1,21102,1,481,0,1106,0,482,99,109,2,21202,-1,1,1,21101,0,40,2,21101,0,513,3,21101,503,0,0,1106,0,546,109,-2,2105,1,0,0,1,0,0,1,109,2,3,10,204,-1,1001,508,509,524,4,0,1001,508,1,508,108,4,508,10,1006,10,540,1101,0,0,508,109,-2,2105,1,0,0,109,4,1202,-1,1,545,1207,-3,0,10,1006,10,563,21102,0,1,-3,21202,-3,1,1,22101,0,-2,2,21102,1,1,3,21101,582,0,0,1105,1,587,109,-4,2106,0,0,109,5,1207,-3,1,10,1006,10,610,2207,-4,-2,10,1006,10,610,21202,-4,1,-4,1106,0,678,22102,1,-4,1,21201,-3,-1,2,21202,-2,2,3,21102,629,1,0,1106,0,587,22102,1,1,-4,21101,0,1,-1,2207,-4,-2,10,1006,10,648,21102,0,1,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,670,21202,-1,1,1,21101,670,0,0,105,1,545,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2106,0,0

4
input/day12.txt Normal file
View File

@@ -0,0 +1,4 @@
<x=19, y=-10, z=7>
<x=1, y=2, z=-3>
<x=14, y=-4, z=1>
<x=8, y=7, z=-6>

View File

@@ -1,5 +1,5 @@
mod tasks; mod tasks;
fn main() { fn main() {
tasks::day10::run(); tasks::day12::run();
} }

106
src/tasks/day11.rs Normal file
View File

@@ -0,0 +1,106 @@
use super::day05::{IntCodeComputer, RAM};
use std::collections::{HashMap, HashSet};
#[allow(dead_code)]
pub fn run() {
let input = std::fs::read_to_string("input/day11.txt").unwrap();
let ram: RAM = input
.split(",")
.enumerate()
.map(|(i, s)| (i, s.parse::<i128>().unwrap()))
.collect();
task1(ram.clone());
task2(ram.clone());
}
fn task1(ram: RAM) {
let directions: HashMap<_, _> =
vec![('^', (0, -1)), ('v', (0, 1)), ('<', (-1, 0)), ('>', (1, 0))]
.into_iter()
.collect();
let turns: HashMap<_, _> = vec![
(('^', 0), '<'),
(('^', 1), '>'),
(('v', 0), '>'),
(('v', 1), '<'),
(('<', 0), 'v'),
(('<', 1), '^'),
(('>', 0), '^'),
(('>', 1), 'v'),
]
.into_iter()
.collect();
let mut colors: HashMap<(i32, i32), i128> = HashMap::new();
let mut painted_positions: HashSet<(i32, i32)> = HashSet::new();
let get = |colors: &HashMap<_, _>, (x, y)| **colors.get(&(x, y)).get_or_insert(&0);
let turn = |(c, d)| turns.get(&(c, d)).unwrap();
let mov = |(x0, y0), (x1, y1)| (x0 + x1, y0 + y1);
let mut done = false;
let mut computer = IntCodeComputer::new(vec![], ram);
let mut pos = (0, 0);
let mut dir = '^';
while !done {
computer.set_input(&[get(&colors, pos)]);
computer.clear_output();
done = computer.run_until_input_empty();
let color = computer.get_output()[0];
let turn_indicator = computer.get_output()[1];
colors.insert(pos, color);
painted_positions.insert(pos);
dir = *turn((dir, turn_indicator));
pos = mov(pos, *directions.get(&dir).unwrap());
}
println!("Task 1: {} positions were painted", painted_positions.len());
}
fn task2(ram: RAM) {
let directions: HashMap<_, _> =
vec![('^', (0, -1)), ('v', (0, 1)), ('<', (-1, 0)), ('>', (1, 0))]
.into_iter()
.collect();
let turns: HashMap<_, _> = vec![
(('^', 0), '<'),
(('^', 1), '>'),
(('v', 0), '>'),
(('v', 1), '<'),
(('<', 0), 'v'),
(('<', 1), '^'),
(('>', 0), '^'),
(('>', 1), 'v'),
]
.into_iter()
.collect();
let mut colors: HashMap<(i32, i32), i128> = HashMap::new();
colors.insert((0, 0), 1);
let mut painted_positions: HashSet<(i32, i32)> = HashSet::new();
let get = |colors: &HashMap<_, _>, (x, y)| **colors.get(&(x, y)).get_or_insert(&0);
let turn = |(c, d)| turns.get(&(c, d)).unwrap();
let mov = |(x0, y0), (x1, y1)| (x0 + x1, y0 + y1);
let mut done = false;
let mut computer = IntCodeComputer::new(vec![], ram);
let mut pos = (0, 0);
let mut dir = '^';
while !done {
computer.set_input(&[get(&colors, pos)]);
computer.clear_output();
done = computer.run_until_input_empty();
let color = computer.get_output()[0];
let turn_indicator = computer.get_output()[1];
colors.insert(pos, color);
painted_positions.insert(pos);
dir = *turn((dir, turn_indicator));
pos = mov(pos, *directions.get(&dir).unwrap());
}
let xmin = painted_positions.iter().min_by_key(|it| it.0).unwrap().0;
let xmax = painted_positions.iter().max_by_key(|it| it.0).unwrap().0;
let ymin = painted_positions.iter().min_by_key(|it| it.1).unwrap().1;
let ymax = painted_positions.iter().max_by_key(|it| it.1).unwrap().1;
(ymin..=ymax).into_iter().for_each(|y| {
(xmin..=xmax).into_iter().for_each(|x| {
print!("{}", if get(&colors, (x, y)) == 1 { '#' } else { ' ' });
});
println!("")
})
}

101
src/tasks/day12.rs Normal file
View File

@@ -0,0 +1,101 @@
use regex::Regex;
#[allow(dead_code)]
pub fn run() {
let regex = Regex::new("<x=(-?\\d+),\\sy=(-?\\d+),\\sz=(-?\\d+)").unwrap();
let input: Vec<Point> = std::fs::read_to_string("input/day12.txt")
.unwrap()
.lines()
.map(|line| {
let m = regex.captures(&line).unwrap();
let x: i32 = m[1].parse().unwrap();
let y: i32 = m[2].parse().unwrap();
let z: i32 = m[3].parse().unwrap();
Point { x, y, z }
})
.collect();
task1(input.clone());
}
fn task1(input: Vec<Point>) {
let moons: Vec<_> = input
.into_iter()
.map(|pos| Moon {
pos: pos,
vel: Point::new(0, 0, 0),
})
.collect();
let finals = (0..1000).fold(moons, |moons, _run| {
let gravities: Vec<Point> = moons
.iter()
.map(|base| {
moons.iter().fold(Point::new(0, 0, 0), |grav, other_moon| {
grav.add(base.pos.gravity(other_moon.pos))
})
})
.collect();
let moons = moons
.into_iter()
.zip(gravities.into_iter())
.map(|(mut moon, grav)| {
moon.vel = moon.vel.add(grav);
moon.pos = moon.pos.add(moon.vel);
moon
})
.collect();
//println!("{}: {:?}", run, moons);
moons
});
let energy: i32 = finals
.iter()
.map(|moon| moon.pos.energy() * moon.vel.energy())
.sum();
println!("Task 1: sum of energy is {}", energy);
//392 too low
}
#[derive(Debug)]
struct Moon {
pos: Point,
vel: Point,
}
#[derive(Clone, Copy, Debug)]
struct Point {
x: i32,
y: i32,
z: i32,
}
impl Point {
fn new(x: i32, y: i32, z: i32) -> Self {
Point { x, y, z }
}
fn add(&self, other: Self) -> Self {
Point {
x: self.x + other.x,
y: self.y + other.y,
z: self.z + other.z,
}
}
fn gravity(&self, other: Self) -> Self {
fn d(a: i32, b: i32) -> i32 {
use std::cmp::Ordering;
match a.cmp(&b) {
Ordering::Less => 1,
Ordering::Equal => 0,
Ordering::Greater => -1,
}
}
Point {
x: d(self.x, other.x),
y: d(self.y, other.y),
z: d(self.z, other.z),
}
}
fn energy(&self) -> i32 {
self.x.abs() + self.y.abs() + self.z.abs()
}
}

View File

@@ -7,3 +7,5 @@ pub mod day07;
pub mod day08; pub mod day08;
pub mod day09; pub mod day09;
pub mod day10; pub mod day10;
pub mod day11;
pub mod day12;