Day 20 Complete

This commit is contained in:
Brian Buller 2016-12-20 11:05:43 -06:00
parent f8e101a721
commit 204f1d2a15
4 changed files with 1174 additions and 0 deletions

1029
2016/day20/input Normal file

File diff suppressed because it is too large Load Diff

87
2016/day20/main.go Normal file
View File

@ -0,0 +1,87 @@
package main
import (
"fmt"
"math"
"sort"
"strings"
"../../"
)
func main() {
part1 := aoc.ArgIsSet("-1")
justSort := aoc.ArgIsSet("-sort")
input := aoc.StdinToStringSlice()
var blacklists []Blacklist
for i := range input {
pts := strings.Split(input[i], "-")
st := aoc.Atoi(pts[0])
end := aoc.Atoi(pts[1])
blacklists = append(blacklists, *CreateBlacklist(st, end))
}
sort.Sort(ByStart(blacklists))
// Just output the sorted blacklists
if justSort {
for i := range blacklists {
fmt.Print(blacklists[i].start, "-", blacklists[i].end, "\n")
}
return
}
// Find the lowest non-blacklisted IP
if part1 {
fmt.Println(findLowest(blacklists))
return
}
// Count how many IPs are allowed
var numIps, blIdx, ip int
highestIp := int(math.Pow(2, 32))
for ip <= highestIp {
if blIdx >= len(blacklists) {
break
}
if ip >= blacklists[blIdx].start {
if ip <= blacklists[blIdx].end {
ip = blacklists[blIdx].end + 1
continue
}
blIdx++
} else {
numIps++
ip++
}
}
fmt.Println(numIps)
}
func findLowest(blacklists []Blacklist) int {
var lowestIp int
for i := range blacklists {
if blacklists[i].hasIp(lowestIp) {
lowestIp = blacklists[i].end + 1
}
}
return lowestIp
}
type Blacklist struct {
start, end int
}
func CreateBlacklist(st, end int) *Blacklist {
return &Blacklist{st, end}
}
func (b *Blacklist) hasIp(inp int) bool {
return inp >= b.start && inp <= b.end
}
type ByStart []Blacklist
func (b ByStart) Len() int { return len(b) }
func (b ByStart) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b ByStart) Less(i, j int) bool { return b[i].start < b[j].start }

49
2016/day20/problem Normal file
View File

@ -0,0 +1,49 @@
Advent of Code
--- Day 20: Firewall Rules ---
You'd like to set up a small hidden computer here so you can use it to get back into the network later. However, the corporate firewall only allows communication with certain external
IP addresses.
You've retrieved the list of blocked IPs from the firewall, but the list seems to be messy and poorly maintained, and it's not clear which IPs are allowed. Also, rather than being
written in dot-decimal notation, they are written as plain 32-bit integers, which can have any value from 0 through 4294967295, inclusive.
For example, suppose only the values 0 through 9 were valid, and that you retrieved the following blacklist:
5-8
0-2
4-7
The blacklist specifies ranges of IPs (inclusive of both the start and end value) that are not allowed. Then, the only IPs that this firewall allows are 3 and 9, since those are the
only numbers not in any range.
Given the list of blocked IPs you retrieved from the firewall (your puzzle input), what is the lowest-valued IP that is not blocked?
Your puzzle answer was _______________.
--- Part Two ---
How many IPs are allowed by the blacklist?
Your puzzle answer was _______________.
References
Visible links
. http://adventofcode.com/
. http://adventofcode.com/2016/about
. http://adventofcode.com/2016/support
. http://adventofcode.com/2016/events
. http://adventofcode.com/2016/settings
. http://adventofcode.com/2016/auth/logout
. http://adventofcode.com/2016
. http://adventofcode.com/2016
. http://adventofcode.com/2016/leaderboard
. http://adventofcode.com/2016/stats
. http://adventofcode.com/2016/sponsors
. http://adventofcode.com/2016/sponsors
. https://en.wikipedia.org/wiki/IPv4#Addressing
. https://en.wikipedia.org/wiki/Dot-decimal_notation
. https://en.wikipedia.org/wiki/32-bit
. http://adventofcode.com/2016
. http://adventofcode.com/2016/day/20/input

View File

@ -75,6 +75,15 @@ func FileToBytes(fn string) []byte {
return c
}
func PrintProgress(curr, total int) {
pct := int(float64(curr)/float64(total)) * 100
for i := 0; i < 100; i += 10 {
if pct > i {
fmt.Print(FillChar)
}
}
}
// Some character code stuff for prettier output
const (
BorderNS = "\u2502"