Compare commits
6 Commits
807bff7977
...
6f2e046080
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6f2e046080 | ||
|
|
6f9ca4dfb6 | ||
|
|
2fa6c40f33 | ||
|
|
54103e8fa6 | ||
|
|
fc328d82af | ||
|
|
267dfb8068 |
34
input/day12.txt
Normal file
34
input/day12.txt
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
initial state: ..##.#######...##.###...#..#.#.#..#.##.#.##....####..........#..#.######..####.#.#..###.##..##..#..#
|
||||||
|
|
||||||
|
#..#. => .
|
||||||
|
..#.. => .
|
||||||
|
..#.# => #
|
||||||
|
##.#. => .
|
||||||
|
.#... => #
|
||||||
|
#.... => .
|
||||||
|
##### => #
|
||||||
|
.#.## => .
|
||||||
|
#.#.. => .
|
||||||
|
#.### => #
|
||||||
|
.##.. => #
|
||||||
|
##... => .
|
||||||
|
#...# => #
|
||||||
|
####. => #
|
||||||
|
#.#.# => .
|
||||||
|
#..## => .
|
||||||
|
.#### => .
|
||||||
|
...## => .
|
||||||
|
..### => #
|
||||||
|
.#..# => .
|
||||||
|
##..# => #
|
||||||
|
.#.#. => .
|
||||||
|
..##. => .
|
||||||
|
###.. => .
|
||||||
|
###.# => #
|
||||||
|
#.##. => #
|
||||||
|
..... => .
|
||||||
|
.##.# => #
|
||||||
|
....# => .
|
||||||
|
##.## => #
|
||||||
|
...#. => #
|
||||||
|
.###. => .
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
fn main() {
|
fn main() {
|
||||||
aoc_2018::tasks::day13::task1();
|
// aoc_2018::tasks::day14::task1();
|
||||||
// aoc_2018::tasks::day09::task2();
|
aoc_2018::tasks::day14::task2();
|
||||||
}
|
}
|
||||||
|
|||||||
124
src/tasks/day11.rs
Normal file
124
src/tasks/day11.rs
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
pub fn task1() {
|
||||||
|
let square_size = 3;
|
||||||
|
let serial = 6392;
|
||||||
|
let result = (1..=300 - square_size)
|
||||||
|
.flat_map(|x: i32| (1..=300 - square_size).map(move |y: i32| (x, y)))
|
||||||
|
.map(|(x, y)| {
|
||||||
|
let power: i32 = (x..x + square_size)
|
||||||
|
.map(|x| {
|
||||||
|
(y..y + square_size)
|
||||||
|
.map(move |y| fuel_level(x, y, serial))
|
||||||
|
.sum::<i32>()
|
||||||
|
})
|
||||||
|
.sum();
|
||||||
|
(x, y, power)
|
||||||
|
})
|
||||||
|
.max_by_key(|(_, _, value)| *value);
|
||||||
|
println!("{:?}", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn task2() {
|
||||||
|
let serial = 6392;
|
||||||
|
|
||||||
|
let mut values: Vec<Vec<i32>> = Vec::with_capacity(301);
|
||||||
|
for x in 0..=300 {
|
||||||
|
let mut v: Vec<i32> = Vec::with_capacity(301);
|
||||||
|
for y in 0..=300 {
|
||||||
|
v.push(fuel_level(x, y, serial));
|
||||||
|
}
|
||||||
|
values.push(v);
|
||||||
|
}
|
||||||
|
let values = &values;
|
||||||
|
|
||||||
|
let result = (1..300)
|
||||||
|
.map(|square_size| {
|
||||||
|
let result = (1..=300 - square_size)
|
||||||
|
.flat_map(|x: i32| (1..=300 - square_size).map(move |y: i32| (x, y)))
|
||||||
|
.map(|(x, y)| {
|
||||||
|
let power: i32 = (x..x + square_size)
|
||||||
|
.map(|x| {
|
||||||
|
(y..y + square_size)
|
||||||
|
.map(move |y| values[x as usize][y as usize])
|
||||||
|
.sum::<i32>()
|
||||||
|
})
|
||||||
|
.sum();
|
||||||
|
(x, y, power)
|
||||||
|
})
|
||||||
|
.max_by_key(|(_, _, value)| *value)
|
||||||
|
.unwrap();
|
||||||
|
(result.0, result.1, result.2, square_size)
|
||||||
|
})
|
||||||
|
.max_by_key(|result| result.2);
|
||||||
|
println!("{:?}", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn task2_fast() {
|
||||||
|
let serial = 6392;
|
||||||
|
|
||||||
|
let mut cache: Vec<Vec<i32>> = Vec::with_capacity(301);
|
||||||
|
for x in 0 as usize..=300 {
|
||||||
|
let mut v: Vec<i32> = Vec::with_capacity(301);
|
||||||
|
for y in 0 as usize..=300 {
|
||||||
|
if x == 0 || y == 0 {
|
||||||
|
v.push(0);
|
||||||
|
} else {
|
||||||
|
v.push(
|
||||||
|
fuel_level(x as i32, y as i32, serial) + v[v.len() - 1] + cache[x - 1][y]
|
||||||
|
- cache[x - 1][y - 1],
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cache.push(v);
|
||||||
|
}
|
||||||
|
let values = &cache;
|
||||||
|
|
||||||
|
for x in 0 as usize..=300 {
|
||||||
|
for y in 0 as usize..=300 {
|
||||||
|
if x != 0 && y != 0 {
|
||||||
|
let cached = area_sum(&values, x, y, 1);
|
||||||
|
let calc = fuel_level(x as i32, y as i32, serial);
|
||||||
|
//println!("{},{}: {} ({})", x, y, values[x][y], calc);
|
||||||
|
assert_eq!(calc, cached);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = (1..=300)
|
||||||
|
.map(|square_size| {
|
||||||
|
let result = (1..=301 - square_size)
|
||||||
|
.flat_map(|x: usize| (1..=301 - square_size).map(move |y: usize| (x, y)))
|
||||||
|
.map(|(x, y)| (x, y, area_sum(&values, x, y, square_size)))
|
||||||
|
.max_by_key(|(_, _, value)| *value)
|
||||||
|
.unwrap();
|
||||||
|
(result.0, result.1, result.2, square_size)
|
||||||
|
})
|
||||||
|
.max_by_key(|result| result.2);
|
||||||
|
println!("{:?}", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn area_sum(values: &Vec<Vec<i32>>, x: usize, y: usize, length: usize) -> i32 {
|
||||||
|
values[x + length - 1][y + length - 1] + values[x - 1][y - 1]
|
||||||
|
- values[x - 1][y + length - 1]
|
||||||
|
- values[x + length - 1][y - 1]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fuel_level(x: i32, y: i32, serial: i32) -> i32 {
|
||||||
|
let rack_id = x + 10;
|
||||||
|
let mut power = rack_id * y;
|
||||||
|
power += serial;
|
||||||
|
power *= rack_id;
|
||||||
|
power = power / 100 % 10;
|
||||||
|
power = power - 5;
|
||||||
|
power
|
||||||
|
}
|
||||||
|
|
||||||
|
mod test {
|
||||||
|
use super::fuel_level;
|
||||||
|
#[test]
|
||||||
|
fn name() {
|
||||||
|
assert_eq!(fuel_level(3, 5, 8), 4);
|
||||||
|
assert_eq!(fuel_level(122, 79, 57), -5);
|
||||||
|
assert_eq!(fuel_level(217, 196, 39), 0);
|
||||||
|
assert_eq!(fuel_level(101, 153, 71), 4);
|
||||||
|
}
|
||||||
|
}
|
||||||
54
src/tasks/day12.rs
Normal file
54
src/tasks/day12.rs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
use crate::utils;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::ops::Add;
|
||||||
|
|
||||||
|
pub fn task1() {
|
||||||
|
let input = utils::read_file("input/day12.txt");
|
||||||
|
let mut input = input.lines();
|
||||||
|
let num_generations = 20;
|
||||||
|
|
||||||
|
let mut state: Vec<char> = "."
|
||||||
|
.repeat(num_generations)
|
||||||
|
.add(&input.next().unwrap()[15..])
|
||||||
|
.add(&".".repeat(num_generations + 2))
|
||||||
|
.chars()
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let mut transformations: HashMap<String, char> = HashMap::new();
|
||||||
|
|
||||||
|
input.next();
|
||||||
|
for line in input {
|
||||||
|
let key = line.split(" => ").nth(0).unwrap();
|
||||||
|
transformations.insert(key.to_string(), line.chars().last().unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
for _ in 0..num_generations {
|
||||||
|
println!("{}", state.iter().collect::<String>());
|
||||||
|
let mut new_state = state.clone();
|
||||||
|
for (i, c) in new_state[2..state.len() - 2].iter_mut().enumerate() {
|
||||||
|
let next = transformations.get(&state[i..i + 5].iter().collect::<String>());
|
||||||
|
if let Some(cc) = next {
|
||||||
|
*c = *cc;
|
||||||
|
} else {
|
||||||
|
*c = '.';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
state = new_state;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{}", state.iter().collect::<String>());
|
||||||
|
|
||||||
|
let sum: isize = state
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, c)| {
|
||||||
|
if *c == '#' {
|
||||||
|
i as isize - (num_generations as isize)
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.sum();
|
||||||
|
|
||||||
|
println!("Result: {}", sum);
|
||||||
|
}
|
||||||
55
src/tasks/day14.rs
Normal file
55
src/tasks/day14.rs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
pub fn task1() {
|
||||||
|
let input: usize = 509671;
|
||||||
|
|
||||||
|
let mut scores = vec![3, 7];
|
||||||
|
let mut recipes = vec![0, 1];
|
||||||
|
|
||||||
|
while scores.len() < input + 10 {
|
||||||
|
let mut score: usize = recipes.iter().map(|recipe| scores[*recipe]).sum();
|
||||||
|
if score >= 10 {
|
||||||
|
scores.push(1);
|
||||||
|
score -= 10;
|
||||||
|
}
|
||||||
|
scores.push(score);
|
||||||
|
|
||||||
|
for recipe in recipes.iter_mut() {
|
||||||
|
*recipe = (*recipe + 1 + scores[*recipe]) % scores.len();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = scores[input..input + 10]
|
||||||
|
.iter()
|
||||||
|
.map(|s| s.to_string())
|
||||||
|
.collect::<String>();
|
||||||
|
println!("Last 10 for {}: {}", input, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn task2() {
|
||||||
|
let input = vec![5, 0, 9, 6, 7, 1];
|
||||||
|
|
||||||
|
let mut scores = vec![3, 7];
|
||||||
|
let mut recipes = vec![0, 1];
|
||||||
|
|
||||||
|
while scores.len() < 7
|
||||||
|
|| scores[scores.len() - 6..] != input[..]
|
||||||
|
&& scores[scores.len() - 7..scores.len() - 1] != input[..]
|
||||||
|
{
|
||||||
|
let mut score: usize = recipes.iter().map(|recipe| scores[*recipe]).sum();
|
||||||
|
if score >= 10 {
|
||||||
|
scores.push(1);
|
||||||
|
score -= 10;
|
||||||
|
}
|
||||||
|
scores.push(score);
|
||||||
|
|
||||||
|
for recipe in recipes.iter_mut() {
|
||||||
|
*recipe = (*recipe + 1 + scores[*recipe]) % scores.len();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if scores[scores.len() - 6..] != input[..] {
|
||||||
|
println!("{}", scores.len() - 7)
|
||||||
|
} else {
|
||||||
|
println!("{}", scores.len() - 8)
|
||||||
|
}
|
||||||
|
// 20227890 too high
|
||||||
|
}
|
||||||
@@ -8,5 +8,7 @@ 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;
|
||||||
pub mod day13;
|
pub mod day13;
|
||||||
|
pub mod day14;
|
||||||
|
|||||||
Reference in New Issue
Block a user