Skip to content

Commit 0225609

Browse files
committed
feat(2018): refactor days 10-12 to use aoc-rust-common
1 parent cbbbb9c commit 0225609

9 files changed

Lines changed: 555 additions & 323 deletions

File tree

2018/inputs/10/prod.txt

Lines changed: 357 additions & 0 deletions
Large diffs are not rendered by default.

2018/inputs/10/test.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
position=< 9, 1> velocity=< 0, 2>
2+
position=< 7, 0> velocity=<-1, 0>
3+
position=< 3, -2> velocity=<-1, 1>
4+
position=< 6, 10> velocity=<-2, -1>
5+
position=< 2, -4> velocity=< 2, 2>
6+
position=<-6, 10> velocity=< 2, -2>
7+
position=< 1, 8> velocity=< 1, -1>
8+
position=< 1, 7> velocity=< 1, 0>
9+
position=<-3, 11> velocity=< 1, -2>
10+
position=< 7, 6> velocity=<-1, -1>
11+
position=<-2, 3> velocity=< 1, 0>
12+
position=<-4, 3> velocity=< 2, 0>
13+
position=<10, -3> velocity=<-1, 1>
14+
position=< 5, 11> velocity=< 1, -2>
15+
position=< 4, 7> velocity=< 0, -1>
16+
position=< 8, -2> velocity=< 0, 1>
17+
position=<15, 0> velocity=<-2, 0>
18+
position=< 1, 6> velocity=< 1, 0>
19+
position=< 8, 9> velocity=< 0, -1>
20+
position=< 3, 3> velocity=<-1, 1>
21+
position=< 0, 5> velocity=< 0, -1>
22+
position=<-2, 2> velocity=< 2, 0>
23+
position=< 5, -2> velocity=< 1, 2>
24+
position=< 1, 4> velocity=< 2, 1>
25+
position=<-2, 7> velocity=< 2, -2>
26+
position=< 3, 6> velocity=<-1, -1>
27+
position=< 5, 0> velocity=< 1, 0>
28+
position=<-6, 0> velocity=< 2, 0>
29+
position=< 5, 9> velocity=< 1, -2>
30+
position=<14, 7> velocity=<-2, 0>
31+
position=<-3, 6> velocity=< 2, -1>

2018/inputs/11/prod.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6392

2018/inputs/12/prod.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
initial state: #.##.####...##...##...##.##..#.#.##########...##.##.##.##..#..#.#..#..###..#..#...#...##...##.##...
2+
..#...#.#.##...#..#...#..#..#.#..#
3+
...#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#....
4+
...#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#..#.....
5+
...#...#..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##..#...##.....

2018/src/day10/mod.rs

Lines changed: 40 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,67 @@
1+
use aoc_rust_common::Solution;
2+
use std::fmt::Display;
3+
use regex::Regex;
14
use lazy_static::lazy_static;
2-
use regex::{Error, Regex};
35

4-
use std::io::BufRead;
6+
pub struct Day10;
57

68
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7-
struct Node {
8-
x: i32,
9-
y: i32,
10-
vx: i32,
11-
vy: i32,
12-
}
9+
struct Node { x: i32, y: i32, vx: i32, vy: i32 }
1310

1411
impl Node {
15-
fn tick(&mut self) {
16-
self.x += self.vx;
17-
self.y += self.vy;
18-
}
12+
fn tick(&mut self) { self.x += self.vx; self.y += self.vy; }
1913
}
2014

2115
impl TryFrom<&str> for Node {
2216
type Error = &'static str;
2317
fn try_from(value: &str) -> Result<Self, Self::Error> {
2418
lazy_static! {
25-
static ref RE: Regex = Regex::new(
26-
r"position=<\s*([-]?\d+),\s*([-]?\d+)> velocity=<\s*([-]?\d+),\s*([-]?\d+)>"
27-
)
28-
.unwrap();
19+
static ref RE: Regex = Regex::new(r"position=<\s*(-?\d+),\s*(-?\d+)> velocity=<\s*(-?\d+),\s*(-?\d+)>").unwrap();
2920
}
30-
3121
let caps = RE.captures(value).unwrap();
32-
let x = caps.get(1).unwrap().as_str().parse::<i32>().unwrap();
33-
let y = caps.get(2).unwrap().as_str().parse::<i32>().unwrap();
34-
let vx = caps.get(3).unwrap().as_str().parse::<i32>().unwrap();
35-
let vy = caps.get(4).unwrap().as_str().parse::<i32>().unwrap();
36-
Ok(Node { x, y, vx, vy })
22+
Ok(Node {
23+
x: caps[1].parse().unwrap(), y: caps[2].parse().unwrap(),
24+
vx: caps[3].parse().unwrap(), vy: caps[4].parse().unwrap(),
25+
})
3726
}
3827
}
3928

40-
fn solution(nodes: &mut Vec<Node>) {
29+
fn find_message(nodes: &mut Vec<Node>) -> (String, i32) {
4130
for counter in 1.. {
4231
nodes.iter_mut().for_each(|node| node.tick());
43-
if nodes[..nodes.len() - 1]
44-
.iter()
45-
.zip(nodes[1..].iter())
46-
.all(|pair| (pair.0.y - pair.1.y).abs() <= 10)
47-
{
48-
println!("result of q02 is {}", counter);
49-
break;
32+
let min_y = nodes.iter().map(|n| n.y).min().unwrap();
33+
let max_y = nodes.iter().map(|n| n.y).max().unwrap();
34+
35+
if max_y - min_y <= 10 {
36+
let min_x = nodes.iter().map(|n| n.x).min().unwrap();
37+
let max_x = nodes.iter().map(|n| n.x).max().unwrap();
38+
39+
let mut grid = vec![vec!['.'; (max_x - min_x + 1) as usize]; (max_y - min_y + 1) as usize];
40+
for node in nodes {
41+
grid[(node.y - min_y) as usize][(node.x - min_x) as usize] = '#';
42+
}
43+
let message = grid.iter().map(|row| row.iter().collect::<String>()).collect::<Vec<_>>().join("
44+
");
45+
return (message, counter);
5046
}
5147
}
48+
("".to_string(), 0)
5249
}
5350

54-
fn visualize(nodes: &Vec<Node>) {
55-
let min_x = nodes.iter().map(|n| n.x).min().unwrap();
56-
let min_y = nodes.iter().map(|n| n.y).min().unwrap();
57-
58-
let mut visualized: [[char; 100]; 10] = [['.'; 100]; 10];
59-
nodes
60-
.iter()
61-
.for_each(|n| visualized[(n.y - min_y) as usize][(n.x - min_x) as usize] = '#');
62-
visualized.iter().for_each(|line| {
63-
line.iter().for_each(|c| print!("{}", c));
64-
println!();
65-
});
66-
}
51+
impl Solution for Day10 {
52+
fn year(&self) -> u32 { 2018 }
53+
fn day(&self) -> u32 { 10 }
6754

68-
fn part1(content: String) -> Result<(), Error> {
69-
let _path = format!("./input/{}", "day10.txt");
70-
71-
let mut nodes: Vec<Node> = content
72-
.lines()
73-
.map(|s| s.try_into().unwrap())
74-
.collect::<Vec<_>>();
75-
76-
solution(&mut nodes);
77-
visualize(&nodes);
78-
Ok(())
79-
}
80-
81-
#[cfg(test)]
82-
mod tests {
83-
use super::*;
55+
fn part1(&self, input: &str) -> Box<dyn Display> {
56+
let mut nodes: Vec<Node> = input.lines().map(|s| s.try_into().unwrap()).collect();
57+
let (message, _) = find_message(&mut nodes);
58+
Box::new(format!("
59+
{}", message))
60+
}
8461

85-
#[test]
86-
#[ignore]
87-
fn test_first() {
88-
let example = include_str!("./example.data");
89-
let _ = part1(example.to_owned());
90-
let prod = include_str!("./prod.data");
91-
let _ = part1(prod.to_owned());
62+
fn part2(&self, input: &str) -> Box<dyn Display> {
63+
let mut nodes: Vec<Node> = input.lines().map(|s| s.try_into().unwrap()).collect();
64+
let (_, seconds) = find_message(&mut nodes);
65+
Box::new(seconds)
9266
}
9367
}

2018/src/day11/mod.rs

Lines changed: 46 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,71 @@
1-
use std::io::Result;
1+
use aoc_rust_common::Solution;
2+
use std::fmt::Display;
23

3-
const SERIAL_NUMBER: i32 = 6392;
4-
const GRID_SIZE: i32 = 300;
5-
6-
struct Grid {
7-
power: Vec<Vec<i32>>,
8-
}
9-
10-
impl Grid {
11-
fn new(size: i32) -> Grid {
12-
Grid {
13-
power: vec![vec![0; size as usize]; size as usize],
14-
}
15-
}
16-
17-
fn set(&mut self, x: i32, y: i32, power: i32) {
18-
self.power[x as usize - 1][y as usize - 1] = power;
19-
}
20-
21-
fn get(&self, x: i32, y: i32) -> Option<i32> {
22-
let (x, y) = (x - 1, y - 1);
23-
if (0..GRID_SIZE).contains(&x) && (0..GRID_SIZE).contains(&y) {
24-
Some(self.power[x as usize][y as usize])
25-
} else {
26-
None
27-
}
28-
}
4+
pub struct Day11;
295

30-
fn square_power(&self, size: i32, top_left_x: i32, top_left_y: i32) -> i32 {
31-
let mut power = 0;
32-
for x in top_left_x..top_left_x + size {
33-
for y in top_left_y..top_left_y + size {
34-
power += self.get(x, y).unwrap_or(0);
35-
}
36-
}
37-
power
38-
}
39-
}
6+
const GRID_SIZE: i32 = 300;
407

41-
fn fuel_cell_power(x: i32, y: i32) -> i32 {
8+
fn get_power(x: i32, y: i32, serial_number: i32) -> i32 {
429
let rack_id = x + 10;
4310
let mut power = rack_id * y;
44-
power += SERIAL_NUMBER;
11+
power += serial_number;
4512
power *= rack_id;
46-
power = (power / 100) % 10;
47-
power -= 5;
48-
power
13+
(power / 100) % 10 - 5
4914
}
5015

51-
fn part1(grid: &Grid) -> Result<(i32, i32)> {
52-
let (mut max_x, mut max_y, mut max_power) = (1, 1, grid.square_power(3, 1, 1));
53-
for x in 1..=GRID_SIZE {
54-
for y in 1..=GRID_SIZE {
55-
let power = grid.square_power(3, x, y);
56-
if power > max_power {
57-
max_x = x;
58-
max_y = y;
59-
max_power = power;
60-
}
16+
fn build_summed_area_table(serial_number: i32) -> Vec<Vec<i32>> {
17+
let mut sat = vec![vec![0; (GRID_SIZE + 1) as usize]; (GRID_SIZE + 1) as usize];
18+
for y in 1..=GRID_SIZE {
19+
for x in 1..=GRID_SIZE {
20+
let power = get_power(x, y, serial_number);
21+
sat[y as usize][x as usize] = power + sat[y as usize - 1][x as usize] + sat[y as usize][x as usize - 1] - sat[y as usize - 1][x as usize - 1];
6122
}
6223
}
24+
sat
25+
}
6326

64-
Ok((max_x, max_y))
27+
fn get_square_power(sat: &[Vec<i32>], x: i32, y: i32, size: i32) -> i32 {
28+
let (x1, y1, x2, y2) = (x - 1, y - 1, x + size - 1, y + size - 1);
29+
sat[y2 as usize][x2 as usize] - sat[y1 as usize][x2 as usize] - sat[y2 as usize][x1 as usize] + sat[y1 as usize][x1 as usize]
6530
}
6631

67-
fn part2(grid: &Grid) -> Result<(i32, i32, i32)> {
68-
let (mut max_size, mut max_x, mut max_y, mut max_power) = (1, 1, 1, grid.square_power(1, 1, 1));
32+
impl Solution for Day11 {
33+
fn year(&self) -> u32 { 2018 }
34+
fn day(&self) -> u32 { 11 }
6935

70-
for size in 1..=GRID_SIZE {
71-
for x in 1..=GRID_SIZE {
72-
for y in 1..=GRID_SIZE {
73-
println!("size: {}, x: {}, y: {}", size, x, y);
74-
let power = grid.square_power(size, x, y);
36+
fn part1(&self, input: &str) -> Box<dyn Display> {
37+
let serial_number: i32 = input.trim().parse().unwrap();
38+
let sat = build_summed_area_table(serial_number);
39+
let mut max_power = i32::MIN;
40+
let mut max_coord = (0, 0);
41+
for y in 1..=GRID_SIZE - 2 {
42+
for x in 1..=GRID_SIZE - 2 {
43+
let power = get_square_power(&sat, x, y, 3);
7544
if power > max_power {
76-
max_size = size;
77-
max_x = x;
78-
max_y = y;
7945
max_power = power;
80-
}
81-
if y + size > GRID_SIZE {
82-
break;
46+
max_coord = (x, y);
8347
}
8448
}
85-
if x + size > GRID_SIZE {
86-
break;
87-
}
8849
}
50+
Box::new(format!("{},{}", max_coord.0, max_coord.1))
8951
}
90-
Ok((max_x, max_y, max_size))
91-
}
92-
93-
#[cfg(test)]
94-
mod tests {
95-
use super::*;
9652

97-
#[test]
98-
#[ignore]
99-
fn test_first() {
100-
let mut grid = Grid::new(GRID_SIZE);
101-
for x in 1..=GRID_SIZE {
102-
for y in 1..=GRID_SIZE {
103-
grid.set(x, y, fuel_cell_power(x, y));
53+
fn part2(&self, input: &str) -> Box<dyn Display> {
54+
let serial_number: i32 = input.trim().parse().unwrap();
55+
let sat = build_summed_area_table(serial_number);
56+
let mut max_power = i32::MIN;
57+
let mut max_details = (0, 0, 0);
58+
for size in 1..=GRID_SIZE {
59+
for y in 1..=GRID_SIZE - size + 1 {
60+
for x in 1..=GRID_SIZE - size + 1 {
61+
let power = get_square_power(&sat, x, y, size);
62+
if power > max_power {
63+
max_power = power;
64+
max_details = (x, y, size);
65+
}
66+
}
10467
}
10568
}
106-
107-
let (x, y) = part1(&grid).unwrap();
108-
println!("most powerful 3x3 square: {},{}", x, y);
109-
let (x, y, size) = part2(&grid).unwrap();
110-
println!("most powerful 3x3 square with size: {},{},{}", x, y, size);
69+
Box::new(format!("{},{},{}", max_details.0, max_details.1, max_details.2))
11170
}
11271
}

0 commit comments

Comments
 (0)