Merge branch 'master' of code.aceofba.se:logaritmisk/adventofcode
This commit is contained in:
161
2018/04/Cargo.lock
generated
Normal file
161
2018/04/Cargo.lock
generated
Normal file
@@ -0,0 +1,161 @@
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "0.6.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"time 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day-04"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.39"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.1.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"regex-syntax 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread_local"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.1.41"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"redox_syscall 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ucd-util"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "utf8-ranges"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[metadata]
|
||||
"checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e"
|
||||
"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
|
||||
"checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878"
|
||||
"checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1"
|
||||
"checksum libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)" = "2d2857ec59fadc0773853c664d2d18e7198e83883e7060b63c924cb077bd5c74"
|
||||
"checksum memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "db4c41318937f6e76648f42826b1d9ade5c09cafb5aef7e351240a70f39206e9"
|
||||
"checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea"
|
||||
"checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1"
|
||||
"checksum redox_syscall 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)" = "a84bcd297b87a545980a2d25a0beb72a1f490c31f0a9fde52fca35bfbb1ceb70"
|
||||
"checksum regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "37e7cbbd370869ce2e8dff25c7018702d10b21a20ef7135316f8daecd6c25b7f"
|
||||
"checksum regex-syntax 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4e47a2ed29da7a9e1960e1639e7a982e6edc6d49be308a3b02daf511504a16d1"
|
||||
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
|
||||
"checksum time 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "847da467bf0db05882a9e2375934a8a55cffdc9db0d128af1518200260ba1f6c"
|
||||
"checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86"
|
||||
"checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737"
|
||||
"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
|
||||
"checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0"
|
||||
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
9
2018/04/Cargo.toml
Normal file
9
2018/04/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "day-04"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
chrono = "0.4"
|
||||
regex = "1.1"
|
||||
1170
2018/04/input.txt
Normal file
1170
2018/04/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
176
2018/04/src/main.rs
Normal file
176
2018/04/src/main.rs
Normal file
@@ -0,0 +1,176 @@
|
||||
use std::collections::HashMap;
|
||||
use std::io::{self, BufRead};
|
||||
|
||||
use chrono::prelude::*;
|
||||
use regex::{Captures, Regex};
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Entry {
|
||||
date: DateTime<Utc>,
|
||||
event: Event,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Event {
|
||||
BeginShift { guard_id: u16 },
|
||||
FallsAsleep,
|
||||
WakesUp,
|
||||
}
|
||||
|
||||
enum State {
|
||||
Default,
|
||||
Guard(u16),
|
||||
FallsAsleep(u16, DateTime<Utc>),
|
||||
}
|
||||
|
||||
fn date_from_captures(caps: &Captures<'_>) -> DateTime<Utc> {
|
||||
let year = caps
|
||||
.get(1)
|
||||
.expect("faild to find year")
|
||||
.as_str()
|
||||
.parse::<i32>()
|
||||
.expect("faild to parse year");
|
||||
|
||||
let month = caps
|
||||
.get(2)
|
||||
.expect("faild to find month")
|
||||
.as_str()
|
||||
.parse::<u32>()
|
||||
.expect("faild to parse month");
|
||||
|
||||
let day = caps
|
||||
.get(3)
|
||||
.expect("failed to find day")
|
||||
.as_str()
|
||||
.parse::<u32>()
|
||||
.expect("faild to parse day");
|
||||
|
||||
let hour = caps
|
||||
.get(4)
|
||||
.expect("faild to find hour")
|
||||
.as_str()
|
||||
.parse::<u32>()
|
||||
.expect("faild to parse hour");
|
||||
|
||||
let minute = caps
|
||||
.get(5)
|
||||
.expect("failed to find minute")
|
||||
.as_str()
|
||||
.parse::<u32>()
|
||||
.expect("faild to parse minute");
|
||||
|
||||
Utc.ymd(year, month, day).and_hms(hour, minute, 0)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let re_begin_shift =
|
||||
Regex::new(r"\[(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})\] Guard #(\d+) begins shift")
|
||||
.expect("failed to build regex");
|
||||
|
||||
let re_falls_asleep = Regex::new(r"\[(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})\] falls asleep")
|
||||
.expect("failed to build regex");
|
||||
|
||||
let re_wakes_up = Regex::new(r"\[(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})\] wakes up")
|
||||
.expect("failed to build regex");
|
||||
|
||||
let mut entries = io::stdin()
|
||||
.lock()
|
||||
.lines()
|
||||
.filter_map(Result::ok)
|
||||
.map(|line| {
|
||||
if let Some(caps) = re_begin_shift.captures(&line) {
|
||||
Entry {
|
||||
date: date_from_captures(&caps),
|
||||
event: Event::BeginShift {
|
||||
guard_id: caps
|
||||
.get(6)
|
||||
.expect("failed to find y")
|
||||
.as_str()
|
||||
.parse::<u16>()
|
||||
.expect("faild to parse y"),
|
||||
},
|
||||
}
|
||||
} else if let Some(caps) = re_falls_asleep.captures(&line) {
|
||||
Entry {
|
||||
date: date_from_captures(&caps),
|
||||
event: Event::FallsAsleep,
|
||||
}
|
||||
} else if let Some(caps) = re_wakes_up.captures(&line) {
|
||||
Entry {
|
||||
date: date_from_captures(&caps),
|
||||
event: Event::WakesUp,
|
||||
}
|
||||
} else {
|
||||
panic!("failed to parse line: {}", line);
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
entries.sort_by_key(|entry| entry.date);
|
||||
|
||||
let entries = entries;
|
||||
|
||||
let mut minute_asleep = HashMap::new();
|
||||
let mut minutes_asleep = HashMap::new();
|
||||
|
||||
let mut state = State::Default;
|
||||
|
||||
for entry in &entries {
|
||||
match (state, &entry.event) {
|
||||
// Default.
|
||||
(State::Default, Event::BeginShift { guard_id }) => {
|
||||
state = State::Guard(*guard_id);
|
||||
}
|
||||
(State::Default, _) => panic!("oh no"),
|
||||
|
||||
// Guard.
|
||||
(State::Guard(_), Event::BeginShift { guard_id }) => state = State::Guard(*guard_id),
|
||||
(State::Guard(guard_id), Event::FallsAsleep) => {
|
||||
state = State::FallsAsleep(guard_id, entry.date);
|
||||
}
|
||||
(State::Guard(_), _) => panic!("oh no"),
|
||||
|
||||
// FallsAsleep.
|
||||
(State::FallsAsleep(guard_id, date), Event::WakesUp) => {
|
||||
state = State::Guard(guard_id);
|
||||
|
||||
for m in date.minute()..entry.date.minute() {
|
||||
minute_asleep
|
||||
.entry((guard_id, m))
|
||||
.and_modify(|x| *x += 1)
|
||||
.or_insert(1);
|
||||
}
|
||||
|
||||
let duration = entry.date - date;
|
||||
|
||||
minutes_asleep
|
||||
.entry(guard_id)
|
||||
.and_modify(|x| *x += duration.num_seconds())
|
||||
.or_insert(0);
|
||||
}
|
||||
(State::FallsAsleep(_, _), _) => panic!("oh no"),
|
||||
}
|
||||
}
|
||||
|
||||
let (guard_id, minutes) = minutes_asleep
|
||||
.iter()
|
||||
.max_by_key(|(_, asleep)| *asleep)
|
||||
.expect("failed to find guard");
|
||||
|
||||
let (minute, _) = minute_asleep
|
||||
.iter()
|
||||
.filter(|((guard, _), _)| guard == guard_id)
|
||||
.map(|((_, minute), total)| (minute, total))
|
||||
.max_by_key(|(minute, total)| *total)
|
||||
.expect("failed to find minute");
|
||||
|
||||
println!("part.one={}", *guard_id as u32 * *minute);
|
||||
|
||||
let (guard_id, minute, _) = minute_asleep
|
||||
.iter()
|
||||
.map(|((guard, minute), total)| (guard, minute, total))
|
||||
.max_by_key(|(guard, minute, total)| *total)
|
||||
.expect("failed to find guard and minute");
|
||||
|
||||
println!("part.two={}", *guard_id as u32 * *minute);
|
||||
}
|
||||
4
2018/05/Cargo.lock
generated
Normal file
4
2018/05/Cargo.lock
generated
Normal file
@@ -0,0 +1,4 @@
|
||||
[[package]]
|
||||
name = "day-05"
|
||||
version = "0.1.0"
|
||||
|
||||
7
2018/05/Cargo.toml
Normal file
7
2018/05/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day-05"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
1
2018/05/example-01.txt
Normal file
1
2018/05/example-01.txt
Normal file
@@ -0,0 +1 @@
|
||||
dabAcCaCBAcCcaDA
|
||||
1
2018/05/input.txt
Normal file
1
2018/05/input.txt
Normal file
File diff suppressed because one or more lines are too long
88
2018/05/src/main.rs
Normal file
88
2018/05/src/main.rs
Normal file
@@ -0,0 +1,88 @@
|
||||
use std::collections::HashSet;
|
||||
use std::io::{self, BufRead};
|
||||
|
||||
fn react<I>(polymer: I) -> String
|
||||
where
|
||||
I: Iterator<Item = char>,
|
||||
{
|
||||
let mut input = polymer.collect::<Vec<char>>();
|
||||
|
||||
loop {
|
||||
let remove = input
|
||||
.windows(2)
|
||||
.enumerate()
|
||||
.filter(|(_, chars)| {
|
||||
assert!(chars.len() == 2);
|
||||
|
||||
let left = chars[0];
|
||||
let right = if chars[1].is_uppercase() {
|
||||
chars[1].to_ascii_lowercase()
|
||||
} else {
|
||||
chars[1].to_ascii_uppercase()
|
||||
};
|
||||
|
||||
left == right
|
||||
})
|
||||
.map(|(i, _)| i)
|
||||
.scan(None, |state, i| {
|
||||
if let Some(n) = state {
|
||||
let prev = *n + 1;
|
||||
|
||||
*n = i;
|
||||
|
||||
if prev != i {
|
||||
Some(i)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
*state = Some(i);
|
||||
|
||||
Some(i)
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
if remove.is_empty() {
|
||||
break;
|
||||
}
|
||||
|
||||
input = input
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.filter(|(i, _)| !remove.contains(i) && !remove.contains(&(i - 1)))
|
||||
.map(|(_, c)| c)
|
||||
.collect::<Vec<_>>();
|
||||
}
|
||||
|
||||
input.into_iter().collect::<String>()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let input = io::stdin()
|
||||
.lock()
|
||||
.lines()
|
||||
.filter_map(Result::ok)
|
||||
.collect::<String>();
|
||||
|
||||
println!("part.one={}", react(input.chars()).len());
|
||||
|
||||
let units = input
|
||||
.chars()
|
||||
.map(|c| c.to_ascii_lowercase())
|
||||
.collect::<HashSet<_>>();
|
||||
|
||||
let min = units
|
||||
.into_iter()
|
||||
.map(|u| {
|
||||
let stream = input
|
||||
.chars()
|
||||
.filter(|c| *c != u && *c != u.to_ascii_uppercase());
|
||||
|
||||
react(stream).len()
|
||||
})
|
||||
.min()
|
||||
.expect("failed to find shortest polymer");
|
||||
|
||||
println!("part.two={}", min);
|
||||
}
|
||||
4
2018/06/Cargo.lock
generated
Normal file
4
2018/06/Cargo.lock
generated
Normal file
@@ -0,0 +1,4 @@
|
||||
[[package]]
|
||||
name = "day-06"
|
||||
version = "0.1.0"
|
||||
|
||||
7
2018/06/Cargo.toml
Normal file
7
2018/06/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day-06"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
6
2018/06/example-01.txt
Normal file
6
2018/06/example-01.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
1, 1
|
||||
1, 6
|
||||
8, 3
|
||||
3, 4
|
||||
5, 5
|
||||
8, 9
|
||||
50
2018/06/input.txt
Normal file
50
2018/06/input.txt
Normal file
@@ -0,0 +1,50 @@
|
||||
194, 200
|
||||
299, 244
|
||||
269, 329
|
||||
292, 55
|
||||
211, 63
|
||||
123, 311
|
||||
212, 90
|
||||
292, 169
|
||||
359, 177
|
||||
354, 95
|
||||
101, 47
|
||||
95, 79
|
||||
95, 287
|
||||
294, 126
|
||||
81, 267
|
||||
330, 78
|
||||
202, 165
|
||||
225, 178
|
||||
266, 272
|
||||
351, 326
|
||||
180, 62
|
||||
102, 178
|
||||
151, 101
|
||||
343, 145
|
||||
205, 312
|
||||
74, 193
|
||||
221, 56
|
||||
89, 89
|
||||
242, 172
|
||||
59, 138
|
||||
83, 179
|
||||
223, 88
|
||||
297, 234
|
||||
147, 351
|
||||
226, 320
|
||||
358, 338
|
||||
321, 172
|
||||
54, 122
|
||||
263, 165
|
||||
126, 341
|
||||
64, 132
|
||||
264, 306
|
||||
72, 202
|
||||
98, 49
|
||||
238, 67
|
||||
310, 303
|
||||
277, 281
|
||||
222, 318
|
||||
357, 169
|
||||
123, 225
|
||||
120
2018/06/src/main.rs
Normal file
120
2018/06/src/main.rs
Normal file
@@ -0,0 +1,120 @@
|
||||
use std::collections::HashMap;
|
||||
use std::io::{self, BufRead};
|
||||
|
||||
fn distance(a: (i32, i32), b: (i32, i32)) -> i32 {
|
||||
(a.0 - b.0).abs() + (a.1 - b.1).abs()
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum State {
|
||||
Empty,
|
||||
Owner(usize, i32),
|
||||
Equal(i32),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum Area {
|
||||
Infinite,
|
||||
Finite(usize),
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let coordinates = io::stdin()
|
||||
.lock()
|
||||
.lines()
|
||||
.filter_map(Result::ok)
|
||||
.map(|line| {
|
||||
let coords = line
|
||||
.split(", ")
|
||||
.map(|coord| coord.parse::<i32>().unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
(coords[0], coords[1])
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let width = coordinates
|
||||
.iter()
|
||||
.map(|(x, _)| *x as usize)
|
||||
.max()
|
||||
.expect("failed to find max x")
|
||||
+ 1;
|
||||
|
||||
let height = coordinates
|
||||
.iter()
|
||||
.map(|(_, y)| *y as usize)
|
||||
.max()
|
||||
.expect("failed to find max y")
|
||||
+ 1;
|
||||
|
||||
let mut grid_view = vec![State::Empty; width * height].into_boxed_slice();
|
||||
|
||||
for x in 0..width {
|
||||
for y in 0..height {
|
||||
for (i, c) in coordinates.iter().enumerate() {
|
||||
let distance = distance(*c, (x as i32, y as i32));
|
||||
let state = &mut grid_view[x + (y * width)];
|
||||
|
||||
*state = match state {
|
||||
State::Empty => State::Owner(i, distance),
|
||||
State::Owner(_, d) if *d > distance => State::Owner(i, distance),
|
||||
State::Owner(o, d) if *d == distance && *o != i => State::Equal(distance),
|
||||
State::Equal(d) if *d > distance => State::Owner(i, distance),
|
||||
_ => *state,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut areas = HashMap::new();
|
||||
|
||||
for x in 0..width {
|
||||
for y in 0..height {
|
||||
if let State::Owner(o, _) = grid_view[x + (y * width)] {
|
||||
let a = areas.entry(o).or_insert(Area::Finite(0));
|
||||
|
||||
if x == 0 || x == width - 1 || y == 0 || y == height - 1 {
|
||||
*a = Area::Infinite;
|
||||
} else {
|
||||
match a {
|
||||
Area::Infinite => (),
|
||||
Area::Finite(area) => *area += 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let (_, area) = areas
|
||||
.iter()
|
||||
.filter_map(|(i, area)| {
|
||||
if let Area::Finite(area) = area {
|
||||
Some((i, area))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.max_by_key(|(_, area)| *area)
|
||||
.expect("failed to find max area");
|
||||
|
||||
println!("part.one={}", area);
|
||||
|
||||
let mut grid_view = vec!['.'; width * height].into_boxed_slice();
|
||||
|
||||
for y in 0..height {
|
||||
for x in 0..width {
|
||||
let total = coordinates
|
||||
.iter()
|
||||
.map(|c| distance(*c, (x as i32, y as i32)))
|
||||
.sum::<i32>();
|
||||
|
||||
if total < 10_000 {
|
||||
grid_view[x + (y * width)] = '#';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let area = grid_view.iter().filter(|r| **r == '#').count();
|
||||
|
||||
println!("part.two={}", area);
|
||||
}
|
||||
7
2018/07/Cargo.toml
Normal file
7
2018/07/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day-07"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
3
2018/07/src/main.rs
Normal file
3
2018/07/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
7
2018/08/Cargo.toml
Normal file
7
2018/08/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day-08"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
3
2018/08/src/main.rs
Normal file
3
2018/08/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
7
2018/09/Cargo.toml
Normal file
7
2018/09/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day-09"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
3
2018/09/src/main.rs
Normal file
3
2018/09/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
7
2018/10/Cargo.toml
Normal file
7
2018/10/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day-10"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
3
2018/10/src/main.rs
Normal file
3
2018/10/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
7
2018/11/Cargo.toml
Normal file
7
2018/11/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day-11"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
3
2018/11/src/main.rs
Normal file
3
2018/11/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
7
2018/12/Cargo.toml
Normal file
7
2018/12/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day-12"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
3
2018/12/src/main.rs
Normal file
3
2018/12/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
7
2018/13/Cargo.toml
Normal file
7
2018/13/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day-13"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
3
2018/13/src/main.rs
Normal file
3
2018/13/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
7
2018/14/Cargo.toml
Normal file
7
2018/14/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day-14"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
3
2018/14/src/main.rs
Normal file
3
2018/14/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
7
2018/15/Cargo.toml
Normal file
7
2018/15/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day-15"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
3
2018/15/src/main.rs
Normal file
3
2018/15/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
7
2018/16/Cargo.toml
Normal file
7
2018/16/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day-16"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
3
2018/16/src/main.rs
Normal file
3
2018/16/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
7
2018/17/Cargo.toml
Normal file
7
2018/17/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day-17"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
3
2018/17/src/main.rs
Normal file
3
2018/17/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
7
2018/18/Cargo.toml
Normal file
7
2018/18/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day-18"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
3
2018/18/src/main.rs
Normal file
3
2018/18/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
7
2018/19/Cargo.toml
Normal file
7
2018/19/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day-19"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
3
2018/19/src/main.rs
Normal file
3
2018/19/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
7
2018/20/Cargo.toml
Normal file
7
2018/20/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day-20"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
3
2018/20/src/main.rs
Normal file
3
2018/20/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
7
2018/21/Cargo.toml
Normal file
7
2018/21/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day-21"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
3
2018/21/src/main.rs
Normal file
3
2018/21/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
7
2018/22/Cargo.toml
Normal file
7
2018/22/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day-22"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
3
2018/22/src/main.rs
Normal file
3
2018/22/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
7
2018/23/Cargo.toml
Normal file
7
2018/23/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day-23"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
3
2018/23/src/main.rs
Normal file
3
2018/23/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
7
2018/24/Cargo.toml
Normal file
7
2018/24/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day-24"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
3
2018/24/src/main.rs
Normal file
3
2018/24/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
7
2018/25/Cargo.toml
Normal file
7
2018/25/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day-25"
|
||||
version = "0.1.0"
|
||||
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
3
2018/25/src/main.rs
Normal file
3
2018/25/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
Reference in New Issue
Block a user