-
Notifications
You must be signed in to change notification settings - Fork 0
/
day16.rs
133 lines (106 loc) · 3.16 KB
/
day16.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
use std::collections::HashSet;
use std::collections::HashMap;
use std::fs;
type Entry = i32;
type Ticket = Vec<Entry>;
struct Notes<'a> {
fields: HashMap<&'a str, HashSet<Entry>>,
ticket: Ticket,
nearby: Vec<Ticket>,
}
fn parse_field<'a>(s: &'a str) -> (&'a str, HashSet<Entry>) {
let xs: Vec<&str> = s.split(": ").collect();
let name = xs[0];
let mut values = HashSet::new();
for r in xs[1].split(" or ") {
let ys: Vec<Entry> = r.split("-").map(|x| x.parse().unwrap()).collect();
values.extend(ys[0]..=ys[1]);
}
(name, values)
}
fn parse_ticket(s: &str) -> Ticket {
s.split(",").map(|x| x.parse().unwrap()).collect()
}
fn parse_notes(data: &str) -> Notes {
let xs: Vec<&str> = data.split("\n\n").collect();
let fields = xs[0].split("\n").map(parse_field).collect();
let ticket = parse_ticket(xs[1].lines().skip(1).next().unwrap());
let nearby = xs[2].lines().skip(1).map(parse_ticket).collect();
Notes { fields, ticket, nearby }
}
fn valid_entries(notes: &Notes) -> HashSet<i32> {
notes.fields
.values()
.fold(
HashSet::new(),
|mut acc, vs| { acc.extend(vs); acc }
)
}
fn part1(notes: &Notes) -> i32 {
let v = valid_entries(notes);
let mut tot = 0;
for f in notes.nearby.iter().flat_map(|t| t.iter()) {
if !v.contains(f) {
tot += f;
}
}
tot
}
fn find_assignment<'a>(notes: &Notes, valid_fields: &Vec<HashSet<&'a str>>, acc: &mut Vec<(&'a str, usize)>) -> bool {
let num_fields = notes.fields.len();
if acc.len() == num_fields {
return true
}
let used_indices: HashSet<usize> = acc.iter().map(|(_,i)| *i).collect();
let used_fields: HashSet<&'a str> = acc.iter().map(|(s,_)| *s).collect();
let mut missing: Vec<(usize, HashSet<&'a str>)> =
(0..num_fields)
.filter(|x| !used_indices.contains(x))
.map(|x| (x, valid_fields[x].difference(&used_fields).cloned().collect()))
.collect();
missing.sort_by_key(|(_,vs)| vs.len());
for (i,vs) in missing.iter() {
for &f in vs {
acc.push((f,*i));
if find_assignment(notes, valid_fields, acc) {
return true;
}
acc.pop();
}
}
false
}
fn part2(notes: &Notes) -> i128 {
let v = valid_entries(notes);
let valid_nearby: Vec<&Ticket> = notes.nearby.iter().filter(|t| t.iter().all(|f| v.contains(f))).collect();
let num_fields = valid_nearby[0].len();
assert_eq!(num_fields, notes.fields.len());
let mut valid_fields = vec![notes.fields.keys().cloned().collect::<HashSet<&str>>(); num_fields];
for t in valid_nearby.iter() {
for i in 0..num_fields {
for f in notes.fields.keys() {
if !notes.fields[f].contains(&t[i]) {
valid_fields[i].remove(f);
}
}
}
}
let mut acc = vec![];
if !find_assignment(notes, &valid_fields, &mut acc) {
println!("ERROR: No valid assignment found!");
return -1;
}
let mut prod: i128 = 1;
for (f, i) in acc.iter() {
if f.starts_with("departure") {
prod *= notes.ticket[*i] as i128;
}
}
prod
}
fn main() {
let data = fs::read_to_string("input/input16.txt").unwrap();
let notes = parse_notes(&data);
println!("Part 1: {}", part1(¬es));
println!("Part 2: {}", part2(¬es));
}