blob: 3773be8ef55fede5f2794becc1ab52b808027bc3 (
plain)
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
|
use advent_lib::prelude::*;
use std::collections::BTreeSet;
fn main() -> Result<()> {
let mut args = std::env::args();
if args.len() != 2 {
eprintln!("Usage: advent input");
}
let _ = args.next();
let filename = args.next().unwrap();
let input = advent_lib::read_lines_file(&filename)?;
let mut max_id = 0;
let mut found_ids = BTreeSet::new();
for line in &input {
let (row, column) = decode_seat(&line);
let id = seat_id(row, column);
found_ids.insert(id);
if id > max_id {
max_id = id;
}
}
println!("{}", max_id);
let mut is_started = false;
for i in 0 .. max_id {
if !is_started {
if found_ids.contains(&i) {
is_started = true;
}
} else {
if !found_ids.contains(&i) {
println!("{}", i);
break;
}
}
}
Ok(())
}
fn decode_seat(seat_spec: &str) -> (i64, i64) {
let mut row: i64 = 0;
let mut column: i64 = 0;
if seat_spec.chars().nth(0) == Some('B') {
row += 64;
}
if seat_spec.chars().nth(1) == Some('B') {
row += 32;
}
if seat_spec.chars().nth(2) == Some('B') {
row += 16;
}
if seat_spec.chars().nth(3) == Some('B') {
row += 8;
}
if seat_spec.chars().nth(4) == Some('B') {
row += 4;
}
if seat_spec.chars().nth(5) == Some('B') {
row += 2;
}
if seat_spec.chars().nth(6) == Some('B') {
row += 1;
}
if seat_spec.chars().nth(7) == Some('R') {
column += 4;
}
if seat_spec.chars().nth(8) == Some('R') {
column += 2;
}
if seat_spec.chars().nth(9) == Some('R') {
column += 1;
}
(row, column)
}
fn seat_id(row: i64, column: i64) -> i64 {
row * 8 + column
}
|