This commit is contained in:
Acvaxoort 2023-12-18 17:14:10 +01:00
parent 865f4cf564
commit 049a2181ad
4 changed files with 908 additions and 0 deletions

7
day18/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day18"
version = "0.1.0"

8
day18/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "day18"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

712
day18/input.txt Normal file
View File

@ -0,0 +1,712 @@
L 4 (#38ce32)
U 3 (#55f513)
L 6 (#46c2c2)
U 3 (#2c5aa3)
R 7 (#3137a2)
U 4 (#3bc623)
R 6 (#924a52)
D 4 (#2880c3)
R 7 (#924a50)
U 6 (#639303)
R 5 (#3137a0)
U 3 (#6ada33)
L 6 (#3f9e92)
U 4 (#2f1fa3)
R 6 (#471352)
U 4 (#2fd2a3)
L 5 (#09d272)
U 4 (#213a13)
L 6 (#78b222)
U 3 (#2fc1a3)
L 3 (#4cbb52)
U 3 (#4ac623)
R 4 (#70efe2)
U 2 (#5eb1f3)
R 5 (#70efe0)
U 3 (#2bf113)
L 6 (#1882e2)
U 3 (#3fca23)
R 4 (#62ba80)
U 5 (#2c54c3)
L 4 (#1dee90)
U 6 (#73a233)
R 5 (#2c7800)
U 4 (#4ad3a3)
L 7 (#4f8940)
U 3 (#48d041)
R 7 (#5221c0)
U 4 (#75a591)
R 2 (#03ec80)
U 4 (#2c54c1)
R 4 (#1bbc10)
D 4 (#4f1b13)
R 4 (#7f90f0)
D 5 (#2752d3)
R 4 (#1eae30)
D 3 (#2a9653)
L 6 (#36eb10)
D 2 (#2a9651)
L 2 (#454ad0)
D 3 (#1557e3)
R 3 (#7707d0)
D 4 (#73c6b3)
R 5 (#295690)
D 6 (#24a233)
R 3 (#7bc852)
U 6 (#15b743)
R 3 (#224732)
U 4 (#4a0323)
R 4 (#810412)
U 7 (#5fba61)
L 4 (#1c2ee2)
U 4 (#6977f3)
R 4 (#3ff1b0)
U 6 (#8ee1f3)
R 4 (#544e00)
U 4 (#244c23)
R 2 (#5bc1f0)
U 4 (#1e5873)
L 3 (#6420d2)
U 4 (#836a83)
R 6 (#6420d0)
U 3 (#024693)
L 6 (#0a2540)
U 4 (#65eda3)
L 2 (#2649e0)
U 2 (#195ba3)
L 4 (#74d120)
U 3 (#795ca3)
R 3 (#31c470)
U 2 (#59afe3)
R 6 (#873020)
U 4 (#695263)
L 9 (#1dc2b0)
U 3 (#288a33)
L 4 (#090860)
D 8 (#00eb93)
L 4 (#5de760)
D 2 (#00eb91)
L 3 (#5ae5c0)
D 4 (#162813)
L 3 (#182e50)
D 5 (#5848f1)
L 2 (#89c410)
D 6 (#5848f3)
L 4 (#549dc0)
D 4 (#7e5a93)
L 2 (#5ac572)
U 4 (#39f613)
L 4 (#300c42)
U 3 (#2c94e1)
R 6 (#68ae32)
U 7 (#2c94e3)
L 3 (#289422)
U 5 (#06ded3)
L 3 (#025d52)
U 2 (#1c4373)
L 5 (#21b252)
U 4 (#8646d3)
L 3 (#764a62)
U 5 (#8646d1)
L 6 (#6d5762)
U 6 (#1c4371)
R 3 (#3c7462)
U 3 (#041483)
R 4 (#01cab2)
U 5 (#784883)
R 3 (#60f6c2)
U 6 (#057d73)
R 2 (#1fb832)
U 2 (#775953)
R 4 (#3c9532)
U 4 (#404733)
R 7 (#2423b2)
U 3 (#1eee81)
R 7 (#797832)
U 3 (#1eee83)
R 4 (#220652)
U 5 (#15f0b3)
R 3 (#4c0d62)
D 4 (#2083c3)
R 4 (#1c5cf2)
D 3 (#5f0f33)
R 3 (#5494e2)
D 5 (#684523)
R 4 (#1202b2)
U 9 (#455503)
R 2 (#15d2a2)
D 9 (#36e533)
R 4 (#1c78a0)
D 3 (#4bcc43)
R 6 (#4baf50)
D 3 (#66edf3)
R 4 (#4baf52)
D 4 (#222a23)
L 9 (#1c78a2)
D 2 (#043003)
R 9 (#1d0a42)
D 4 (#1e6941)
R 5 (#3a6662)
U 4 (#1df801)
R 3 (#8b8d92)
U 7 (#35c951)
R 4 (#8b8d90)
U 3 (#4d8771)
R 3 (#1b5502)
U 3 (#4a6233)
R 5 (#71c6c2)
D 4 (#408383)
R 5 (#057732)
U 4 (#076963)
R 4 (#045692)
U 6 (#4e80e1)
R 3 (#5f77d2)
U 3 (#114573)
R 2 (#6c4b92)
U 3 (#114571)
L 8 (#07cc82)
U 3 (#4e80e3)
R 8 (#166fe2)
U 4 (#924f11)
R 3 (#496b52)
U 2 (#912dc3)
R 3 (#0e9052)
D 3 (#2e8443)
R 4 (#572312)
D 4 (#31d0d3)
L 5 (#745c00)
D 5 (#058313)
R 5 (#2847e0)
D 3 (#5ba6c3)
R 7 (#58b860)
U 4 (#560193)
R 4 (#5e3d90)
U 7 (#36d363)
R 6 (#02d030)
D 7 (#49d633)
R 4 (#4e03d2)
U 4 (#8d4853)
R 8 (#3b9d12)
U 4 (#8d4851)
R 5 (#302542)
U 6 (#40e003)
R 7 (#130d32)
D 4 (#5323d1)
R 6 (#4ed652)
D 6 (#5323d3)
R 4 (#541fd2)
U 2 (#0f08e3)
R 3 (#16bd42)
U 5 (#603191)
R 6 (#3545f2)
U 3 (#011651)
R 4 (#3ecb62)
U 6 (#963781)
R 5 (#2c7612)
D 6 (#509971)
R 6 (#885a52)
D 6 (#3b0dd3)
R 2 (#2afe62)
D 4 (#1f4e53)
R 3 (#2c0ab2)
D 5 (#3be5a3)
L 3 (#2c0ab0)
D 4 (#55e5c3)
L 7 (#2afe60)
D 4 (#5bf153)
L 5 (#617782)
U 4 (#094123)
L 4 (#5186f2)
U 4 (#544353)
L 3 (#40f5c2)
D 3 (#325493)
L 6 (#40b782)
D 5 (#713cc3)
L 4 (#3fdd60)
D 8 (#0b87f3)
L 5 (#41cfe0)
D 3 (#340bf3)
L 2 (#718d12)
D 7 (#6a0d63)
R 6 (#88f440)
U 3 (#612011)
R 2 (#4eb270)
U 4 (#6f7821)
R 5 (#077c50)
U 6 (#209901)
R 4 (#1eeaa0)
D 3 (#1176f1)
R 2 (#2b79f0)
D 5 (#6cd941)
R 5 (#72d3a0)
D 3 (#25a453)
R 3 (#09e140)
D 3 (#7944e3)
R 4 (#071320)
U 9 (#540d61)
R 2 (#63bb30)
U 5 (#317753)
R 3 (#5a9e00)
D 5 (#056963)
R 3 (#385080)
D 3 (#75cda3)
R 2 (#331280)
D 7 (#23db43)
R 2 (#11d3e2)
D 3 (#2f8123)
R 4 (#77eef2)
D 3 (#2f8121)
R 4 (#3c3e32)
D 5 (#541c03)
R 6 (#531fb0)
D 3 (#43a463)
R 3 (#2a8950)
D 6 (#2a8193)
R 3 (#078660)
D 3 (#668ce3)
R 3 (#640a50)
D 3 (#64e5a3)
R 5 (#961a02)
D 6 (#492053)
R 4 (#4d71d0)
D 3 (#48f563)
R 4 (#14d310)
D 5 (#48f561)
R 5 (#53be70)
U 5 (#195313)
R 3 (#0b2550)
U 2 (#5fde13)
R 2 (#7dbeb0)
U 4 (#5fde11)
R 6 (#145d90)
U 4 (#34b771)
R 6 (#834300)
U 3 (#34b773)
L 6 (#0387d0)
U 4 (#469471)
R 6 (#9406f0)
U 6 (#469473)
R 3 (#2a4d50)
D 5 (#037633)
R 7 (#14d340)
D 6 (#11daf1)
R 7 (#337f90)
D 4 (#526051)
R 4 (#757a30)
D 8 (#643b43)
R 3 (#1185a0)
D 6 (#07f453)
R 3 (#621c50)
D 2 (#07f451)
R 3 (#30dcf0)
D 4 (#0a7101)
L 4 (#08b2b0)
D 3 (#0c9b51)
L 2 (#1c5e10)
D 7 (#6bf7c1)
L 4 (#5702a0)
D 2 (#6bf7c3)
L 8 (#2d1240)
D 5 (#5cf4e1)
L 6 (#14c4d2)
D 4 (#98b1f1)
L 3 (#4b01e2)
D 8 (#2711a1)
L 2 (#4dca52)
U 8 (#41b261)
L 3 (#114f32)
U 5 (#842761)
L 6 (#3f4122)
U 4 (#842763)
L 3 (#4d3102)
D 3 (#41b263)
L 2 (#1118e2)
D 5 (#59cc03)
L 8 (#8b9742)
D 2 (#65f793)
R 8 (#328982)
D 4 (#85c501)
L 3 (#441702)
D 5 (#0a1f51)
L 8 (#2ccc92)
D 3 (#52f541)
L 6 (#5dfc42)
D 5 (#52f543)
L 4 (#1d83f2)
D 6 (#5eeab1)
R 4 (#4634e0)
D 4 (#968ca1)
L 3 (#437e40)
D 7 (#968ca3)
L 3 (#62b0a0)
D 5 (#38aa71)
L 5 (#3c8e22)
U 5 (#7bb931)
L 6 (#172202)
U 3 (#809981)
L 5 (#172200)
D 4 (#0b1ac1)
L 2 (#3c8e20)
D 4 (#1d0d71)
L 6 (#82ea50)
D 2 (#373bd1)
L 2 (#1ec200)
D 7 (#471d01)
R 6 (#3b6472)
D 4 (#0273b3)
R 3 (#2b4df2)
D 2 (#0273b1)
R 7 (#3af9f2)
D 4 (#432781)
R 6 (#161170)
D 3 (#682741)
L 6 (#5aee90)
D 4 (#574c13)
L 4 (#4c3770)
D 2 (#5eadb3)
R 4 (#3b4a30)
D 5 (#5eadb1)
L 3 (#4700c0)
D 5 (#574c11)
L 5 (#4569e0)
D 5 (#250471)
L 5 (#014740)
D 4 (#61e5f1)
L 3 (#772650)
D 3 (#1e6f21)
R 3 (#1d3220)
D 6 (#430041)
R 2 (#608810)
D 6 (#341ae3)
R 4 (#5bd730)
U 6 (#3132d3)
R 2 (#3fab42)
U 6 (#90b273)
R 4 (#3fab40)
D 7 (#263173)
R 4 (#5bd732)
D 5 (#423813)
R 5 (#6ba250)
D 2 (#773ab1)
R 5 (#37ed60)
D 4 (#048871)
L 10 (#4de780)
D 3 (#185e81)
R 3 (#40d660)
D 6 (#5dd3d3)
L 7 (#4d2210)
U 3 (#364dd3)
L 4 (#2208a0)
U 7 (#13db53)
L 4 (#1ff800)
D 3 (#74fa63)
L 8 (#5405c0)
D 6 (#7a2ee3)
L 2 (#4ac3c0)
D 3 (#30c2e3)
L 2 (#15a070)
D 6 (#0cac13)
L 5 (#495b10)
U 8 (#8c3253)
L 3 (#495b12)
U 2 (#2b5c03)
L 5 (#1bbdb0)
U 8 (#203f53)
L 3 (#57a1e2)
U 3 (#5032f3)
L 4 (#57a1e0)
U 6 (#327c63)
R 4 (#50a820)
U 4 (#2b3343)
L 3 (#7014d0)
U 4 (#12bef3)
L 3 (#656f40)
U 4 (#2dc663)
L 6 (#113020)
U 4 (#3a26d3)
L 5 (#133f60)
U 8 (#024a51)
L 2 (#584c70)
U 3 (#024a53)
L 4 (#364750)
U 4 (#3a26d1)
L 3 (#023e50)
U 6 (#07ffd3)
R 6 (#6a4610)
U 3 (#0a1a53)
R 4 (#1d5f72)
U 4 (#3dc7a3)
L 10 (#43bdc0)
U 3 (#2c8853)
L 8 (#43bdc2)
U 6 (#646bb3)
R 6 (#1d5f70)
U 7 (#2d4373)
R 3 (#8264e0)
U 5 (#35f151)
R 4 (#2b1760)
U 4 (#999c03)
R 3 (#4175f0)
D 8 (#999c01)
R 5 (#3c0e70)
D 2 (#32a5e1)
R 3 (#1b64a0)
D 6 (#261bc1)
R 3 (#0b6c22)
U 2 (#2d3081)
R 4 (#72b8a2)
U 4 (#236cb1)
R 6 (#45dba2)
U 4 (#209921)
L 6 (#128470)
U 6 (#4d6b01)
L 2 (#190550)
U 3 (#40f381)
L 7 (#804a40)
U 4 (#1accd1)
L 4 (#5d9a40)
U 2 (#042311)
L 6 (#1fa392)
U 3 (#3b7471)
L 3 (#565ac2)
U 5 (#3b7473)
L 7 (#4c7822)
U 7 (#47ab31)
L 3 (#184fa2)
U 7 (#417391)
L 4 (#4cffd2)
U 3 (#3bf361)
L 3 (#84fdd0)
D 7 (#178953)
L 2 (#5ddab0)
D 3 (#178951)
L 5 (#44ed60)
D 5 (#6eb101)
L 3 (#455aa0)
U 4 (#231db1)
L 8 (#2511b0)
U 4 (#886b31)
R 4 (#2511b2)
U 3 (#36b861)
R 4 (#421320)
U 4 (#267471)
L 3 (#43b980)
U 3 (#214141)
R 3 (#2491c0)
U 2 (#214143)
R 7 (#4ae330)
U 3 (#4c5d41)
R 8 (#1897d0)
D 5 (#39eba1)
R 5 (#6c3442)
D 7 (#439aa1)
R 4 (#652a42)
U 7 (#5437e1)
R 3 (#3c7ae2)
U 6 (#3653c1)
L 5 (#6b4d52)
U 4 (#5cb7f1)
L 3 (#073a02)
U 5 (#360581)
L 3 (#6d9fd2)
U 7 (#7901a1)
L 2 (#123b32)
U 3 (#4b1f83)
L 4 (#2d6582)
U 10 (#342933)
R 4 (#538792)
U 5 (#263fd3)
L 5 (#0a3c42)
D 2 (#433c41)
L 3 (#4c3042)
D 5 (#35f621)
L 6 (#42f670)
D 6 (#466371)
R 6 (#42f672)
D 7 (#6248a1)
L 2 (#670152)
D 5 (#90e203)
L 5 (#4495b2)
U 4 (#90fc73)
L 2 (#5707b2)
U 8 (#4c76e1)
L 4 (#540512)
U 3 (#492111)
R 4 (#540510)
U 6 (#0ff091)
L 4 (#2b4272)
U 5 (#05b161)
L 3 (#7483e2)
D 6 (#6b3f11)
L 3 (#480342)
D 6 (#307111)
L 5 (#0f29d2)
D 6 (#019f91)
L 6 (#342822)
D 4 (#180cb1)
R 8 (#00f720)
D 4 (#31b091)
L 8 (#2f1380)
D 3 (#4ab461)
L 6 (#2194c0)
U 5 (#6bee03)
L 5 (#65f3b0)
U 8 (#1076f3)
L 3 (#1c34c0)
U 3 (#1151c1)
R 6 (#0bed40)
U 3 (#3db921)
L 4 (#4e4190)
U 2 (#674151)
L 2 (#2f95c0)
U 8 (#097d01)
L 3 (#4f5a20)
D 2 (#69a8c1)
L 2 (#04b560)
D 3 (#2ca343)
L 4 (#70ccd0)
D 5 (#6c7323)
R 4 (#377f50)
D 4 (#3ae061)
L 2 (#5883f0)
D 6 (#5e3601)
L 2 (#27eb00)
D 7 (#6fffc3)
L 4 (#06e992)
D 4 (#43dee3)
L 4 (#06e990)
D 4 (#759353)
L 6 (#7da150)
D 5 (#5f0761)
R 5 (#0ec360)
D 3 (#4b82f1)
R 3 (#400b40)
D 3 (#72f301)
R 4 (#288900)
D 3 (#4b0f11)
R 8 (#288902)
D 4 (#471d21)
L 8 (#708912)
D 4 (#2264e1)
R 4 (#708910)
D 6 (#3c2e31)
R 2 (#23e370)
D 2 (#6e40f1)
R 4 (#2ccbc0)
U 9 (#51a4c1)
R 4 (#1e2560)
D 9 (#1dafa1)
R 3 (#44ac70)
D 5 (#028461)
R 9 (#3d9be0)
D 4 (#929d31)
L 6 (#5e7b02)
D 6 (#559df1)
L 8 (#5e7b00)
D 4 (#48ec01)
L 5 (#93cfe2)
D 8 (#1cafe1)
L 4 (#0459f2)
U 8 (#724081)
L 4 (#47c962)
D 4 (#5e74e1)
L 3 (#495f80)
D 5 (#7299e1)
L 5 (#495f82)
D 5 (#045f61)
R 7 (#47c960)
D 3 (#2335f1)
R 6 (#499fd2)
D 6 (#43b841)
R 6 (#031222)
D 6 (#48f681)
L 5 (#727da2)
D 4 (#41d7e1)
R 5 (#113942)
D 5 (#03ee33)
R 4 (#0350c2)
D 2 (#4727a3)
R 4 (#148620)
D 4 (#33d453)
R 8 (#148622)
D 3 (#210703)
L 3 (#1c3d42)
D 3 (#19c743)
L 7 (#3991b2)
D 4 (#4fb431)
L 3 (#0563c2)
D 4 (#6a0431)
L 5 (#592952)
D 6 (#0898b1)
L 4 (#1f2f32)
D 6 (#42ac11)
L 3 (#045bb2)
D 5 (#62d0c1)
L 6 (#84e602)
D 2 (#68e891)
L 6 (#2c8512)
D 5 (#16e5c1)
L 5 (#2dc202)
U 3 (#08e561)
L 2 (#1d84c0)
U 8 (#64b671)
L 4 (#1d84c2)
U 3 (#2d4741)
L 4 (#5342c2)
D 3 (#012ce1)
L 6 (#229ca2)
D 3 (#5d0f51)
L 2 (#6f27a2)
D 5 (#3102b1)
L 2 (#39f292)
D 3 (#0e1d31)
L 4 (#1fe602)
U 6 (#1fbbb1)
L 6 (#001c52)
U 3 (#880b71)
L 4 (#001c50)
U 7 (#12db41)
L 2 (#2d7072)
U 3 (#941843)
R 8 (#255d72)
U 3 (#0ac063)
L 5 (#481670)
U 4 (#7f2aa3)
L 5 (#481672)
U 3 (#379e93)
L 4 (#255d70)
U 5 (#025ca3)
R 3 (#958792)
U 2 (#258701)
R 5 (#32e690)
D 4 (#6c9431)
R 2 (#32e692)
U 4 (#850cf1)
R 4 (#1f6942)
U 4 (#8dc581)
L 8 (#5949e2)
U 3 (#62cf01)
R 3 (#930cf2)
U 2 (#014781)
R 5 (#39d8b2)
U 4 (#9a50e1)
R 2 (#4e2032)
D 4 (#1129e1)
R 4 (#68c522)
U 5 (#030c91)
R 6 (#46e632)
U 3 (#54c0c1)
R 3 (#1104c2)
U 6 (#3b3a11)
L 7 (#72ea42)
U 7 (#5eaf41)
L 3 (#140192)
D 4 (#05dbd1)
L 2 (#507802)
D 2 (#05dbd3)
L 7 (#732442)
U 6 (#144931)
L 4 (#2e2f92)
U 7 (#29bd33)
L 4 (#1b1ca2)
U 4 (#89f183)

181
day18/src/main.rs Normal file
View File

@ -0,0 +1,181 @@
use std::collections::BTreeMap;
use std::collections::btree_map::Entry;
use std::fs::read_to_string;
use std::time::Instant;
#[derive(Copy, Clone, PartialEq)]
#[repr(u8)]
enum Direction {
Left = 0,
Right = 1,
Up = 2,
Down = 3,
}
const DIRECTION_OFFSET: [(i32, i32); 4] = [(-1, 0), (1, 0), (0, -1), (0, 1)];
fn get_area(horizontal_edges: &BTreeMap<i32, Vec<(i32, i32)>>) -> u64 {
struct Segment {
x0: i32,
x1: i32,
// coverage refers to coverage in current row if the segment shrinks in this row,
// if the segment shrinks in this row, coverage remains its previous bigger version
coverage: i32,
to_delete: bool, // whether it needs to be deleted after this row
}
// Segments present in currently processed row
let mut current_segments: Vec<Segment> = vec![];
// Current vertical position, start doesn't matter but must be very low
let mut current_y = i32::MIN;
// Result
let mut area: u64 = 0;
// Going through the edge map
for (&y, edges) in horizontal_edges.iter() {
// Catch up from the previous vertical position
for segment in current_segments.iter() {
area += (segment.coverage as i64 * (y - current_y - 1) as i64) as u64;
}
current_y = y;
// Going through all the edges
for &edge in edges {
let mut found_intersection = false;
// Segment to insert for splitting
let mut segment_to_insert: Option<Segment> = None;
// Checking if the processed edge intersects with some segment and acting accordingly
for segment in current_segments.iter_mut() {
if segment.x1 == edge.0 {
segment.x1 = edge.1;
segment.coverage += edge.1 - edge.0;
found_intersection = true;
} else if segment.x0 == edge.1 {
segment.x0 = edge.0;
segment.coverage += edge.1 - edge.0;
found_intersection = true;
} else if segment.x0 == edge.0 && segment.x1 == edge.1 {
// Here the edge is exactly the same like one segment, so it's going to end
segment.to_delete = true;
found_intersection = true;
} else if segment.x0 == edge.0 {
segment.x0 = edge.1;
found_intersection = true;
} else if segment.x1 == edge.1 {
segment.x1 = edge.0;
found_intersection = true;
} else if segment.x0 < edge.0 && segment.x1 > edge.1 {
// Here the edge is fully inside one of current segments, need to split
segment_to_insert = Some(Segment {
x0: edge.1,
x1: segment.x1,
coverage: 0, // Don't care how coverage is split, it will be recalculated
to_delete: false,
});
segment.x1 = edge.0;
found_intersection = true;
}
if found_intersection {
break;
}
}
if !found_intersection {
// No intersection, add new segment
current_segments.push(Segment {
x0: edge.0,
x1: edge.1,
coverage: edge.1 - edge.0 + 1,
to_delete: false,
});
} else if let Some(segment) = segment_to_insert {
// Do the splitting
current_segments.push(segment);
}
}
// Sort by lower bound and then merge segments which have overlapping bounds
current_segments.sort_by(|a, b| a.x0.cmp(&b.x0));
let mut i = 1;
while i < current_segments.len() {
if current_segments[i - 1].x1 == current_segments[i].x0 {
current_segments[i - 1].x1 = current_segments[i].x1;
current_segments[i - 1].coverage += current_segments[i].coverage - 1;
current_segments.remove(i);
} else {
i += 1;
}
}
// Add up current row coverage and update coverages for catching up
for segment in current_segments.iter_mut() {
area += segment.coverage as u64;
segment.coverage = segment.x1 - segment.x0 + 1;
}
// Remove segments which were removed this iteration
current_segments.retain(|segment| !segment.to_delete);
}
area
}
fn main() {
let time_start = Instant::now();
let input_str = read_to_string("input.txt").unwrap();
let time_start_no_io = Instant::now();
// edge maps, keys - y (vertical) position, values: list of horizontal edges (x1, x2)
let mut horizontal_edges1: BTreeMap<i32, Vec<(i32, i32)>> = BTreeMap::new();
let mut horizontal_edges2: BTreeMap<i32, Vec<(i32, i32)>> = BTreeMap::new();
// Current position for parsing
let mut current_pos1 = (0, 0);
let mut current_pos2 = (0, 0);
// Takes current_pos, direction and distance of the next step
// Inserts horizontal edges into the map, ignores vertical, modifies current_pos
let process_edge = |horizontal_edges: &mut BTreeMap<i32, Vec<(i32, i32)>>,
current_pos: &mut (i32, i32), dir: Direction, dist: i32| {
let mut offset = DIRECTION_OFFSET[dir as usize];
offset.0 *= dist;
offset.1 *= dist;
let new_pos = (current_pos.0 + offset.0, current_pos.1 + offset.1);
if dir == Direction::Left || dir == Direction::Right {
let vec;
match horizontal_edges.entry(new_pos.1) {
Entry::Vacant(vacant_entry) => {
vec = vacant_entry.insert(vec![]);
}
Entry::Occupied(occupied_entry) => {
vec = occupied_entry.into_mut();
}
}
if dir == Direction::Left {
vec.push((new_pos.0, current_pos.0));
} else {
vec.push((current_pos.0, new_pos.0));
}
}
*current_pos = new_pos;
};
// Populate both edge maps
for line in input_str.lines() {
let mut split_whitespace = line.split_whitespace();
let dir1 = match split_whitespace.next().unwrap().as_bytes()[0] {
b'L' => Direction::Left,
b'R' => Direction::Right,
b'U' => Direction::Up,
_ => Direction::Down
};
let dist1 = split_whitespace.next().unwrap().parse::<u8>().unwrap();
let code = &split_whitespace.next().unwrap()[2..8];
let dir2 = match code.as_bytes()[5] {
b'0' => Direction::Right,
b'1' => Direction::Down,
b'2' => Direction::Left,
_ => Direction::Up
};
let dist2 = i32::from_str_radix(&code[0..5], 16).unwrap();
process_edge(&mut horizontal_edges1, &mut current_pos1, dir1, dist1 as i32);
process_edge(&mut horizontal_edges2, &mut current_pos2, dir2, dist2);
}
// Compute areas based on the edge maps
let area1 = get_area(&horizontal_edges1);
let area2 = get_area(&horizontal_edges2);
let elapsed = time_start.elapsed().as_micros();
let elapsed_no_io = time_start_no_io.elapsed().as_micros();
println!("Time: {}us", elapsed);
println!("Time without file i/o: {}us", elapsed_no_io);
println!("Area1: {}", area1);
println!("Area2: {}", area2);
}