From 9278a5472ab18a1b9118b9dac352ba574a31d56f Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Mon, 25 Nov 2019 19:04:48 +0100 Subject: [PATCH] 2018: day04: part 1 --- aoc2018/input/day04.txt | 1017 +++++++++++++++++++++++++++++++++++++++ aoc2018/src/day04.rs | 216 +++++++++ aoc2018/src/lib.rs | 1 + aoc2018/src/main.rs | 3 +- 4 files changed, 1236 insertions(+), 1 deletion(-) create mode 100644 aoc2018/input/day04.txt create mode 100644 aoc2018/src/day04.rs diff --git a/aoc2018/input/day04.txt b/aoc2018/input/day04.txt new file mode 100644 index 0000000..2cb73d5 --- /dev/null +++ b/aoc2018/input/day04.txt @@ -0,0 +1,1017 @@ +[1518-09-19 00:42] wakes up +[1518-08-06 00:16] wakes up +[1518-07-18 00:14] wakes up +[1518-03-23 00:19] falls asleep +[1518-10-12 23:58] Guard #421 begins shift +[1518-08-05 00:24] falls asleep +[1518-09-18 00:48] wakes up +[1518-08-03 00:57] wakes up +[1518-03-08 00:00] falls asleep +[1518-03-06 23:47] Guard #1009 begins shift +[1518-10-14 00:03] falls asleep +[1518-06-15 00:51] falls asleep +[1518-10-09 23:57] Guard #1667 begins shift +[1518-10-21 00:21] falls asleep +[1518-09-03 00:02] falls asleep +[1518-03-11 00:08] wakes up +[1518-08-13 00:10] falls asleep +[1518-08-25 00:11] falls asleep +[1518-08-18 00:14] falls asleep +[1518-10-05 00:51] wakes up +[1518-05-22 00:38] wakes up +[1518-11-01 00:54] wakes up +[1518-06-09 00:36] wakes up +[1518-10-30 23:56] Guard #3167 begins shift +[1518-04-05 00:03] falls asleep +[1518-09-26 00:51] wakes up +[1518-09-06 00:34] wakes up +[1518-11-21 00:27] wakes up +[1518-07-17 00:02] falls asleep +[1518-06-25 00:51] falls asleep +[1518-11-22 00:38] wakes up +[1518-10-14 00:10] wakes up +[1518-07-23 00:50] falls asleep +[1518-07-15 00:48] falls asleep +[1518-03-14 00:00] Guard #1163 begins shift +[1518-07-22 00:46] falls asleep +[1518-10-24 00:51] wakes up +[1518-09-27 23:58] Guard #277 begins shift +[1518-10-03 00:58] wakes up +[1518-11-07 23:59] Guard #683 begins shift +[1518-04-06 00:45] falls asleep +[1518-06-13 00:59] wakes up +[1518-11-13 00:55] falls asleep +[1518-09-27 00:01] falls asleep +[1518-07-24 00:51] wakes up +[1518-05-27 00:44] wakes up +[1518-07-26 00:55] wakes up +[1518-04-13 00:48] falls asleep +[1518-10-25 00:38] wakes up +[1518-10-07 00:24] falls asleep +[1518-08-27 00:00] Guard #1061 begins shift +[1518-04-03 23:54] Guard #109 begins shift +[1518-07-08 00:34] falls asleep +[1518-07-26 23:56] Guard #421 begins shift +[1518-11-19 00:00] Guard #863 begins shift +[1518-03-10 00:50] wakes up +[1518-08-01 00:35] falls asleep +[1518-10-13 00:18] wakes up +[1518-04-12 00:56] wakes up +[1518-06-17 00:39] falls asleep +[1518-06-29 00:49] wakes up +[1518-06-15 00:53] wakes up +[1518-09-28 00:50] falls asleep +[1518-05-25 00:03] Guard #421 begins shift +[1518-04-13 00:54] wakes up +[1518-05-19 00:27] falls asleep +[1518-09-22 00:17] falls asleep +[1518-09-02 23:52] Guard #509 begins shift +[1518-03-25 00:00] falls asleep +[1518-06-24 00:00] Guard #1777 begins shift +[1518-08-04 00:02] falls asleep +[1518-07-19 00:53] wakes up +[1518-09-05 23:46] Guard #1163 begins shift +[1518-07-16 00:55] wakes up +[1518-03-29 00:23] falls asleep +[1518-03-13 00:41] wakes up +[1518-10-28 00:46] wakes up +[1518-07-05 00:20] falls asleep +[1518-06-16 00:54] wakes up +[1518-06-19 23:58] Guard #1009 begins shift +[1518-10-27 00:53] falls asleep +[1518-10-15 00:06] falls asleep +[1518-04-01 00:45] wakes up +[1518-06-07 00:23] falls asleep +[1518-09-01 00:58] wakes up +[1518-06-01 00:23] wakes up +[1518-11-08 00:55] wakes up +[1518-09-19 00:49] falls asleep +[1518-07-19 00:49] falls asleep +[1518-06-02 00:40] wakes up +[1518-06-22 00:29] wakes up +[1518-11-16 23:52] Guard #1061 begins shift +[1518-07-25 00:45] wakes up +[1518-05-19 00:33] wakes up +[1518-11-12 00:45] wakes up +[1518-06-19 00:56] falls asleep +[1518-05-05 00:34] wakes up +[1518-05-13 00:00] Guard #2521 begins shift +[1518-06-21 00:02] Guard #1009 begins shift +[1518-09-17 00:46] wakes up +[1518-08-20 00:16] wakes up +[1518-03-25 00:55] wakes up +[1518-06-02 00:45] falls asleep +[1518-11-05 00:03] Guard #2539 begins shift +[1518-07-26 00:10] falls asleep +[1518-07-12 00:57] wakes up +[1518-06-13 00:40] wakes up +[1518-08-24 00:50] wakes up +[1518-03-06 00:32] falls asleep +[1518-06-28 00:50] wakes up +[1518-06-27 00:19] wakes up +[1518-11-20 00:21] falls asleep +[1518-03-13 00:10] falls asleep +[1518-06-02 00:05] falls asleep +[1518-09-05 00:21] falls asleep +[1518-09-02 00:02] Guard #109 begins shift +[1518-09-23 00:50] wakes up +[1518-08-04 00:58] wakes up +[1518-05-03 00:55] wakes up +[1518-04-24 00:13] falls asleep +[1518-09-01 00:57] falls asleep +[1518-03-19 23:59] Guard #3167 begins shift +[1518-05-28 00:22] falls asleep +[1518-11-14 00:33] wakes up +[1518-10-09 00:08] wakes up +[1518-08-10 00:38] wakes up +[1518-08-15 00:19] falls asleep +[1518-07-05 00:51] wakes up +[1518-10-20 00:56] wakes up +[1518-06-23 00:42] wakes up +[1518-05-05 00:19] falls asleep +[1518-05-15 00:45] wakes up +[1518-07-13 00:34] wakes up +[1518-04-12 23:58] Guard #2269 begins shift +[1518-08-19 00:09] falls asleep +[1518-04-21 00:33] falls asleep +[1518-08-20 00:43] wakes up +[1518-07-12 00:56] falls asleep +[1518-05-27 00:36] falls asleep +[1518-08-17 00:49] falls asleep +[1518-11-18 00:48] wakes up +[1518-08-05 00:10] wakes up +[1518-06-30 00:37] falls asleep +[1518-06-02 00:55] wakes up +[1518-09-06 00:05] falls asleep +[1518-08-09 00:00] Guard #3083 begins shift +[1518-06-24 23:59] Guard #1229 begins shift +[1518-09-28 00:58] wakes up +[1518-06-18 00:41] wakes up +[1518-05-23 00:03] Guard #1061 begins shift +[1518-06-23 00:30] wakes up +[1518-05-17 00:59] wakes up +[1518-07-18 00:00] Guard #1163 begins shift +[1518-06-27 00:59] wakes up +[1518-08-08 00:01] Guard #2521 begins shift +[1518-03-17 00:43] falls asleep +[1518-04-14 00:47] wakes up +[1518-05-23 23:53] Guard #701 begins shift +[1518-08-28 00:51] falls asleep +[1518-10-18 23:56] Guard #1667 begins shift +[1518-04-10 00:51] wakes up +[1518-10-22 00:15] falls asleep +[1518-07-01 00:57] wakes up +[1518-06-24 00:49] falls asleep +[1518-10-27 00:29] falls asleep +[1518-03-19 00:56] wakes up +[1518-06-05 00:00] Guard #1229 begins shift +[1518-09-13 00:04] Guard #179 begins shift +[1518-04-28 00:49] falls asleep +[1518-09-18 00:24] falls asleep +[1518-09-20 00:25] wakes up +[1518-08-05 00:56] wakes up +[1518-03-22 00:42] wakes up +[1518-08-11 00:28] falls asleep +[1518-07-02 00:27] falls asleep +[1518-05-06 00:52] wakes up +[1518-03-12 00:37] wakes up +[1518-07-17 00:16] falls asleep +[1518-03-29 00:54] wakes up +[1518-10-05 00:02] Guard #2539 begins shift +[1518-04-30 00:51] wakes up +[1518-07-10 00:25] wakes up +[1518-10-03 00:00] Guard #1061 begins shift +[1518-04-19 00:29] wakes up +[1518-05-18 23:58] Guard #1163 begins shift +[1518-10-29 00:12] falls asleep +[1518-09-18 00:36] wakes up +[1518-11-12 00:14] falls asleep +[1518-03-06 00:49] falls asleep +[1518-09-28 00:30] falls asleep +[1518-04-12 00:00] Guard #683 begins shift +[1518-03-11 00:23] wakes up +[1518-05-02 23:51] Guard #863 begins shift +[1518-10-18 00:27] falls asleep +[1518-08-05 23:52] Guard #3083 begins shift +[1518-07-10 23:56] Guard #421 begins shift +[1518-06-16 00:41] falls asleep +[1518-11-10 23:59] Guard #1229 begins shift +[1518-10-08 00:28] falls asleep +[1518-04-08 00:59] wakes up +[1518-07-03 00:02] Guard #701 begins shift +[1518-11-21 23:50] Guard #421 begins shift +[1518-11-19 00:52] wakes up +[1518-07-31 23:56] Guard #1009 begins shift +[1518-06-17 00:31] wakes up +[1518-06-24 00:42] wakes up +[1518-08-21 00:20] wakes up +[1518-09-07 00:02] Guard #863 begins shift +[1518-07-15 00:05] falls asleep +[1518-04-25 00:53] wakes up +[1518-08-06 00:01] falls asleep +[1518-03-25 23:50] Guard #1381 begins shift +[1518-03-26 23:56] Guard #3167 begins shift +[1518-03-04 23:58] Guard #683 begins shift +[1518-05-15 00:36] falls asleep +[1518-03-24 00:52] falls asleep +[1518-06-27 00:45] falls asleep +[1518-05-12 00:47] falls asleep +[1518-06-13 00:55] falls asleep +[1518-06-25 00:11] falls asleep +[1518-07-04 00:02] Guard #2521 begins shift +[1518-04-14 00:13] wakes up +[1518-11-19 00:50] falls asleep +[1518-05-07 00:19] wakes up +[1518-04-15 00:53] wakes up +[1518-04-28 00:27] falls asleep +[1518-11-13 00:12] wakes up +[1518-07-21 00:46] wakes up +[1518-05-01 23:58] Guard #683 begins shift +[1518-03-07 00:02] falls asleep +[1518-04-08 00:34] falls asleep +[1518-10-31 00:44] falls asleep +[1518-03-07 23:48] Guard #2269 begins shift +[1518-08-03 00:03] Guard #2539 begins shift +[1518-07-06 00:16] falls asleep +[1518-07-09 00:42] wakes up +[1518-09-04 00:11] falls asleep +[1518-04-20 23:59] Guard #683 begins shift +[1518-06-28 00:00] Guard #2539 begins shift +[1518-09-26 00:22] falls asleep +[1518-10-03 00:20] falls asleep +[1518-06-19 00:18] falls asleep +[1518-03-21 00:01] Guard #1381 begins shift +[1518-04-25 00:17] falls asleep +[1518-09-20 00:56] wakes up +[1518-11-02 00:02] Guard #683 begins shift +[1518-06-25 00:40] wakes up +[1518-04-13 00:38] falls asleep +[1518-03-18 00:33] falls asleep +[1518-04-17 00:22] falls asleep +[1518-06-12 00:28] falls asleep +[1518-10-26 00:00] Guard #179 begins shift +[1518-07-14 23:50] Guard #863 begins shift +[1518-04-15 00:22] falls asleep +[1518-05-29 00:19] falls asleep +[1518-11-21 00:24] falls asleep +[1518-10-29 23:59] Guard #421 begins shift +[1518-04-21 00:08] falls asleep +[1518-03-30 23:57] Guard #863 begins shift +[1518-03-05 00:34] falls asleep +[1518-08-28 00:46] wakes up +[1518-05-20 00:32] wakes up +[1518-03-03 00:00] Guard #2269 begins shift +[1518-11-05 00:27] falls asleep +[1518-07-22 00:50] wakes up +[1518-04-01 23:53] Guard #109 begins shift +[1518-05-28 00:53] wakes up +[1518-07-21 00:34] falls asleep +[1518-06-26 00:48] wakes up +[1518-03-10 23:49] Guard #863 begins shift +[1518-11-19 23:50] Guard #3167 begins shift +[1518-07-09 00:58] wakes up +[1518-10-20 00:03] Guard #1229 begins shift +[1518-06-30 00:42] wakes up +[1518-07-23 00:54] wakes up +[1518-04-06 00:49] falls asleep +[1518-04-14 00:00] Guard #1381 begins shift +[1518-05-26 00:00] Guard #277 begins shift +[1518-04-07 00:14] falls asleep +[1518-04-10 00:14] falls asleep +[1518-05-04 00:58] wakes up +[1518-07-14 00:08] falls asleep +[1518-07-27 00:54] wakes up +[1518-06-07 00:26] wakes up +[1518-11-17 00:40] wakes up +[1518-05-17 23:57] Guard #1381 begins shift +[1518-05-09 00:00] Guard #2539 begins shift +[1518-03-28 00:57] wakes up +[1518-04-10 00:49] falls asleep +[1518-05-26 00:35] falls asleep +[1518-06-29 00:40] falls asleep +[1518-06-20 00:10] falls asleep +[1518-11-04 00:38] falls asleep +[1518-06-30 00:29] wakes up +[1518-11-01 00:15] falls asleep +[1518-04-09 00:15] falls asleep +[1518-06-27 00:38] wakes up +[1518-04-18 00:07] falls asleep +[1518-10-25 00:09] falls asleep +[1518-10-17 00:21] wakes up +[1518-11-18 00:30] falls asleep +[1518-07-30 00:04] Guard #1777 begins shift +[1518-03-15 00:48] falls asleep +[1518-03-18 00:53] wakes up +[1518-11-03 00:07] wakes up +[1518-05-10 00:57] wakes up +[1518-04-10 00:33] wakes up +[1518-09-25 00:50] wakes up +[1518-06-21 00:52] falls asleep +[1518-04-21 00:48] wakes up +[1518-08-19 00:17] wakes up +[1518-08-20 00:35] falls asleep +[1518-08-26 00:00] Guard #3331 begins shift +[1518-07-10 00:00] falls asleep +[1518-03-30 00:22] falls asleep +[1518-06-03 23:56] Guard #863 begins shift +[1518-06-12 00:01] Guard #683 begins shift +[1518-04-30 00:02] Guard #1381 begins shift +[1518-11-13 00:11] falls asleep +[1518-09-23 00:00] Guard #3331 begins shift +[1518-11-04 00:57] wakes up +[1518-10-24 00:17] wakes up +[1518-04-11 00:04] Guard #277 begins shift +[1518-07-16 00:31] falls asleep +[1518-07-07 00:34] wakes up +[1518-04-24 00:59] wakes up +[1518-06-05 00:27] falls asleep +[1518-07-05 00:00] Guard #277 begins shift +[1518-05-02 00:31] falls asleep +[1518-04-05 00:33] falls asleep +[1518-06-07 00:33] falls asleep +[1518-07-14 00:56] wakes up +[1518-10-05 00:49] falls asleep +[1518-03-07 00:51] wakes up +[1518-05-22 00:18] falls asleep +[1518-03-17 00:29] wakes up +[1518-03-15 00:30] falls asleep +[1518-07-29 00:49] wakes up +[1518-08-24 23:59] Guard #3331 begins shift +[1518-10-04 00:56] wakes up +[1518-05-20 23:46] Guard #863 begins shift +[1518-05-14 00:14] falls asleep +[1518-10-24 00:34] falls asleep +[1518-03-10 00:44] falls asleep +[1518-09-07 00:28] wakes up +[1518-03-10 00:18] falls asleep +[1518-06-08 23:49] Guard #1061 begins shift +[1518-05-30 00:58] wakes up +[1518-10-12 00:55] wakes up +[1518-05-07 23:56] Guard #701 begins shift +[1518-11-12 00:58] wakes up +[1518-11-15 00:03] falls asleep +[1518-08-28 00:04] Guard #3167 begins shift +[1518-03-20 00:47] wakes up +[1518-03-26 00:45] falls asleep +[1518-04-14 00:26] falls asleep +[1518-06-01 00:00] Guard #1061 begins shift +[1518-04-27 00:32] wakes up +[1518-07-16 00:47] falls asleep +[1518-03-27 00:08] falls asleep +[1518-09-07 00:57] wakes up +[1518-05-25 00:11] falls asleep +[1518-07-27 23:59] Guard #863 begins shift +[1518-06-21 23:58] Guard #421 begins shift +[1518-09-12 00:55] wakes up +[1518-11-14 00:52] wakes up +[1518-08-19 00:32] falls asleep +[1518-04-30 23:46] Guard #109 begins shift +[1518-06-19 00:04] Guard #1163 begins shift +[1518-03-14 23:57] Guard #179 begins shift +[1518-11-17 00:59] wakes up +[1518-03-15 00:39] falls asleep +[1518-04-04 23:50] Guard #109 begins shift +[1518-07-31 00:36] wakes up +[1518-06-01 23:52] Guard #701 begins shift +[1518-09-07 00:11] falls asleep +[1518-09-24 00:21] falls asleep +[1518-11-23 00:00] Guard #2539 begins shift +[1518-07-23 00:23] wakes up +[1518-10-21 00:52] wakes up +[1518-03-13 00:00] Guard #3331 begins shift +[1518-11-15 00:52] wakes up +[1518-06-24 00:22] falls asleep +[1518-06-12 00:31] wakes up +[1518-08-09 00:06] falls asleep +[1518-10-22 00:43] wakes up +[1518-09-21 00:52] wakes up +[1518-10-13 23:47] Guard #179 begins shift +[1518-08-11 00:00] Guard #1009 begins shift +[1518-03-15 00:43] wakes up +[1518-07-06 00:35] wakes up +[1518-03-17 23:47] Guard #2269 begins shift +[1518-08-30 00:38] wakes up +[1518-09-09 23:56] Guard #3331 begins shift +[1518-11-06 00:02] Guard #2539 begins shift +[1518-04-17 23:56] Guard #509 begins shift +[1518-04-23 00:26] falls asleep +[1518-04-14 00:08] falls asleep +[1518-03-16 00:00] Guard #3083 begins shift +[1518-04-22 00:28] falls asleep +[1518-03-24 00:47] falls asleep +[1518-08-05 00:05] falls asleep +[1518-09-10 00:48] falls asleep +[1518-03-30 00:00] Guard #179 begins shift +[1518-04-07 23:58] Guard #3167 begins shift +[1518-08-29 00:17] wakes up +[1518-08-24 00:00] Guard #3083 begins shift +[1518-05-08 00:48] falls asleep +[1518-10-11 00:28] falls asleep +[1518-04-19 23:58] Guard #683 begins shift +[1518-08-10 00:34] falls asleep +[1518-08-14 00:24] falls asleep +[1518-04-16 00:02] Guard #2521 begins shift +[1518-06-18 00:02] Guard #179 begins shift +[1518-05-10 00:45] wakes up +[1518-04-07 00:00] Guard #1009 begins shift +[1518-06-19 00:58] wakes up +[1518-03-12 00:20] falls asleep +[1518-04-10 00:26] falls asleep +[1518-05-09 23:57] Guard #1061 begins shift +[1518-10-22 00:03] Guard #3331 begins shift +[1518-08-12 00:00] Guard #1009 begins shift +[1518-11-14 00:43] falls asleep +[1518-05-07 00:25] falls asleep +[1518-11-10 00:29] wakes up +[1518-09-27 00:31] wakes up +[1518-05-31 00:35] wakes up +[1518-10-26 00:30] falls asleep +[1518-03-24 00:37] wakes up +[1518-11-15 23:57] Guard #1163 begins shift +[1518-09-15 00:48] falls asleep +[1518-09-04 00:49] wakes up +[1518-09-05 00:00] Guard #1913 begins shift +[1518-07-06 00:25] falls asleep +[1518-10-31 00:50] wakes up +[1518-06-09 00:09] wakes up +[1518-05-15 23:57] Guard #1913 begins shift +[1518-08-16 00:00] falls asleep +[1518-10-24 00:13] falls asleep +[1518-04-25 00:03] Guard #1381 begins shift +[1518-11-02 00:10] falls asleep +[1518-03-26 00:57] wakes up +[1518-08-15 00:58] wakes up +[1518-09-30 00:04] Guard #683 begins shift +[1518-05-02 00:20] falls asleep +[1518-09-06 00:25] wakes up +[1518-05-29 00:14] falls asleep +[1518-04-27 00:11] falls asleep +[1518-09-25 00:00] Guard #2269 begins shift +[1518-09-21 00:51] falls asleep +[1518-08-12 00:54] falls asleep +[1518-06-23 00:55] wakes up +[1518-08-31 00:41] wakes up +[1518-05-04 00:38] falls asleep +[1518-03-26 00:00] falls asleep +[1518-11-16 00:15] wakes up +[1518-06-23 00:25] falls asleep +[1518-08-22 00:03] Guard #1061 begins shift +[1518-05-23 00:56] wakes up +[1518-03-22 00:20] wakes up +[1518-05-06 00:41] wakes up +[1518-04-01 00:31] falls asleep +[1518-07-14 00:37] wakes up +[1518-03-26 00:38] wakes up +[1518-09-18 00:33] falls asleep +[1518-09-26 23:51] Guard #3083 begins shift +[1518-03-15 00:35] wakes up +[1518-11-07 00:50] falls asleep +[1518-05-06 23:57] Guard #109 begins shift +[1518-09-20 00:19] falls asleep +[1518-06-14 00:49] wakes up +[1518-10-25 00:51] falls asleep +[1518-08-17 00:01] Guard #2539 begins shift +[1518-04-28 00:33] wakes up +[1518-08-01 00:46] wakes up +[1518-03-01 23:58] Guard #179 begins shift +[1518-10-16 23:47] Guard #109 begins shift +[1518-04-15 00:01] Guard #1381 begins shift +[1518-03-23 00:45] wakes up +[1518-09-10 00:50] wakes up +[1518-05-10 00:56] falls asleep +[1518-05-26 00:44] wakes up +[1518-03-12 00:01] Guard #1009 begins shift +[1518-04-10 00:19] wakes up +[1518-11-23 00:11] falls asleep +[1518-07-11 00:47] wakes up +[1518-09-18 00:43] falls asleep +[1518-03-21 00:32] wakes up +[1518-07-15 00:55] wakes up +[1518-10-09 00:04] falls asleep +[1518-05-14 00:53] falls asleep +[1518-09-18 00:25] wakes up +[1518-08-03 23:52] Guard #1229 begins shift +[1518-04-22 00:01] Guard #863 begins shift +[1518-09-22 00:03] Guard #2539 begins shift +[1518-03-11 00:53] wakes up +[1518-05-27 23:58] Guard #1913 begins shift +[1518-07-14 00:47] falls asleep +[1518-09-25 00:59] wakes up +[1518-06-20 00:55] wakes up +[1518-08-24 00:49] falls asleep +[1518-06-25 00:53] wakes up +[1518-08-28 00:54] wakes up +[1518-07-16 23:46] Guard #701 begins shift +[1518-05-31 00:14] falls asleep +[1518-03-11 00:43] falls asleep +[1518-11-03 00:14] falls asleep +[1518-11-23 00:50] wakes up +[1518-05-07 00:27] wakes up +[1518-04-18 00:41] falls asleep +[1518-05-21 00:53] wakes up +[1518-10-26 00:43] wakes up +[1518-06-17 00:17] falls asleep +[1518-03-22 00:10] falls asleep +[1518-03-14 00:39] falls asleep +[1518-03-31 00:36] wakes up +[1518-10-02 00:56] falls asleep +[1518-07-25 00:17] falls asleep +[1518-05-09 00:29] falls asleep +[1518-04-23 00:04] Guard #179 begins shift +[1518-05-16 23:46] Guard #1229 begins shift +[1518-08-02 00:00] falls asleep +[1518-05-26 00:20] wakes up +[1518-07-29 00:22] falls asleep +[1518-08-29 00:49] wakes up +[1518-08-04 23:46] Guard #701 begins shift +[1518-11-13 23:59] Guard #1229 begins shift +[1518-08-06 23:59] Guard #3083 begins shift +[1518-03-31 00:25] wakes up +[1518-09-17 23:58] Guard #109 begins shift +[1518-09-21 00:23] falls asleep +[1518-06-26 23:47] Guard #109 begins shift +[1518-08-14 00:39] wakes up +[1518-09-20 00:00] Guard #1163 begins shift +[1518-07-28 00:16] falls asleep +[1518-05-08 00:51] wakes up +[1518-06-22 00:47] falls asleep +[1518-10-06 23:59] Guard #179 begins shift +[1518-11-06 00:20] falls asleep +[1518-05-07 00:36] falls asleep +[1518-09-10 00:59] wakes up +[1518-06-22 00:23] falls asleep +[1518-03-04 00:15] falls asleep +[1518-10-23 00:42] falls asleep +[1518-09-17 00:00] Guard #2539 begins shift +[1518-09-22 00:48] wakes up +[1518-04-28 00:50] wakes up +[1518-06-04 00:54] wakes up +[1518-05-19 00:56] wakes up +[1518-09-30 00:24] falls asleep +[1518-05-02 00:41] wakes up +[1518-10-23 00:02] Guard #3083 begins shift +[1518-11-11 00:57] falls asleep +[1518-08-01 23:52] Guard #3167 begins shift +[1518-08-07 00:14] falls asleep +[1518-10-27 23:53] Guard #863 begins shift +[1518-03-30 00:57] wakes up +[1518-05-21 00:42] falls asleep +[1518-10-18 00:03] Guard #1061 begins shift +[1518-05-22 00:00] Guard #3083 begins shift +[1518-07-13 00:41] falls asleep +[1518-03-13 00:15] wakes up +[1518-03-02 00:27] falls asleep +[1518-04-11 00:06] falls asleep +[1518-07-13 00:29] falls asleep +[1518-05-11 00:02] Guard #1163 begins shift +[1518-09-09 00:54] falls asleep +[1518-10-23 23:57] Guard #1061 begins shift +[1518-04-06 00:59] wakes up +[1518-07-03 00:54] wakes up +[1518-06-25 00:22] falls asleep +[1518-07-06 23:57] Guard #1777 begins shift +[1518-07-23 00:15] falls asleep +[1518-10-07 00:33] wakes up +[1518-04-09 00:56] wakes up +[1518-07-30 00:25] falls asleep +[1518-06-04 00:19] falls asleep +[1518-06-23 00:53] falls asleep +[1518-08-22 00:41] wakes up +[1518-06-01 00:42] falls asleep +[1518-10-04 00:04] Guard #1777 begins shift +[1518-06-13 00:02] Guard #1229 begins shift +[1518-11-03 00:00] falls asleep +[1518-07-23 23:58] Guard #3167 begins shift +[1518-10-27 00:46] wakes up +[1518-04-26 00:03] Guard #2269 begins shift +[1518-07-08 00:36] wakes up +[1518-09-13 00:26] falls asleep +[1518-06-15 00:00] Guard #109 begins shift +[1518-10-25 00:54] wakes up +[1518-06-27 00:25] falls asleep +[1518-11-09 00:02] Guard #509 begins shift +[1518-03-24 23:49] Guard #1163 begins shift +[1518-10-11 00:56] wakes up +[1518-06-07 00:58] wakes up +[1518-10-06 00:00] Guard #1061 begins shift +[1518-04-29 00:29] falls asleep +[1518-03-03 23:56] Guard #1061 begins shift +[1518-07-01 23:57] Guard #1381 begins shift +[1518-03-05 23:58] Guard #1229 begins shift +[1518-04-06 00:46] wakes up +[1518-08-18 00:19] wakes up +[1518-05-29 00:15] wakes up +[1518-10-17 00:43] wakes up +[1518-07-29 00:00] Guard #1381 begins shift +[1518-03-31 00:45] falls asleep +[1518-05-26 00:11] falls asleep +[1518-03-07 00:57] wakes up +[1518-09-14 00:00] Guard #2521 begins shift +[1518-05-01 00:19] wakes up +[1518-10-02 00:57] wakes up +[1518-09-18 23:57] Guard #3167 begins shift +[1518-03-19 00:00] Guard #863 begins shift +[1518-03-07 00:55] falls asleep +[1518-09-23 00:18] falls asleep +[1518-04-20 00:26] wakes up +[1518-06-13 00:32] falls asleep +[1518-06-17 00:00] Guard #421 begins shift +[1518-10-16 00:02] falls asleep +[1518-08-14 23:57] Guard #179 begins shift +[1518-03-16 00:14] falls asleep +[1518-10-30 00:21] wakes up +[1518-04-30 00:17] falls asleep +[1518-11-07 00:17] falls asleep +[1518-05-06 00:00] Guard #1163 begins shift +[1518-05-21 00:03] falls asleep +[1518-09-06 00:28] falls asleep +[1518-06-28 00:48] falls asleep +[1518-09-20 00:47] falls asleep +[1518-07-12 00:53] wakes up +[1518-06-29 00:03] Guard #277 begins shift +[1518-05-17 00:44] wakes up +[1518-03-11 00:03] falls asleep +[1518-05-09 00:30] wakes up +[1518-10-11 00:36] falls asleep +[1518-11-11 00:58] wakes up +[1518-03-16 00:31] wakes up +[1518-11-10 00:01] Guard #179 begins shift +[1518-06-06 00:39] wakes up +[1518-03-31 00:58] wakes up +[1518-06-11 00:19] falls asleep +[1518-08-13 00:59] wakes up +[1518-10-25 00:04] Guard #2269 begins shift +[1518-09-24 00:38] wakes up +[1518-05-09 00:57] falls asleep +[1518-10-08 00:33] wakes up +[1518-11-12 00:49] falls asleep +[1518-06-13 23:58] Guard #179 begins shift +[1518-10-13 00:53] wakes up +[1518-05-16 00:59] wakes up +[1518-05-14 00:01] Guard #3167 begins shift +[1518-07-06 00:01] Guard #3167 begins shift +[1518-08-20 00:04] Guard #1381 begins shift +[1518-06-10 00:11] falls asleep +[1518-03-03 00:24] wakes up +[1518-07-22 23:59] Guard #863 begins shift +[1518-04-23 00:31] wakes up +[1518-07-08 00:00] Guard #1229 begins shift +[1518-09-10 00:40] wakes up +[1518-08-09 00:32] wakes up +[1518-07-12 00:45] falls asleep +[1518-10-29 00:39] wakes up +[1518-06-11 00:00] Guard #421 begins shift +[1518-07-07 00:16] falls asleep +[1518-07-30 00:52] wakes up +[1518-04-27 23:59] Guard #701 begins shift +[1518-08-26 00:59] wakes up +[1518-06-01 00:50] wakes up +[1518-03-10 00:01] Guard #3167 begins shift +[1518-05-05 00:02] Guard #109 begins shift +[1518-07-13 23:59] Guard #109 begins shift +[1518-03-18 00:10] wakes up +[1518-08-01 00:27] wakes up +[1518-10-12 00:14] falls asleep +[1518-05-17 00:53] falls asleep +[1518-06-09 00:05] falls asleep +[1518-10-11 00:33] wakes up +[1518-07-09 23:52] Guard #1061 begins shift +[1518-07-12 00:03] Guard #1061 begins shift +[1518-05-11 23:57] Guard #3167 begins shift +[1518-04-26 00:30] wakes up +[1518-09-07 00:52] falls asleep +[1518-07-22 00:00] Guard #277 begins shift +[1518-08-14 00:00] Guard #179 begins shift +[1518-05-09 00:48] falls asleep +[1518-09-21 00:41] wakes up +[1518-07-30 23:51] Guard #683 begins shift +[1518-04-27 00:57] wakes up +[1518-04-26 00:08] falls asleep +[1518-09-26 00:31] wakes up +[1518-07-12 00:27] wakes up +[1518-10-23 00:50] wakes up +[1518-10-28 00:05] falls asleep +[1518-09-13 00:40] wakes up +[1518-11-07 00:33] wakes up +[1518-03-24 00:00] Guard #1913 begins shift +[1518-04-15 00:40] wakes up +[1518-04-26 00:56] wakes up +[1518-09-24 00:00] Guard #421 begins shift +[1518-09-20 23:57] Guard #863 begins shift +[1518-03-22 00:02] Guard #1777 begins shift +[1518-09-10 00:54] falls asleep +[1518-11-17 00:05] falls asleep +[1518-06-22 00:48] wakes up +[1518-04-15 00:44] falls asleep +[1518-11-09 00:07] falls asleep +[1518-09-15 00:55] wakes up +[1518-09-25 00:56] falls asleep +[1518-10-01 23:57] Guard #1777 begins shift +[1518-04-28 23:47] Guard #2539 begins shift +[1518-03-06 00:59] wakes up +[1518-08-13 00:01] Guard #277 begins shift +[1518-11-08 00:27] falls asleep +[1518-07-13 00:00] Guard #179 begins shift +[1518-08-12 00:57] wakes up +[1518-04-13 00:39] wakes up +[1518-05-09 00:49] wakes up +[1518-07-19 00:25] falls asleep +[1518-08-22 00:24] falls asleep +[1518-03-17 00:11] falls asleep +[1518-03-31 00:21] falls asleep +[1518-08-15 23:49] Guard #109 begins shift +[1518-04-18 23:57] Guard #2269 begins shift +[1518-06-19 00:44] wakes up +[1518-10-15 23:47] Guard #179 begins shift +[1518-05-11 00:54] wakes up +[1518-09-14 23:56] Guard #1163 begins shift +[1518-05-06 00:13] falls asleep +[1518-10-11 00:04] Guard #3167 begins shift +[1518-05-19 00:45] falls asleep +[1518-05-04 00:01] Guard #683 begins shift +[1518-03-09 00:58] wakes up +[1518-04-26 00:37] falls asleep +[1518-07-16 00:03] Guard #109 begins shift +[1518-09-10 00:21] falls asleep +[1518-11-17 00:53] falls asleep +[1518-09-29 00:53] wakes up +[1518-10-08 23:50] Guard #3083 begins shift +[1518-05-14 23:58] Guard #2269 begins shift +[1518-03-22 00:27] falls asleep +[1518-09-28 00:45] wakes up +[1518-05-01 00:03] falls asleep +[1518-11-02 00:38] wakes up +[1518-08-30 00:10] falls asleep +[1518-05-19 23:56] Guard #1229 begins shift +[1518-10-13 00:43] falls asleep +[1518-10-14 00:30] falls asleep +[1518-09-19 00:54] wakes up +[1518-10-15 00:26] wakes up +[1518-03-18 00:25] falls asleep +[1518-05-04 00:10] falls asleep +[1518-08-03 00:37] falls asleep +[1518-05-10 00:19] falls asleep +[1518-11-04 00:00] Guard #863 begins shift +[1518-06-10 00:26] wakes up +[1518-10-17 00:02] falls asleep +[1518-03-31 23:56] Guard #1229 begins shift +[1518-05-06 00:59] wakes up +[1518-07-01 00:33] falls asleep +[1518-03-19 00:39] falls asleep +[1518-08-29 00:02] Guard #2539 begins shift +[1518-06-27 00:03] falls asleep +[1518-05-04 00:22] wakes up +[1518-08-17 00:27] wakes up +[1518-08-21 00:08] falls asleep +[1518-05-03 00:02] falls asleep +[1518-07-06 00:52] wakes up +[1518-04-17 00:18] wakes up +[1518-04-27 00:38] falls asleep +[1518-04-24 00:33] falls asleep +[1518-09-11 00:30] falls asleep +[1518-06-24 00:58] wakes up +[1518-04-03 00:01] Guard #1667 begins shift +[1518-03-26 00:46] wakes up +[1518-07-17 00:09] wakes up +[1518-08-02 00:55] wakes up +[1518-11-21 00:11] falls asleep +[1518-04-04 00:02] falls asleep +[1518-07-14 00:24] wakes up +[1518-10-27 00:57] wakes up +[1518-05-24 00:53] wakes up +[1518-11-02 23:53] Guard #3083 begins shift +[1518-11-22 00:05] falls asleep +[1518-07-25 00:03] Guard #509 begins shift +[1518-05-11 00:26] falls asleep +[1518-04-18 00:48] wakes up +[1518-05-30 00:23] falls asleep +[1518-06-11 00:40] wakes up +[1518-04-30 00:50] falls asleep +[1518-06-17 00:58] wakes up +[1518-03-11 00:15] falls asleep +[1518-06-09 00:27] falls asleep +[1518-11-10 00:13] falls asleep +[1518-10-06 00:48] falls asleep +[1518-04-29 00:58] wakes up +[1518-11-06 23:57] Guard #3083 begins shift +[1518-07-16 00:37] wakes up +[1518-07-25 23:57] Guard #2269 begins shift +[1518-09-29 00:03] falls asleep +[1518-03-14 00:58] wakes up +[1518-03-10 00:28] wakes up +[1518-07-06 00:45] falls asleep +[1518-03-02 00:32] wakes up +[1518-05-24 00:02] falls asleep +[1518-03-28 00:12] falls asleep +[1518-11-20 00:47] wakes up +[1518-11-18 00:51] falls asleep +[1518-07-06 00:22] wakes up +[1518-04-07 00:32] wakes up +[1518-06-18 00:19] falls asleep +[1518-11-17 23:56] Guard #2539 begins shift +[1518-09-03 00:40] wakes up +[1518-07-01 00:01] Guard #509 begins shift +[1518-09-19 00:23] falls asleep +[1518-05-21 00:10] wakes up +[1518-03-17 00:53] wakes up +[1518-06-25 00:14] wakes up +[1518-04-12 00:22] wakes up +[1518-04-21 00:19] wakes up +[1518-05-29 00:35] wakes up +[1518-08-16 00:47] wakes up +[1518-08-30 00:03] Guard #179 begins shift +[1518-09-01 00:00] falls asleep +[1518-08-17 00:54] wakes up +[1518-08-18 00:53] wakes up +[1518-04-24 00:28] wakes up +[1518-03-24 00:48] wakes up +[1518-05-02 00:24] wakes up +[1518-06-10 00:00] Guard #3331 begins shift +[1518-04-05 00:37] wakes up +[1518-10-06 00:55] wakes up +[1518-08-23 00:01] Guard #1667 begins shift +[1518-07-21 00:04] Guard #1009 begins shift +[1518-09-25 00:12] falls asleep +[1518-09-16 00:03] Guard #2539 begins shift +[1518-07-17 00:51] wakes up +[1518-03-20 00:13] falls asleep +[1518-05-06 00:56] falls asleep +[1518-11-20 00:09] wakes up +[1518-09-04 00:00] Guard #1913 begins shift +[1518-08-17 23:56] Guard #701 begins shift +[1518-11-03 00:27] wakes up +[1518-04-23 23:59] Guard #1381 begins shift +[1518-04-27 00:03] Guard #1381 begins shift +[1518-07-20 00:02] Guard #1667 begins shift +[1518-08-31 00:29] falls asleep +[1518-08-07 00:23] wakes up +[1518-10-01 00:38] wakes up +[1518-10-30 00:06] falls asleep +[1518-03-04 00:54] wakes up +[1518-05-27 00:04] Guard #3331 begins shift +[1518-03-24 00:59] wakes up +[1518-04-12 00:27] falls asleep +[1518-05-16 00:07] falls asleep +[1518-08-19 00:00] Guard #421 begins shift +[1518-03-27 23:58] Guard #1913 begins shift +[1518-05-23 00:08] falls asleep +[1518-04-19 00:26] falls asleep +[1518-09-09 00:02] Guard #863 begins shift +[1518-05-28 23:57] Guard #421 begins shift +[1518-10-15 00:00] Guard #3167 begins shift +[1518-11-21 00:03] Guard #3083 begins shift +[1518-04-04 00:32] wakes up +[1518-09-17 00:38] falls asleep +[1518-03-16 23:56] Guard #2539 begins shift +[1518-11-05 00:46] wakes up +[1518-09-28 23:52] Guard #109 begins shift +[1518-04-29 00:23] wakes up +[1518-07-28 00:57] falls asleep +[1518-09-30 00:46] wakes up +[1518-09-05 00:23] wakes up +[1518-06-06 00:22] falls asleep +[1518-08-30 23:57] Guard #1163 begins shift +[1518-03-03 00:23] falls asleep +[1518-08-05 00:51] falls asleep +[1518-07-03 00:38] falls asleep +[1518-04-29 00:00] falls asleep +[1518-06-21 00:54] wakes up +[1518-10-01 00:14] falls asleep +[1518-05-20 00:09] falls asleep +[1518-10-04 00:26] falls asleep +[1518-09-16 00:39] falls asleep +[1518-07-18 00:59] wakes up +[1518-04-22 00:53] wakes up +[1518-07-09 00:00] Guard #2269 begins shift +[1518-08-28 00:15] falls asleep +[1518-11-21 00:12] wakes up +[1518-03-28 23:56] Guard #1913 begins shift +[1518-06-23 00:38] falls asleep +[1518-06-02 23:58] Guard #1187 begins shift +[1518-08-25 00:13] wakes up +[1518-08-17 00:26] falls asleep +[1518-04-09 00:00] Guard #1913 begins shift +[1518-07-12 00:06] falls asleep +[1518-07-18 00:12] falls asleep +[1518-08-31 23:53] Guard #2539 begins shift +[1518-11-18 00:53] wakes up +[1518-06-25 23:49] Guard #509 begins shift +[1518-06-11 00:47] wakes up +[1518-05-25 00:34] wakes up +[1518-07-13 00:43] wakes up +[1518-09-08 00:00] Guard #1187 begins shift +[1518-07-24 00:10] falls asleep +[1518-08-01 00:09] falls asleep +[1518-04-05 23:57] Guard #1009 begins shift +[1518-09-02 00:19] falls asleep +[1518-03-22 23:57] Guard #3083 begins shift +[1518-05-31 00:03] Guard #683 begins shift +[1518-06-01 00:13] falls asleep +[1518-04-17 00:51] wakes up +[1518-03-18 00:05] falls asleep +[1518-05-07 00:16] falls asleep +[1518-08-29 00:06] falls asleep +[1518-03-31 00:33] falls asleep +[1518-10-17 00:38] falls asleep +[1518-03-13 00:40] falls asleep +[1518-09-11 00:46] wakes up +[1518-05-14 00:49] wakes up +[1518-05-17 00:04] falls asleep +[1518-10-14 00:41] wakes up +[1518-11-13 00:57] wakes up +[1518-10-12 00:01] Guard #1381 begins shift +[1518-09-09 00:56] wakes up +[1518-11-07 00:51] wakes up +[1518-09-26 00:43] falls asleep +[1518-04-02 00:02] falls asleep +[1518-03-18 00:29] wakes up +[1518-03-21 00:28] falls asleep +[1518-03-06 00:35] wakes up +[1518-03-08 00:48] wakes up +[1518-05-07 00:44] wakes up +[1518-08-20 23:58] Guard #179 begins shift +[1518-06-26 00:00] falls asleep +[1518-08-18 00:52] falls asleep +[1518-07-14 00:27] falls asleep +[1518-04-12 00:11] falls asleep +[1518-11-16 00:10] falls asleep +[1518-03-27 00:51] wakes up +[1518-03-24 00:31] falls asleep +[1518-06-22 23:59] Guard #509 begins shift +[1518-04-17 00:04] Guard #2269 begins shift +[1518-06-30 00:15] falls asleep +[1518-10-20 00:13] falls asleep +[1518-11-06 00:55] wakes up +[1518-10-21 00:03] Guard #1229 begins shift +[1518-03-15 00:52] wakes up +[1518-08-26 00:15] falls asleep +[1518-07-27 00:26] falls asleep +[1518-07-31 00:03] falls asleep +[1518-04-02 00:51] wakes up +[1518-07-02 00:45] wakes up +[1518-03-05 00:41] wakes up +[1518-08-29 00:36] falls asleep +[1518-04-18 00:31] wakes up +[1518-06-08 00:00] Guard #1187 begins shift +[1518-09-12 00:08] falls asleep +[1518-11-09 00:59] wakes up +[1518-10-27 00:02] Guard #421 begins shift +[1518-11-20 00:00] falls asleep +[1518-09-25 23:59] Guard #1777 begins shift +[1518-11-01 00:00] Guard #1913 begins shift +[1518-06-16 00:00] Guard #2539 begins shift +[1518-09-11 23:56] Guard #2539 begins shift +[1518-08-20 00:12] falls asleep +[1518-07-18 23:56] Guard #3083 begins shift +[1518-07-19 00:36] wakes up +[1518-06-14 00:14] falls asleep +[1518-06-05 00:41] wakes up +[1518-05-06 00:48] falls asleep +[1518-11-12 23:56] Guard #1777 begins shift +[1518-04-20 00:06] falls asleep +[1518-07-15 00:17] wakes up +[1518-06-29 23:58] Guard #1163 begins shift +[1518-08-27 00:48] falls asleep +[1518-09-16 00:59] wakes up +[1518-05-09 00:59] wakes up +[1518-05-29 23:57] Guard #863 begins shift +[1518-05-18 00:58] wakes up +[1518-10-01 00:02] Guard #179 begins shift +[1518-07-09 00:55] falls asleep +[1518-10-16 00:37] wakes up +[1518-03-08 23:59] Guard #3167 begins shift +[1518-07-28 00:59] wakes up +[1518-03-26 00:55] falls asleep +[1518-08-05 00:38] wakes up +[1518-04-11 00:58] wakes up +[1518-11-14 23:54] Guard #863 begins shift +[1518-06-05 23:59] Guard #277 begins shift +[1518-10-18 00:57] wakes up +[1518-08-11 00:40] wakes up +[1518-08-19 00:35] wakes up +[1518-06-06 23:56] Guard #179 begins shift +[1518-11-11 23:56] Guard #2269 begins shift +[1518-05-18 00:15] falls asleep +[1518-07-28 00:52] wakes up +[1518-07-09 00:25] falls asleep +[1518-03-09 00:21] falls asleep +[1518-09-02 00:37] wakes up +[1518-04-17 00:09] falls asleep +[1518-04-05 00:17] wakes up +[1518-05-12 00:56] wakes up +[1518-04-30 00:46] wakes up +[1518-07-18 00:57] falls asleep +[1518-07-11 00:37] falls asleep +[1518-10-13 00:12] falls asleep +[1518-04-09 23:59] Guard #1913 begins shift +[1518-08-10 00:04] Guard #421 begins shift +[1518-10-05 00:35] wakes up +[1518-08-27 00:54] wakes up +[1518-10-07 23:57] Guard #179 begins shift +[1518-10-05 00:23] falls asleep +[1518-06-11 00:45] falls asleep +[1518-09-01 00:51] wakes up +[1518-10-28 23:58] Guard #277 begins shift +[1518-09-11 00:03] Guard #179 begins shift +[1518-05-14 00:54] wakes up +[1518-11-14 00:23] falls asleep diff --git a/aoc2018/src/day04.rs b/aoc2018/src/day04.rs new file mode 100644 index 0000000..0249f6d --- /dev/null +++ b/aoc2018/src/day04.rs @@ -0,0 +1,216 @@ +use std::collections::HashMap; +use std::error::Error; +use std::str::FromStr; + +use super::err; +use super::Result; + +const INPUT: &str = include_str!("../input/day04.txt"); + +fn sorted_lines(input: &str) -> String { + let mut lines: Vec<_> = input.lines().collect(); + lines.sort_unstable(); + lines.join("\n") +} + +pub fn run() -> Result<()> { + println!("part 1: {}", part1(&sorted_lines(INPUT))?); + + Ok(()) +} + +#[derive(Debug)] +enum Event { + ShiftChange(u64), + FallAsleep, + WakeUp, +} + +impl FromStr for Event { + type Err = Box; + + fn from_str(s: &str) -> Result { + if s.find("wakes up").is_some() { + Ok(Event::WakeUp) + } else if s.find("falls asleep").is_some() { + Ok(Event::FallAsleep) + } else if s.find("begins shift").is_some() { + let pound = s.find('#').ok_or_else(|| err!("`#` not found"))?; + let s = &s[(pound + 1)..]; + let space = s.find(' ').ok_or_else(|| err!("` ` not found after `#`"))?; + let id = s[..space].parse()?; + Ok(Event::ShiftChange(id)) + } else { + Err(err!("unknown event type")) + } + } +} + +#[derive(Debug)] +struct Date { + year: u32, + month: u8, + day: u8, + + hour: u8, + minute: u8, +} + +impl FromStr for Date { + type Err = Box; + + fn from_str(s: &str) -> Result { + let lbracket = s.find('[').ok_or_else(|| err!("`[` not found"))?; + let s = &s[(lbracket + 1)..]; + let dash = s.find('-').ok_or_else(|| err!("`-` not found"))?; + + let year = s[..dash].parse()?; + let s = &s[(dash + 1)..]; + let dash = s.find('-').ok_or_else(|| err!("`-` not found"))?; + + let month = s[..dash].parse()?; + let s = &s[(dash + 1)..]; + let space = s.find(' ').ok_or_else(|| err!("` ` not found"))?; + + let day = s[..space].parse()?; + let s = &s[(space + 1)..]; + let colon = s.find(':').ok_or_else(|| err!("`:` not found"))?; + + let hour = s[..colon].parse()?; + let s = &s[(colon + 1)..]; + let rbracket = s.find(']').ok_or_else(|| err!("`]` not found"))?; + + let minute = s[..rbracket].parse()?; + + Ok(Date { + year, + month, + day, + hour, + minute, + }) + } +} + +#[derive(Debug)] +struct LogEntry { + date: Date, + event: Event, +} + +impl FromStr for LogEntry { + type Err = Box; + + fn from_str(s: &str) -> Result { + let date: Date = s.parse().map_err(|e| err!("couldn't parse date: {}", e))?; + let event = s.parse().map_err(|e| err!("couldn't parse event: {}", e))?; + + let entry = LogEntry { date, event }; + + match entry.event { + Event::FallAsleep | Event::WakeUp => { + assert!(entry.date.hour == 0); + } + _ => {} + }; + + Ok(entry) + } +} + +fn part1(input: &str) -> Result { + let mut guard_id = None; + let mut map: HashMap> = HashMap::new(); + + for line in input.lines() { + let log_entry: LogEntry = line.parse()?; + + if let Event::ShiftChange(id) = log_entry.event { + guard_id = Some(id); + } + + match guard_id { + Some(id) => map.entry(id).or_default().push(log_entry), + None => return Err(err!("event before first shift")), + } + } + + // Fill frequency by minute and by guard + let mut sleep_freq_per_guard = HashMap::new(); + + for (id, log_entries) in map { + let mut fell_asleep = None; + let mut sleep_freq: HashMap = HashMap::new(); + + for e in log_entries { + match e.event { + Event::ShiftChange(_) => fell_asleep = None, // new day! + Event::FallAsleep => fell_asleep = Some(e.date.minute), + Event::WakeUp => match fell_asleep { + Some(asleep) => { + let awake = e.date.minute; + for m in asleep..awake { + *sleep_freq.entry(m).or_default() += 1; + } + fell_asleep = None; + } + None => return Err(err!("woke up before falling asleep")), + }, + } + } + + if fell_asleep.is_some() { + return Err(err!("fell asleep but never woke up!")); + } + + sleep_freq_per_guard.insert(id, sleep_freq); + } + + let (heavy_sleeper, _) = sleep_freq_per_guard + .iter() + .max_by_key(|(_, freqs)| freqs.values().sum::()) + .unwrap(); + + let heavy_sleeper_freq = &sleep_freq_per_guard[heavy_sleeper]; + + let (best_minute, _) = heavy_sleeper_freq + .iter() + .max_by_key(|(_, freq)| *freq) + .unwrap(); + + Ok((*best_minute as u64) * heavy_sleeper) +} + +#[cfg(test)] +mod tests { + use super::*; + + const PROVIDED: &str = "[1518-11-01 00:00] Guard #10 begins shift +[1518-11-01 00:05] falls asleep +[1518-11-01 00:25] wakes up +[1518-11-01 00:30] falls asleep +[1518-11-01 00:55] wakes up +[1518-11-01 23:58] Guard #99 begins shift +[1518-11-02 00:40] falls asleep +[1518-11-02 00:50] wakes up +[1518-11-03 00:05] Guard #10 begins shift +[1518-11-03 00:24] falls asleep +[1518-11-03 00:29] wakes up +[1518-11-04 00:02] Guard #99 begins shift +[1518-11-04 00:36] falls asleep +[1518-11-04 00:46] wakes up +[1518-11-05 00:03] Guard #99 begins shift +[1518-11-05 00:45] falls asleep +[1518-11-05 00:55] wakes up +"; + + #[test] + fn part1_provided() { + assert_eq!(part1(PROVIDED).unwrap(), 240); + } + + #[test] + fn part1_real() { + assert_eq!(part1(&sorted_lines(INPUT)).unwrap(), 142515); + } +} diff --git a/aoc2018/src/lib.rs b/aoc2018/src/lib.rs index cc2f62a..69d6b3b 100644 --- a/aoc2018/src/lib.rs +++ b/aoc2018/src/lib.rs @@ -1,6 +1,7 @@ pub mod day01; pub mod day02; pub mod day03; +pub mod day04; pub mod day05; pub type Result = std::result::Result>; diff --git a/aoc2018/src/main.rs b/aoc2018/src/main.rs index 057a168..15a9db4 100644 --- a/aoc2018/src/main.rs +++ b/aoc2018/src/main.rs @@ -3,12 +3,13 @@ use std::env; use aoc2018::day01; use aoc2018::day02; use aoc2018::day03; +use aoc2018::day04; use aoc2018::day05; use aoc2018::Result; fn main() -> Result<()> { - let days: &[fn() -> Result<()>] = &[day01::run, day02::run, day03::run, day05::run]; + let days: &[fn() -> Result<()>] = &[day01::run, day02::run, day03::run, day04::run, day05::run]; let mut args = env::args(); args.next();