r/adventofcode • • Dec 22 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 22 Solutions -๐ŸŽ„-

--- Day 22: Sporifica Virus ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


  • [T-10 to launch] AoC ops, /r/nocontext edition:
  • <Endorphion> You may now make your waffle.
  • <Endorphion> ... on Mars.
  • [Update @ 00:17] 50 gold, silver cap
  • <Aneurysm9> you could also just run ubuntu on the NAS, if you were crazy
  • <Topaz> that doesn't seem necessary
  • <Aneurysm9> what does "necessary" have to do with anything!
  • [Update @ 00:20] Leaderboard cap!
  • <Topaz> POUR YOURSELF A SCOTCH FOR COLOR REFERENCE

This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

8 Upvotes

174 comments sorted by

View all comments

2

u/timblair Dec 22 '17

Go / Golang

Glad to have something less brainache-inducing after yesterday.

Uses a map[node]int to track the infection state, where node is a struct type with x and y positions. Works as a map key because struct values are comparable when all fields of the struct are comparable. Missing values are automatically "clean" due to Go's zero value for ints being 0.

Part 2 below (runs in ~1s):

package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

type node struct {
	x, y int
}

// Travel directions.
const (
	north int = iota
	east
	south
	west
)

// Node states.
const (
	clean int = iota
	weakened
	infected
	flagged
)

func main() {
	lines := [][]string{}

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanLines)
	for scanner.Scan() {
		lines = append(lines, strings.Split(scanner.Text(), ""))
	}

	// Map of node --> infection state, with a reasonable preallocation.
	infections := make(map[node]int, 100000)

	// Initial infection state.
	lw, lh := len(lines[0]), len(lines)
	for i := 0; i < lh; i++ {
		for j := 0; j < lw; j++ {
			if lines[i][j] == "#" {
				n := node{j - lw/2, i - lh/2}
				infections[n] = infected
			}
		}
	}

	// Initial carrier states.
	icnt, pos, dir := 0, node{0, 0}, north

	for i := 0; i < 10000000; i++ {
		// 1. Decide which way to turn based on the current node.
		// 2. Adjust the node state.
		switch infections[pos] {
		case clean:
			dir = (dir + 3) % 4
			infections[pos] = weakened
		case weakened:
			infections[pos] = infected
			icnt++
		case infected:
			dir = (dir + 1) % 4
			infections[pos] = flagged
		case flagged:
			dir = (dir + 2) % 4
			infections[pos] = clean
		}

		// 3. Move the carrier.
		switch dir {
		case north:
			pos = node{pos.x, pos.y - 1}
		case east:
			pos = node{pos.x + 1, pos.y}
		case south:
			pos = node{pos.x, pos.y + 1}
		case west:
			pos = node{pos.x - 1, pos.y}
		}
	}

	fmt.Println(icnt)
}

1

u/cluk Dec 22 '17

I used [2]int arrays as keys instead of structs. In my solution the top left corner of input is point (0,0). Code here.