1
0
Fork 0

day 10 solution

main
Andrew Coleman 2018-12-13 22:54:53 -05:00
parent 93e8aef6c0
commit 50eefc8230
4 changed files with 539 additions and 0 deletions

95
2018/day10/Cargo.lock generated Normal file
View File

@ -0,0 +1,95 @@
[[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 = "day10"
version = "0.1.0"
dependencies = [
"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 = "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 = "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"
[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 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 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 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"

8
2018/day10/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "day10"
version = "0.1.0"
authors = ["Andrew Coleman <penguincoder@gmail.com>"]
edition = "2018"
[dependencies]
regex = "1"

334
2018/day10/input Normal file
View File

@ -0,0 +1,334 @@
position=<-54217, -21587> velocity=< 5, 2>
position=<-54233, -10693> velocity=< 5, 1>
position=<-54217, -43356> velocity=< 5, 4>
position=<-32441, -43354> velocity=< 3, 4>
position=<-43345, -10690> velocity=< 4, 1>
position=< 21983, 54631> velocity=<-2, -5>
position=<-21566, 32857> velocity=< 2, -3>
position=< 43767, -54245> velocity=<-4, 5>
position=<-32473, 54631> velocity=< 3, -5>
position=< 11071, -21580> velocity=<-1, 2>
position=< 32903, -54247> velocity=<-3, 5>
position=<-43361, 54632> velocity=< 4, -5>
position=< 32857, -32472> velocity=<-3, 3>
position=< 32900, 21968> velocity=<-3, -2>
position=<-10688, -43354> velocity=< 1, 4>
position=<-43317, 11082> velocity=< 4, -1>
position=< 11079, -32469> velocity=<-1, 3>
position=< 11121, 21965> velocity=<-1, -2>
position=<-10669, -54246> velocity=< 1, 5>
position=< 43759, 32860> velocity=<-4, -3>
position=<-32441, 54630> velocity=< 3, -5>
position=< 21972, -21578> velocity=<-2, 2>
position=< 32847, -43355> velocity=<-3, 4>
position=< 11111, -21586> velocity=<-1, 2>
position=<-21585, 32859> velocity=< 2, -3>
position=< 32871, 54637> velocity=<-3, -5>
position=<-10671, -10699> velocity=< 1, 1>
position=<-10676, -43363> velocity=< 1, 4>
position=<-43333, -43358> velocity=< 4, 4>
position=< 21983, -54244> velocity=<-2, 5>
position=<-54217, 11081> velocity=< 5, -1>
position=<-43332, -21581> velocity=< 4, 2>
position=<-21581, -54250> velocity=< 2, 5>
position=<-43313, 21969> velocity=< 4, -2>
position=< 54643, 21965> velocity=<-5, -2>
position=<-32452, 11077> velocity=< 3, -1>
position=<-43343, 43741> velocity=< 4, -4>
position=< 32871, -54243> velocity=<-3, 5>
position=<-54204, 43750> velocity=< 5, -4>
position=< 54631, 32861> velocity=<-5, -3>
position=< 11103, 11079> velocity=<-1, -1>
position=< 54643, -32474> velocity=<-5, 3>
position=< 43767, 11084> velocity=<-4, -1>
position=< 32850, 11081> velocity=<-3, -1>
position=< 32855, 32861> velocity=<-3, -3>
position=< 54663, -43357> velocity=<-5, 4>
position=<-54252, -10690> velocity=< 5, 1>
position=< 11076, 21973> velocity=<-1, -2>
position=<-32447, -43363> velocity=< 3, 4>
position=<-21534, -32471> velocity=< 2, 3>
position=<-32461, -54245> velocity=< 3, 5>
position=< 54681, 43745> velocity=<-5, -4>
position=<-10694, -10697> velocity=< 1, 1>
position=< 32847, -32475> velocity=<-3, 3>
position=< 43776, -21583> velocity=<-4, 2>
position=<-43313, -43359> velocity=< 4, 4>
position=< 32865, -32466> velocity=<-3, 3>
position=<-54214, 11079> velocity=< 5, -1>
position=<-54239, -54242> velocity=< 5, 5>
position=<-10686, 21974> velocity=< 1, -2>
position=<-21593, -21578> velocity=< 2, 2>
position=<-43309, -10699> velocity=< 4, 1>
position=<-21561, -43354> velocity=< 2, 4>
position=<-10686, -21587> velocity=< 1, 2>
position=<-43364, -54243> velocity=< 4, 5>
position=<-43332, -10693> velocity=< 4, 1>
position=<-54204, -32469> velocity=< 5, 3>
position=<-54249, -43360> velocity=< 5, 4>
position=< 11076, 32853> velocity=<-1, -3>
position=< 21961, 11081> velocity=<-2, -1>
position=< 21996, -10695> velocity=<-2, 1>
position=<-21593, 32854> velocity=< 2, -3>
position=< 54668, -10690> velocity=<-5, 1>
position=< 32895, 43747> velocity=<-3, -4>
position=< 32875, -21583> velocity=<-3, 2>
position=< 11082, -10697> velocity=<-1, 1>
position=<-21589, 32857> velocity=< 2, -3>
position=< 11129, 54629> velocity=<-1, -5>
position=<-54209, 21967> velocity=< 5, -2>
position=< 32891, 21966> velocity=<-3, -2>
position=< 54655, 54631> velocity=<-5, -5>
position=< 11095, 32858> velocity=<-1, -3>
position=<-54233, -54244> velocity=< 5, 5>
position=< 54672, 54634> velocity=<-5, -5>
position=<-32481, -54247> velocity=< 3, 5>
position=< 11071, -21586> velocity=<-1, 2>
position=< 54643, 32861> velocity=<-5, -3>
position=< 43735, -54245> velocity=<-4, 5>
position=< 54647, 43744> velocity=<-5, -4>
position=<-32428, 32862> velocity=< 3, -3>
position=<-21561, 32862> velocity=< 2, -3>
position=< 22007, -54248> velocity=<-2, 5>
position=< 43783, -21580> velocity=<-4, 2>
position=< 43745, -43357> velocity=<-4, 4>
position=< 32847, -10693> velocity=<-3, 1>
position=< 11124, 11084> velocity=<-1, -1>
position=<-10700, -54245> velocity=< 1, 5>
position=<-21556, 54636> velocity=< 2, -5>
position=<-10700, 21966> velocity=< 1, -2>
position=< 22015, -43358> velocity=<-2, 4>
position=< 22015, -32475> velocity=<-2, 3>
position=< 21984, 54633> velocity=<-2, -5>
position=<-21545, -32471> velocity=< 2, 3>
position=< 21992, 54634> velocity=<-2, -5>
position=< 43762, -10699> velocity=<-4, 1>
position=<-32431, -43358> velocity=< 3, 4>
position=<-10656, 11078> velocity=< 1, -1>
position=<-32455, 11081> velocity=< 3, -1>
position=<-32455, -54247> velocity=< 3, 5>
position=< 43783, 43743> velocity=<-4, -4>
position=<-32428, 43743> velocity=< 3, -4>
position=< 11108, -10690> velocity=<-1, 1>
position=<-32428, -43357> velocity=< 3, 4>
position=<-54237, -21587> velocity=< 5, 2>
position=<-43360, 43745> velocity=< 4, -4>
position=< 21964, 32860> velocity=<-2, -3>
position=< 54641, 54638> velocity=<-5, -5>
position=< 11127, -32469> velocity=<-1, 3>
position=<-54200, 21969> velocity=< 5, -2>
position=<-43332, -43360> velocity=< 4, 4>
position=<-32452, 43741> velocity=< 3, -4>
position=< 32879, 21967> velocity=<-3, -2>
position=< 54663, 21966> velocity=<-5, -2>
position=<-21561, 21968> velocity=< 2, -2>
position=< 21972, 54629> velocity=<-2, -5>
position=< 32867, 21966> velocity=<-3, -2>
position=<-21573, -10696> velocity=< 2, 1>
position=<-43321, -54246> velocity=< 4, 5>
position=<-10705, -43358> velocity=< 1, 4>
position=<-10661, -54250> velocity=< 1, 5>
position=<-54205, -54246> velocity=< 5, 5>
position=< 21961, -32471> velocity=<-2, 3>
position=< 54651, 43745> velocity=<-5, -4>
position=<-21548, 54629> velocity=< 2, -5>
position=< 11111, 32855> velocity=<-1, -3>
position=<-54256, -10695> velocity=< 5, 1>
position=< 43791, 32861> velocity=<-4, -3>
position=< 11083, 43742> velocity=<-1, -4>
position=< 32895, -32469> velocity=<-3, 3>
position=<-21560, 43742> velocity=< 2, -4>
position=<-10670, 21970> velocity=< 1, -2>
position=< 54643, 32858> velocity=<-5, -3>
position=< 21967, -21578> velocity=<-2, 2>
position=< 43755, 54633> velocity=<-4, -5>
position=<-10693, -21579> velocity=< 1, 2>
position=<-43350, 54629> velocity=< 4, -5>
position=< 11124, -21580> velocity=<-1, 2>
position=<-43324, 11077> velocity=< 4, -1>
position=< 21979, -54246> velocity=<-2, 5>
position=<-43332, 21969> velocity=< 4, -2>
position=< 21991, -21580> velocity=<-2, 2>
position=<-54238, -43363> velocity=< 5, 4>
position=<-21569, 21970> velocity=< 2, -2>
position=< 43795, 21969> velocity=<-4, -2>
position=< 54647, -21585> velocity=<-5, 2>
position=< 43762, -43363> velocity=<-4, 4>
position=< 43743, -21587> velocity=<-4, 2>
position=< 11076, -21580> velocity=<-1, 2>
position=<-32461, -43355> velocity=< 3, 4>
position=< 54648, 11081> velocity=<-5, -1>
position=< 32904, -10699> velocity=<-3, 1>
position=< 54647, -43362> velocity=<-5, 4>
position=< 21993, 11077> velocity=<-2, -1>
position=<-10652, -54248> velocity=< 1, 5>
position=< 22007, 21970> velocity=<-2, -2>
position=<-54216, 21970> velocity=< 5, -2>
position=<-32425, -10692> velocity=< 3, 1>
position=< 54623, -43360> velocity=<-5, 4>
position=<-21573, -32469> velocity=< 2, 3>
position=<-43327, 11083> velocity=< 4, -1>
position=< 43775, 32862> velocity=<-4, -3>
position=<-21584, 54634> velocity=< 2, -5>
position=< 43740, -54251> velocity=<-4, 5>
position=<-21553, 21968> velocity=< 2, -2>
position=<-32461, -54244> velocity=< 3, 5>
position=<-10697, -21582> velocity=< 1, 2>
position=< 54625, -54247> velocity=<-5, 5>
position=<-43316, -32467> velocity=< 4, 3>
position=< 54663, -43354> velocity=<-5, 4>
position=< 32884, -54242> velocity=<-3, 5>
position=<-54220, -10697> velocity=< 5, 1>
position=< 11128, 43745> velocity=<-1, -4>
position=< 54647, 43742> velocity=<-5, -4>
position=< 54647, 43744> velocity=<-5, -4>
position=< 43740, 54632> velocity=<-4, -5>
position=< 43783, 54637> velocity=<-4, -5>
position=< 11075, 54633> velocity=<-1, -5>
position=< 43791, 54631> velocity=<-4, -5>
position=< 11074, 11081> velocity=<-1, -1>
position=< 54679, -10699> velocity=<-5, 1>
position=< 54651, 21965> velocity=<-5, -2>
position=< 43735, -43361> velocity=<-4, 4>
position=< 43785, -43358> velocity=<-4, 4>
position=<-32456, -32471> velocity=< 3, 3>
position=<-32441, 32861> velocity=< 3, -3>
position=< 54655, -10694> velocity=<-5, 1>
position=< 22010, -10699> velocity=<-2, 1>
position=< 11124, -43361> velocity=<-1, 4>
position=<-32461, -54250> velocity=< 3, 5>
position=<-54204, -43359> velocity=< 5, 4>
position=< 43778, -10692> velocity=<-4, 1>
position=<-43328, -21583> velocity=< 4, 2>
position=< 43755, 54632> velocity=<-4, -5>
position=<-10646, -43363> velocity=< 1, 4>
position=< 21979, 11080> velocity=<-2, -1>
position=< 43786, 43746> velocity=<-4, -4>
position=<-32425, 43744> velocity=< 3, -4>
position=<-10652, -43358> velocity=< 1, 4>
position=<-21537, 54632> velocity=< 2, -5>
position=< 11071, 21965> velocity=<-1, -2>
position=< 11115, 32854> velocity=<-1, -3>
position=< 11116, 11077> velocity=<-1, -1>
position=<-32441, 54637> velocity=< 3, -5>
position=< 43746, -10692> velocity=<-4, 1>
position=< 21964, -32470> velocity=<-2, 3>
position=<-32473, -43356> velocity=< 3, 4>
position=<-10697, -54246> velocity=< 1, 5>
position=<-21569, 54637> velocity=< 2, -5>
position=< 54656, -10698> velocity=<-5, 1>
position=<-54217, 54632> velocity=< 5, -5>
position=<-32439, 21971> velocity=< 3, -2>
position=< 11107, 32854> velocity=<-1, -3>
position=<-54215, -10696> velocity=< 5, 1>
position=<-10646, 32857> velocity=< 1, -3>
position=<-32444, 32862> velocity=< 3, -3>
position=<-21593, 11084> velocity=< 2, -1>
position=< 32882, -32475> velocity=<-3, 3>
position=<-54248, -43358> velocity=< 5, 4>
position=< 32851, 11081> velocity=<-3, -1>
position=<-10700, 43743> velocity=< 1, -4>
position=<-21565, 32853> velocity=< 2, -3>
position=<-32447, -21582> velocity=< 3, 2>
position=<-43349, 54636> velocity=< 4, -5>
position=< 54676, -54249> velocity=<-5, 5>
position=< 32897, -10699> velocity=<-3, 1>
position=<-43316, -54247> velocity=< 4, 5>
position=<-21593, -32471> velocity=< 2, 3>
position=< 43740, -10695> velocity=<-4, 1>
position=<-43312, 43745> velocity=< 4, -4>
position=< 21999, -43356> velocity=<-2, 4>
position=<-54220, 32860> velocity=< 5, -3>
position=< 22015, -32469> velocity=<-2, 3>
position=< 21980, 43741> velocity=<-2, -4>
position=< 32884, 11080> velocity=<-3, -1>
position=< 43760, -21587> velocity=<-4, 2>
position=<-43349, 32858> velocity=< 4, -3>
position=< 21991, -21579> velocity=<-2, 2>
position=<-43353, -43355> velocity=< 4, 4>
position=<-32421, -21583> velocity=< 3, 2>
position=< 43788, -21578> velocity=<-4, 2>
position=<-54233, -21587> velocity=< 5, 2>
position=< 43788, -43355> velocity=<-4, 4>
position=<-54225, 54636> velocity=< 5, -5>
position=<-54225, -32471> velocity=< 5, 3>
position=< 54631, -32473> velocity=<-5, 3>
position=< 11111, 21970> velocity=<-1, -2>
position=<-21550, -21580> velocity=< 2, 2>
position=<-32456, 32853> velocity=< 3, -3>
position=<-43327, -43360> velocity=< 4, 4>
position=<-10685, -10691> velocity=< 1, 1>
position=< 43759, 32856> velocity=<-4, -3>
position=< 32887, 21969> velocity=<-3, -2>
position=< 43743, -32473> velocity=<-4, 3>
position=< 32847, 54635> velocity=<-3, -5>
position=<-54198, -32475> velocity=< 5, 3>
position=<-21545, -43359> velocity=< 2, 4>
position=< 22007, -32471> velocity=<-2, 3>
position=<-21537, -43363> velocity=< 2, 4>
position=< 21994, -21587> velocity=<-2, 2>
position=< 54623, -54250> velocity=<-5, 5>
position=< 21983, -21583> velocity=<-2, 2>
position=<-21556, -10692> velocity=< 2, 1>
position=< 54628, 11080> velocity=<-5, -1>
position=< 11127, -54242> velocity=<-1, 5>
position=< 11079, -54250> velocity=<-1, 5>
position=< 43767, -43354> velocity=<-4, 4>
position=< 32884, 21969> velocity=<-3, -2>
position=< 54631, 21966> velocity=<-5, -2>
position=< 11132, -32475> velocity=<-1, 3>
position=<-10645, 21965> velocity=< 1, -2>
position=<-10697, 54629> velocity=< 1, -5>
position=< 11115, 43749> velocity=<-1, -4>
position=< 54676, 32857> velocity=<-5, -3>
position=<-21533, -10695> velocity=< 2, 1>
position=<-32455, -10699> velocity=< 3, 1>
position=<-10649, -32474> velocity=< 1, 3>
position=<-21569, -21581> velocity=< 2, 2>
position=< 32863, -54244> velocity=<-3, 5>
position=<-32425, -43362> velocity=< 3, 4>
position=<-10652, 54632> velocity=< 1, -5>
position=< 43792, 11077> velocity=<-4, -1>
position=< 21996, 21973> velocity=<-2, -2>
position=<-21556, -10696> velocity=< 2, 1>
position=<-10700, -54242> velocity=< 1, 5>
position=< 54672, -43358> velocity=<-5, 4>
position=<-10660, 54638> velocity=< 1, -5>
position=<-21560, 54634> velocity=< 2, -5>
position=<-10697, -10695> velocity=< 1, 1>
position=< 11079, 32854> velocity=<-1, -3>
position=<-54220, -10694> velocity=< 5, 1>
position=<-54252, -10694> velocity=< 5, 1>
position=< 43759, 54631> velocity=<-4, -5>
position=<-54249, 11083> velocity=< 5, -1>
position=< 21979, 11079> velocity=<-2, -1>
position=< 21983, 54629> velocity=<-2, -5>
position=<-54257, 43750> velocity=< 5, -4>
position=< 32852, 11084> velocity=<-3, -1>
position=< 32852, 32853> velocity=<-3, -3>
position=<-21585, -43358> velocity=< 2, 4>
position=<-54244, -32475> velocity=< 5, 3>
position=< 21983, 21974> velocity=<-2, -2>
position=< 32871, -54242> velocity=<-3, 5>
position=< 21991, -43357> velocity=<-2, 4>
position=<-54248, 54634> velocity=< 5, -5>
position=< 32882, -21587> velocity=<-3, 2>
position=< 43796, -10699> velocity=<-4, 1>
position=< 11114, -21580> velocity=<-1, 2>
position=<-10688, -21578> velocity=< 1, 2>
position=<-10700, 11082> velocity=< 1, -1>
position=<-43327, 11080> velocity=< 4, -1>
position=< 54660, -43357> velocity=<-5, 4>
position=< 11099, 21969> velocity=<-1, -2>
position=<-43353, -54244> velocity=< 4, 5>
position=<-10653, 43742> velocity=< 1, -4>
position=< 21994, -10694> velocity=<-2, 1>
position=< 43740, -10691> velocity=<-4, 1>
position=< 32871, -10695> velocity=<-3, 1>
position=< 32900, 54635> velocity=<-3, -5>
position=<-21545, -21578> velocity=< 2, 2>
position=< 43767, 11081> velocity=<-4, -1>
position=<-54244, -32475> velocity=< 5, 3>
position=<-32454, -21587> velocity=< 3, 2>
position=<-32422, -21587> velocity=< 3, 2>
position=<-43361, -32468> velocity=< 4, 3>

102
2018/day10/src/main.rs Normal file
View File

@ -0,0 +1,102 @@
extern crate regex;
use regex::Regex;
use std::collections::HashSet;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::iter::Iterator;
#[derive(Debug)]
struct Point {
x: i32,
y: i32,
x_velocity: i32,
y_velocity: i32
}
fn bounding_box(points: &Vec<Point>) -> (i32, i32, i32, i32) {
let mut min_x = std::i32::MAX;
let mut min_y = std::i32::MAX;
let mut max_x = std::i32::MIN;
let mut max_y = std::i32::MIN;
for p in points.iter() {
if min_x > p.x { min_x = p.x };
if min_y > p.y { min_y = p.y };
if max_x < p.x { max_x = p.x };
if max_y < p.y { max_y = p.y };
}
(min_x, min_y, max_x, max_y)
}
fn tick(points: &mut Vec<Point>) -> i32 {
let mut seconds = 0;
let mut old_height = std::i32::MAX;
loop {
let (_min_x, min_y, _max_x, max_y) = bounding_box(points);
let new_height = max_y - min_y;
if new_height > old_height {
for p in points.iter_mut() {
p.x -= p.x_velocity;
p.y -= p.y_velocity;
}
seconds -= 1;
break;
}
old_height = new_height;
seconds += 1;
for p in points.iter_mut() {
p.x += p.x_velocity;
p.y += p.y_velocity;
}
}
let (min_x, min_y, max_x, max_y) = bounding_box(points);
println!("final bounding box: ({}, {}) ({}, {})", min_x, min_y, max_x, max_y);
let mut final_set = HashSet::with_capacity(points.len());
for p in points.iter() {
final_set.insert((p.x, p.y));
}
for j in min_y..=max_y {
for i in min_x..=max_x {
if final_set.contains(&(i, j)) {
print!("#")
} else {
print!(".")
};
}
println!("");
}
seconds
}
fn main() {
let lines: Vec<String> = BufReader::new(File::open("input").unwrap())
.lines()
.map(|line| line.unwrap())
.collect();
let mut points: Vec<Point> = Vec::with_capacity(lines.len());
let re = Regex::new(r"^position=<([-| ]\d+), ([-| ]\d+)> velocity=<([-| ]\d), ([-| ]\d)>$").unwrap();
for line in lines.iter() {
if re.is_match(line) {
let cap = re.captures(line).unwrap();
let x = cap[1].trim_start().parse::<i32>().unwrap();
let y = cap[2].trim_start().parse::<i32>().unwrap();
let x_velocity = cap[3].trim_start().parse::<i32>().unwrap();
let y_velocity = cap[4].trim_start().parse::<i32>().unwrap();
points.push(Point {
x,
y,
x_velocity,
y_velocity
});
}
}
let seconds = tick(&mut points);
println!("time elapsed: {}", seconds);
}