Skip to content

Instantly share code, notes, and snippets.

@HumanEquivalentUnit
Created December 2, 2017 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HumanEquivalentUnit/94ba88140f1b284bd3567654835098f3 to your computer and use it in GitHub Desktop.
Save HumanEquivalentUnit/94ba88140f1b284bd3567654835098f3 to your computer and use it in GitHub Desktop.
2017 Advent of Code - day 2
$in = @'
5 9 2 8
9 4 7 3
3 8 6 5
'@ -split "`r?`n"
$in | ForEach-Object {
# part 1 was easy, split string, measure the max and min, then find the difference
# part 1 # $minmax = -split $_ | Measure-Object -Minimum -Maximum
# part 1 # $minmax.Maximum - $minmax.Minimum
# part 2 was harder, needed a couple of redesigns
# concerned that one input might be a trap and the "evenly divisible" numbers
# might be 1,N or N,N, so didn't go for "work out the divisors" and avoided
# testing numbers against themselves.
# make the test include forwards and backwards check.
$rowNums = -split $_ | ForEach-Object { [int]$_ }
$a, $b = foreach ($i in 0..($rowNums.Length-1)) {
foreach ($j in 0..($rowNums.Length-1)) {
if ($i -ne $j)
{
$x,$y = $rowNums[$i,$j]
if ((($x/$y) -eq [int]($x/$y)) -or (($y/$x) -eq [int]($y/$x)))
{
$x
}
}
}
}
$min, $max = $a, $b | Sort-Object
$max / $min
} | Measure-Object -Sum | Select-Object -ExpandProperty sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment