-
Notifications
You must be signed in to change notification settings - Fork 0
/
day23.rs
152 lines (122 loc) · 3.98 KB
/
day23.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//! [Day 23: Experimental Emergency Teleportation](https://adventofcode.com/2018/day/23)
use regex::Regex;
use std::ops::AddAssign;
use z3::ast::Int;
struct Nanobot {
x: i64,
y: i64,
z: i64,
r: u64,
}
impl Nanobot {
fn from(text: &str) -> Self {
let re = Regex::new(r"^pos=<(-?\d+),(-?\d+),(-?\d+)>, r=(-?\d+)$").unwrap();
let caps = re.captures(text).unwrap();
Self {
x: caps.get(1).unwrap().as_str().parse().unwrap(),
y: caps.get(2).unwrap().as_str().parse().unwrap(),
z: caps.get(3).unwrap().as_str().parse().unwrap(),
r: caps.get(4).unwrap().as_str().parse().unwrap(),
}
}
fn dist(&self, other: &Self) -> u64 {
self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z)
}
}
struct Puzzle {
nanobots: Vec<Nanobot>,
}
impl Puzzle {
fn new() -> Puzzle {
Puzzle { nanobots: vec![] }
}
/// Get the puzzle input.
fn configure(&mut self, path: &str) {
let data = std::fs::read_to_string(path).unwrap();
for line in data.lines() {
self.nanobots.push(Nanobot::from(line));
}
}
/// Solve part one.
fn part1(&self) -> usize {
let strongest = self.nanobots.iter().max_by_key(|a| a.r).unwrap();
self.nanobots
.iter()
.filter(|a| a.dist(strongest) <= strongest.r)
.count()
}
/// Solve part two.
fn part2(&self) -> i64 {
let cfg = z3::Config::new();
let ctx = z3::Context::new(&cfg);
let o = z3::Optimize::new(&ctx);
let x = Int::new_const(&ctx, "x");
let y = Int::new_const(&ctx, "y");
let z = Int::new_const(&ctx, "z");
let one = Int::from_u64(&ctx, 1);
let zero = Int::from_u64(&ctx, 0);
let mut count = Int::from_u64(&ctx, 0);
let dist = |px, py, pz| -> _ {
let px = Int::from_i64(&ctx, px);
let py = Int::from_i64(&ctx, py);
let pz = Int::from_i64(&ctx, pz);
let dx = x
.ge(&px) // x-px if x>=px, px-x otherwises
.ite(&Int::sub(&ctx, &[&x, &px]), &Int::sub(&ctx, &[&px, &x]));
let dy = y
.ge(&py)
.ite(&Int::sub(&ctx, &[&y, &py]), &Int::sub(&ctx, &[&py, &y]));
let dz = z
.ge(&pz)
.ite(&Int::sub(&ctx, &[&z, &pz]), &Int::sub(&ctx, &[&pz, &z]));
Int::add(&ctx, &[&dx, &dy, &dz])
};
for bot in &self.nanobots {
let d = dist(bot.x, bot.y, bot.z);
count.add_assign(
d // manhattan distance
.le(&Int::from_u64(&ctx, bot.r)) // <=r
.ite(&one, &zero), // count of
);
}
o.maximize(&count);
o.minimize(&dist(0, 0, 0));
match o.check(&[]) {
z3::SatResult::Sat => {
if let Some(model) = o.get_model() {
let xx = model.eval(&x, true).unwrap();
let yy = model.eval(&y, true).unwrap();
let zz = model.eval(&z, true).unwrap();
return xx.as_i64().unwrap() + yy.as_i64().unwrap() + zz.as_i64().unwrap();
}
}
z3::SatResult::Unsat => eprintln!("result Unsat"),
z3::SatResult::Unknown => eprintln!("result Unknown"),
}
0
}
}
fn main() {
let args = aoc::parse_args();
let mut puzzle = Puzzle::new();
puzzle.configure(args.path.as_str());
println!("{}", puzzle.part1());
println!("{}", puzzle.part2());
}
/// Test from puzzle input
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test01() {
let mut puzzle = Puzzle::new();
puzzle.configure("sample_1.txt");
assert_eq!(puzzle.part1(), 7);
}
#[test]
fn test02() {
let mut puzzle = Puzzle::new();
puzzle.configure("sample_2.txt");
assert_eq!(puzzle.part2(), 36);
}
}