AoC day 4
This commit is contained in:
37
AdventOfCode2018/4thDay/challenge1/.vscode/settings.json
vendored
Normal file
37
AdventOfCode2018/4thDay/challenge1/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"cctype": "cpp",
|
||||||
|
"clocale": "cpp",
|
||||||
|
"cmath": "cpp",
|
||||||
|
"cstddef": "cpp",
|
||||||
|
"cstdio": "cpp",
|
||||||
|
"cstdlib": "cpp",
|
||||||
|
"cwchar": "cpp",
|
||||||
|
"cwctype": "cpp",
|
||||||
|
"array": "cpp",
|
||||||
|
"*.tcc": "cpp",
|
||||||
|
"cstdint": "cpp",
|
||||||
|
"deque": "cpp",
|
||||||
|
"unordered_map": "cpp",
|
||||||
|
"vector": "cpp",
|
||||||
|
"exception": "cpp",
|
||||||
|
"fstream": "cpp",
|
||||||
|
"initializer_list": "cpp",
|
||||||
|
"iosfwd": "cpp",
|
||||||
|
"iostream": "cpp",
|
||||||
|
"istream": "cpp",
|
||||||
|
"limits": "cpp",
|
||||||
|
"new": "cpp",
|
||||||
|
"optional": "cpp",
|
||||||
|
"ostream": "cpp",
|
||||||
|
"sstream": "cpp",
|
||||||
|
"stdexcept": "cpp",
|
||||||
|
"streambuf": "cpp",
|
||||||
|
"string_view": "cpp",
|
||||||
|
"system_error": "cpp",
|
||||||
|
"type_traits": "cpp",
|
||||||
|
"tuple": "cpp",
|
||||||
|
"typeinfo": "cpp",
|
||||||
|
"utility": "cpp"
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
AdventOfCode2018/4thDay/challenge1/a.out
Executable file
BIN
AdventOfCode2018/4thDay/challenge1/a.out
Executable file
Binary file not shown.
49
AdventOfCode2018/4thDay/challenge1/challenge.txt
Executable file
49
AdventOfCode2018/4thDay/challenge1/challenge.txt
Executable file
@@ -0,0 +1,49 @@
|
|||||||
|
--- Day 4: Repose Record ---
|
||||||
|
You've sneaked into another supply closet - this time, it's across from the prototype suit manufacturing lab. You need to sneak inside and fix the issues with the suit, but there's a guard stationed outside the lab, so this is as close as you can safely get.
|
||||||
|
|
||||||
|
As you search the closet for anything that might help, you discover that you're not the first person to want to sneak in. Covering the walls, someone has spent an hour starting every midnight for the past few months secretly observing this guard post! They've been writing down the ID of the one guard on duty that night - the Elves seem to have decided that one guard was enough for the overnight shift - as well as when they fall asleep or wake up while at their post (your puzzle input).
|
||||||
|
|
||||||
|
For example, consider the following records, which have already been organized into chronological order:
|
||||||
|
|
||||||
|
[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
|
||||||
|
Timestamps are written using year-month-day hour:minute format. The guard falling asleep or waking up is always the one whose shift most recently started. Because all asleep/awake times are during the midnight hour (00:00 - 00:59), only the minute portion (00 - 59) is relevant for those events.
|
||||||
|
|
||||||
|
Visually, these records show that the guards are asleep at these times:
|
||||||
|
|
||||||
|
Date ID Minute
|
||||||
|
000000000011111111112222222222333333333344444444445555555555
|
||||||
|
012345678901234567890123456789012345678901234567890123456789
|
||||||
|
11-01 #10 .....####################.....#########################.....
|
||||||
|
11-02 #99 ........................................##########..........
|
||||||
|
11-03 #10 ........................#####...............................
|
||||||
|
11-04 #99 ....................................##########..............
|
||||||
|
11-05 #99 .............................................##########.....
|
||||||
|
The columns are Date, which shows the month-day portion of the relevant day; ID, which shows the guard on duty that day; and Minute, which shows the minutes during which the guard was asleep within the midnight hour. (The Minute column's header shows the minute's ten's digit in the first row and the one's digit in the second row.) Awake is shown as ., and asleep is shown as #.
|
||||||
|
|
||||||
|
Note that guards count as asleep on the minute they fall asleep, and they count as awake on the minute they wake up. For example, because Guard #10 wakes up at 00:25 on 1518-11-01, minute 25 is marked as awake.
|
||||||
|
|
||||||
|
If you can figure out the guard most likely to be asleep at a specific time, you might be able to trick that guard into working tonight so you can have the best chance of sneaking in. You have two strategies for choosing the best guard/minute combination.
|
||||||
|
|
||||||
|
Strategy 1: Find the guard that has the most minutes asleep. What minute does that guard spend asleep the most?
|
||||||
|
|
||||||
|
In the example above, Guard #10 spent the most minutes asleep, a total of 50 minutes (20+25+5), while Guard #99 only slept for a total of 30 minutes (10+10+10). Guard #10 was asleep most during minute 24 (on two days, whereas any other minute the guard was asleep was only seen on one day).
|
||||||
|
|
||||||
|
While this example listed the entries in chronological order, your entries are in the order you found them. You'll need to organize them before they can be analyzed.
|
||||||
|
|
||||||
|
What is the ID of the guard you chose multiplied by the minute you chose? (In the above example, the answer would be 10 * 24 = 240.)
|
||||||
71
AdventOfCode2018/4thDay/challenge1/index.js
Normal file
71
AdventOfCode2018/4thDay/challenge1/index.js
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
class Guard {
|
||||||
|
constructor (id) {
|
||||||
|
this.id = id;
|
||||||
|
this.minutesAsleep = Array.from({ length: 60 }).map(() => 0);
|
||||||
|
this.fellAsleepAt = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
sleep (fellAsleepAt) {
|
||||||
|
this.fellAsleepAt = fellAsleepAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
awake (awokeAt) {
|
||||||
|
for (let i = this.fellAsleepAt; i < awokeAt; i++) {
|
||||||
|
this.minutesAsleep[i] += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
get totalSleepMinutes () {
|
||||||
|
return this.minutesAsleep.reduce((a, b) => a + b, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
get sleepiestMinute () {
|
||||||
|
return this.minutesAsleep
|
||||||
|
.map((value, index) => ({ index, value }))
|
||||||
|
.sort((a, b) => b.value - a.value)[0].index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const sleep = (input) => {
|
||||||
|
const guards = {};
|
||||||
|
const schedule = input
|
||||||
|
.split('\n')
|
||||||
|
.map((x) => x.trim())
|
||||||
|
.sort()
|
||||||
|
.map((x) => {
|
||||||
|
const parts = x.match(/\[(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})\] (.*)/);
|
||||||
|
|
||||||
|
return {
|
||||||
|
minute: +parts[5],
|
||||||
|
message: parts[6],
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
let currentGuard = null;
|
||||||
|
|
||||||
|
for (let i = 0; i < schedule.length; i++) {
|
||||||
|
const { message, minute } = schedule[i];
|
||||||
|
|
||||||
|
if (/Guard/.test(message)) {
|
||||||
|
const parts = message.match(/Guard #(\d+) begins shift/);
|
||||||
|
const guardId = +parts[1];
|
||||||
|
const guard = guards[guardId] ? guards[guardId] : new Guard(guardId);
|
||||||
|
|
||||||
|
currentGuard = guards[guardId] = guard;
|
||||||
|
} else if (message === 'falls asleep') {
|
||||||
|
currentGuard.sleep(minute);
|
||||||
|
} else if (message === 'wakes up') {
|
||||||
|
currentGuard.awake(minute);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const sleepiestGuard = Object
|
||||||
|
.keys(guards)
|
||||||
|
.map((guardId) => guards[guardId])
|
||||||
|
.sort((a, b) => b.totalSleepMinutes - a.totalSleepMinutes)[0];
|
||||||
|
|
||||||
|
return sleepiestGuard.id * sleepiestGuard.sleepiestMinute;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fs = require('fs');
|
||||||
|
console.log(sleep(fs.readFileSync("input.txt").toString()));
|
||||||
|
|
||||||
1000
AdventOfCode2018/4thDay/challenge1/input.txt
Executable file
1000
AdventOfCode2018/4thDay/challenge1/input.txt
Executable file
File diff suppressed because it is too large
Load Diff
180
AdventOfCode2018/4thDay/challenge1/main.cpp
Normal file
180
AdventOfCode2018/4thDay/challenge1/main.cpp
Normal file
@@ -0,0 +1,180 @@
|
|||||||
|
#include <algorithm>
|
||||||
|
#include <iterator>
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
|
enum class State {begin, sleep, awaken};
|
||||||
|
struct Action
|
||||||
|
{
|
||||||
|
State state;
|
||||||
|
int64_t guard_id;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Event
|
||||||
|
{
|
||||||
|
int64_t month, day, hour, minute;
|
||||||
|
Action action;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
std::istream &operator>>(std::istream &is, Action &action)
|
||||||
|
{
|
||||||
|
std::string element;
|
||||||
|
is >> element;
|
||||||
|
if(element=="Guard")
|
||||||
|
{
|
||||||
|
action.state=State::begin;
|
||||||
|
char c;
|
||||||
|
is >> c >> action.guard_id;
|
||||||
|
}
|
||||||
|
else if(element=="falls")
|
||||||
|
{
|
||||||
|
action.state=State::sleep;
|
||||||
|
}
|
||||||
|
else if(element=="wakes")
|
||||||
|
{
|
||||||
|
action.state=State::awaken;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Invalid input");
|
||||||
|
}
|
||||||
|
std::getline(is,element);
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::istream &operator>>(std::istream &is, Event &event)
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
int64_t year;
|
||||||
|
is >> c;
|
||||||
|
if(is.good())
|
||||||
|
{
|
||||||
|
is >> year >> c >> event.month >> c >> event.day
|
||||||
|
>> event.hour >> c >> event.minute >> c
|
||||||
|
>> event.action;
|
||||||
|
|
||||||
|
if(event.hour==23)
|
||||||
|
{
|
||||||
|
event.minute=0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
std::ostream &operator<<(std::ostream &os, const Action &action)
|
||||||
|
{
|
||||||
|
switch(action.state)
|
||||||
|
{
|
||||||
|
case State::begin:
|
||||||
|
os << "Guard #" << action.guard_id << " begins shift";
|
||||||
|
break;
|
||||||
|
case State::sleep:
|
||||||
|
os << "falls asleep";
|
||||||
|
break;
|
||||||
|
case State::awaken:
|
||||||
|
os << "wakes up";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
os << "What?";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostream &operator<<(std::ostream &os, const Event &event)
|
||||||
|
{
|
||||||
|
os << "[1518-"
|
||||||
|
<< std::setw(2) << std::setfill('0') << event.month << "-"
|
||||||
|
<< std::setw(2) << std::setfill('0') << event.day << " "
|
||||||
|
<< std::setw(2) << std::setfill('0') << event.hour << ":"
|
||||||
|
<< std::setw(2) << std::setfill('0') << event.minute << "] "
|
||||||
|
<< event.action;
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::pair<int64_t,int64_t> max_incident(const std::vector<std::pair<int64_t,
|
||||||
|
int64_t>> &incidents)
|
||||||
|
{
|
||||||
|
std::vector<int64_t> sleep_incidents(60,0);
|
||||||
|
for(auto ×: incidents)
|
||||||
|
{
|
||||||
|
for(size_t time=times.first; time<times.second; ++time)
|
||||||
|
{ ++(sleep_incidents[time]); }
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t max_incidents(0), max_minute(0);
|
||||||
|
for(size_t time=0; time<sleep_incidents.size(); ++time)
|
||||||
|
{
|
||||||
|
if(max_incidents<sleep_incidents[time])
|
||||||
|
{
|
||||||
|
max_incidents=sleep_incidents[time];
|
||||||
|
max_minute=time;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return std::make_pair(max_incidents,max_minute);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
std::ifstream infile(argv[1]);
|
||||||
|
std::vector<Event> events(std::istream_iterator<Event>(infile), {});
|
||||||
|
|
||||||
|
std::map<int,std::vector<std::pair<int64_t,int64_t>>> guards;
|
||||||
|
|
||||||
|
int64_t guard_id;
|
||||||
|
int64_t start(-1);
|
||||||
|
for(auto &event: events)
|
||||||
|
{
|
||||||
|
switch(event.action.state)
|
||||||
|
{
|
||||||
|
case State::begin:
|
||||||
|
guard_id=event.action.guard_id;
|
||||||
|
break;
|
||||||
|
case State::sleep:
|
||||||
|
start=event.minute;
|
||||||
|
break;
|
||||||
|
case State::awaken:
|
||||||
|
guards[guard_id].emplace_back(start,event.minute);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t max_time_guard, max_time(0);
|
||||||
|
for(auto &guard: guards)
|
||||||
|
{
|
||||||
|
int64_t total_time(0);
|
||||||
|
for(auto &time: guard.second)
|
||||||
|
{
|
||||||
|
total_time+=time.second-time.first;
|
||||||
|
}
|
||||||
|
if(total_time>max_time)
|
||||||
|
{
|
||||||
|
max_time_guard=guard.first;
|
||||||
|
max_time=total_time;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto [max_incidents,max_minute]=max_incident(guards[max_time_guard]);
|
||||||
|
std::cout << "Part 1: " << (max_minute*max_time_guard) << "\n";
|
||||||
|
|
||||||
|
std::vector<int64_t> num_incidents(guards.size());
|
||||||
|
int64_t max_guard_incidents(0), max_guard_minute(0), max_guard_id(-1);
|
||||||
|
for(auto &guard: guards)
|
||||||
|
{
|
||||||
|
auto [incidents,minute]=max_incident(guard.second);
|
||||||
|
if(incidents>max_guard_incidents)
|
||||||
|
{
|
||||||
|
max_guard_incidents=incidents;
|
||||||
|
max_guard_minute=minute;
|
||||||
|
max_guard_id=guard.first;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout << "Part 2: " << (max_guard_id * max_guard_minute) << "\n";
|
||||||
|
}
|
||||||
7
AdventOfCode2018/4thDay/challenge2/challenge.txt
Normal file
7
AdventOfCode2018/4thDay/challenge2/challenge.txt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
--- Part Two ---
|
||||||
|
Strategy 2: Of all guards, which guard is most frequently asleep on the same minute?
|
||||||
|
|
||||||
|
In the example above, Guard #99 spent minute 45 asleep more than any other guard or minute - three times in total. (In all other cases, any guard spent any minute asleep at most twice.)
|
||||||
|
|
||||||
|
What is the ID of the guard you chose multiplied by the minute you chose? (In the above example, the answer would be 99 * 45 = 4455.)
|
||||||
|
|
||||||
68
AdventOfCode2018/4thDay/challenge2/index.js
Normal file
68
AdventOfCode2018/4thDay/challenge2/index.js
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
class Guard {
|
||||||
|
constructor (id) {
|
||||||
|
this.id = id;
|
||||||
|
this.minutesAsleep = Array.from({ length: 60 }).map(() => 0);
|
||||||
|
this.fellAsleepAt = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
sleep (fellAsleepAt) {
|
||||||
|
this.fellAsleepAt = fellAsleepAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
awake (awokeAt) {
|
||||||
|
for (let i = this.fellAsleepAt; i < awokeAt; i++) {
|
||||||
|
this.minutesAsleep[i] += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
get sleepiestMinute () {
|
||||||
|
return this.minutesAsleep
|
||||||
|
.map((value, index) => ({ index, value }))
|
||||||
|
.sort((a, b) => b.value - a.value)[0].index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const sleep = (input) => {
|
||||||
|
const guards = {};
|
||||||
|
const schedule = input
|
||||||
|
.split('\n')
|
||||||
|
.map((x) => x.trim())
|
||||||
|
.sort()
|
||||||
|
.map((x) => {
|
||||||
|
const parts = x.match(/\[(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})\] (.*)/);
|
||||||
|
|
||||||
|
return {
|
||||||
|
minute: +parts[5],
|
||||||
|
message: parts[6],
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
let currentGuard = null;
|
||||||
|
|
||||||
|
for (let i = 0; i < schedule.length; i++) {
|
||||||
|
const { message, minute } = schedule[i];
|
||||||
|
|
||||||
|
if (/Guard/.test(message)) {
|
||||||
|
const parts = message.match(/Guard #(\d+) begins shift/);
|
||||||
|
const guardId = +parts[1];
|
||||||
|
const guard = guards[guardId] ? guards[guardId] : new Guard(guardId);
|
||||||
|
|
||||||
|
currentGuard = guards[guardId] = guard;
|
||||||
|
} else if (message === 'falls asleep') {
|
||||||
|
currentGuard.sleep(minute);
|
||||||
|
} else if (message === 'wakes up') {
|
||||||
|
currentGuard.awake(minute);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const sleepiestGuard = Object
|
||||||
|
.keys(guards)
|
||||||
|
.map((guardId) => guards[guardId])
|
||||||
|
.sort((a, b) => b.minutesAsleep[b.sleepiestMinute] -
|
||||||
|
a.minutesAsleep[a.sleepiestMinute])[0];
|
||||||
|
|
||||||
|
return sleepiestGuard.id * sleepiestGuard.sleepiestMinute;
|
||||||
|
};
|
||||||
|
|
||||||
|
const fs = require('fs');
|
||||||
|
console.log(sleep(fs.readFileSync("input.txt").toString()));
|
||||||
1000
AdventOfCode2018/4thDay/challenge2/input.txt
Executable file
1000
AdventOfCode2018/4thDay/challenge2/input.txt
Executable file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user