mirror of
https://github.com/jcreek/advent-of-code.git
synced 2026-07-12 18:53:47 +00:00
2018 Day 02 incomplete solution - committed for posterity in case I ever go back to it
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Day_04
|
||||
{
|
||||
public class Action
|
||||
{
|
||||
public string Date { get; set; }
|
||||
public string Time { get; set; }
|
||||
public string Description { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
||||
<RootNamespace>Day_04</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Day_04
|
||||
{
|
||||
public class Day
|
||||
{
|
||||
public string Date { get; set; }
|
||||
public int GuardId { get; set; }
|
||||
public List<bool> IsAsleepThisMinute { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Day_04
|
||||
{
|
||||
public class Guard
|
||||
{
|
||||
public int GuardId { get; set; }
|
||||
public int TotalMinutesAsleep { get; set; }
|
||||
public List<int> TimesSleptPerMinute { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Day_04
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
// Find the guard that has the most minutes asleep.
|
||||
// What minute does that guard spend asleep the most?
|
||||
|
||||
|
||||
|
||||
// Make an empty list of guards
|
||||
List<Guard> guards = new List<Guard>();
|
||||
|
||||
// Take in an unordered list of times and dates when guards start shifts, fall asleep and wake up
|
||||
var lines = File.ReadAllLines("input.txt");
|
||||
|
||||
// Sort that into an ordered list, by date then time
|
||||
List<Action> actions = new List<Action>();
|
||||
foreach (var line in lines)
|
||||
{
|
||||
Action action = new Action();
|
||||
|
||||
Regex re1 = new Regex(@"((\d\d\d\d-\d\d-\d\d))");
|
||||
var dateString = re1.Matches(line)[0].Value;
|
||||
action.Date = dateString.Substring(0, dateString.Length);
|
||||
|
||||
Regex re2 = new Regex(@"((\d\d:\d\d))");
|
||||
var timeString = re2.Matches(line)[0].Value;
|
||||
action.Time = timeString.Substring(0, timeString.Length);
|
||||
|
||||
Regex re3 = new Regex(@"(\] (.*))");
|
||||
Match match3 = re3.Match(line);
|
||||
action.Description = match3.Groups[2].Value;
|
||||
|
||||
actions.Add(action);
|
||||
}
|
||||
actions = actions.OrderBy(a => a.Date).ThenBy(a => a.Time).ToList();
|
||||
|
||||
// Use the ordered list to create a list of days with the guard id and a breakdown of minutes slept and awake
|
||||
List<Day> days = new List<Day>();
|
||||
foreach (var action in actions)
|
||||
{
|
||||
Day day = new Day();
|
||||
if(days.Any(d => d.Date == action.Date))
|
||||
{
|
||||
day = days.First
|
||||
}
|
||||
|
||||
day.Date = action.Date;
|
||||
|
||||
Regex re1 = new Regex(@"(#(\d+))");
|
||||
Match match1 = re1.Match(action.Description);
|
||||
if (match1.Success)
|
||||
{
|
||||
day.GuardId = Int32.Parse(match1.Groups[2].Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Use the ID from a previous action on the same day
|
||||
day.GuardId = days.First(d => d.Date == day.Date).GuardId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Date
|
||||
// GuardId
|
||||
// IsAsleepThisMinute
|
||||
}
|
||||
|
||||
// Use the list of days to populate the list of guards
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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.)
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user