Last active
December 3, 2021 16:00
-
-
Save RichardSlater/ff6b64460b498665f4bab50c93aafecd to your computer and use it in GitHub Desktop.
Advent of Code 2021
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var depths = File.ReadAllLines(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "input.txt")).Select(x => Int32.Parse(x)); | |
// part 1 | |
depths | |
.Skip(1) | |
.Zip(depths, (curr, prev) => curr > prev) | |
.Count(x => x == true) | |
.Dump("Part 1: Depths larger than previous depth"); | |
// part 2 | |
var depthSlidingWindow = depths | |
.Window(3) | |
.Select(x => x.Sum()); | |
depthSlidingWindow | |
.Skip(1) | |
.Zip(depthSlidingWindow, (curr, prev) => curr > prev) | |
.Count(x => x == true) | |
.Dump("Part 2: Depths larger than previous depth when applying a sliding window"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var commands = File.ReadAllLines(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "input2.txt")).Select(x => x.Split(' ')); | |
// part 1 | |
var c = commands | |
.GroupBy(k => k[0], v => Int32.Parse(v[1])) | |
.ToDictionary(k => k.Key, v => v.Sum()); | |
(c["forward"] * (c["down"] - c["up"])).Dump("Part 1"); | |
// part 2 | |
int aim = 0; | |
int horizontal = 0; | |
int depth = 0; | |
foreach (var cmd in commands.Select(x => new { Instruction = x[0], Parameter = Int32.Parse(x[1])})) { | |
switch(cmd.Instruction) { | |
case "up": | |
aim = aim - cmd.Parameter; break; | |
case "down": | |
aim = aim + cmd.Parameter; break; | |
case "forward": | |
horizontal = horizontal + cmd.Parameter; | |
depth = depth + (aim * cmd.Parameter); | |
break; | |
default: | |
throw new NotImplementedException($"{cmd.Instruction} isn't implemented"); | |
} | |
} | |
(horizontal * depth).Dump("Part 2"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var lines = File.ReadAllLines(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "input.txt")) | |
.Select(x => x.Select(d => byte.Parse(d.ToString())).ToArray()); | |
// Part 1 | |
var bits = new BitArray(Enumerable.Range(0, lines.First().Length) | |
.Select(x => lines.Sum(l => l[x])) | |
.Select(x => x >= lines.Count() / 2 ? true : false) | |
.Reverse() | |
.ToArray() | |
); | |
var gamma = new int[1]; | |
var epsilon = new int[1]; | |
bits.CopyTo(gamma, 0); | |
bits.Not(); | |
bits.CopyTo(epsilon, 0); | |
(gamma[0] * epsilon[0]).Dump("Part 1"); | |
// Part 2 | |
var o2buffer = lines.ToArray(); | |
var o2bit = 0; | |
while (o2buffer.Count() > 1) | |
{ | |
var o2prevelance = o2buffer | |
.GroupBy(x => x[o2bit]); | |
var mostPrevelant = o2prevelance | |
.OrderBy(x => x.Count()) | |
.Last() | |
.Key; | |
if (o2prevelance.First().Count() == o2prevelance.Last().Count()) | |
{ | |
o2buffer = o2buffer.Where(x => x[o2bit] == 1).ToArray(); | |
} | |
else | |
{ | |
o2buffer = o2buffer.Where(x => x[o2bit] == mostPrevelant).ToArray(); | |
} | |
o2bit++; | |
} | |
var o2value = new int[1]; | |
var o2bits = new BitArray(o2buffer.First().Select(x => x == 1).Reverse().ToArray()); | |
o2bits.CopyTo(o2value, 0); | |
var co2buffer = lines.ToArray(); | |
var co2bit = 0; | |
while (co2buffer.Count() > 1) | |
{ | |
var co2prevelance = co2buffer | |
.GroupBy(x => x[co2bit]); | |
var leastPrevelant = co2prevelance | |
.OrderBy(x => x.Count()) | |
.First() | |
.Key; | |
if (co2prevelance.First().Count() == co2prevelance.Last().Count()) | |
{ | |
co2buffer = co2buffer.Where(x => x[co2bit] == 0).ToArray(); | |
} | |
else | |
{ | |
co2buffer = co2buffer.Where(x => x[co2bit] == leastPrevelant).ToArray(); | |
} | |
co2bit++; | |
} | |
var co2value = new int[1]; | |
var co2bits = new BitArray(co2buffer.First().Select(x => x == 1).Reverse().ToArray()); | |
co2bits.CopyTo(co2value, 0); | |
(o2value[0].Dump("O2") * co2value[0].Dump("Co2")).Dump("Part 2"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment