From 2a35384c761630d4301ef7a4b9c7bd2a4b2b2863 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Fri, 22 Dec 2017 16:15:32 +0100 Subject: [PATCH] Solve day 22, in Julia. Only know programming languages left! --- 2017/README.md | 6 ++-- 2017/day-22/input.txt | 25 ++++++++++++++ 2017/day-22/solution.jl | 74 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 2017/day-22/input.txt create mode 100755 2017/day-22/solution.jl diff --git a/2017/README.md b/2017/README.md index c60e3c5..1ac7519 100644 --- a/2017/README.md +++ b/2017/README.md @@ -17,6 +17,7 @@ The current plan, in no particular order: - [x] Groovy - [Day 16](./day-16/solution.groovy) - [x] Haskell - [Day 15](./day-15/solution.hs) - [ ] Java +- [x] Julia - [Day 22](./day-22/solution.jl) - [x] Kotlin - [Day 10](./day-10/solution.kt) - [x] Lex - [Day 09](./day-09/solution.l) - [x] Lua - [Day 18](./day-18/solution.lua) @@ -25,14 +26,11 @@ The current plan, in no particular order: - [ ] Objective C - [x] Perl - [Day 05](./day-05/solution.pl) - [x] PHP - [Day 19](./day-19/solution.php) -- [ ] Prolog - [ ] Python - [x] R - [Day 20](./day-20/solution.r) - [x] Ruby - [Day 08](./day-08/solution.rb) - [x] Rust - [Day 14](./day-14/solution.rs) - [x] Scala - [Day 11](./day-11/solution.scala) -- [ ] Scheme -- [ ] SQL - [x] Swift - [Day 17](./day-17/solution.swift) -… and then I will need some more. But that will come in time. +… and then I will be done! diff --git a/2017/day-22/input.txt b/2017/day-22/input.txt new file mode 100644 index 0000000..b0b7886 --- /dev/null +++ b/2017/day-22/input.txt @@ -0,0 +1,25 @@ +##.###.....##..#.####.... +##...#.#.#..##.#....#.#.. +...#..#.###.#.###.##.#### +..##..###....#.##.#..##.# +###....#####..###.#..#..# +.....#.#...#..##..#.##... +.##.#.###.#.#...##.#.##.# +......######.###......### +#.....##.#....#...#...... +....#..###.#.#.####.##.#. +.#.#.##...###.######.#### +####......#...#...#..#.#. +###.##.##..##....#..##.#. +..#.###.##..#...#######.. +...####.#...###..#..###.# +..#.#.......#.####.#..... +..##..####.######..##.### +..#..#..##...#.####....#. +.#..#.####.#..##..#..##.. +......#####...#.##.#....# +###..#...#.#...#.#..#.#.# +.#.###.#....##..######.## +##.######.....##.#.#.#..# +..#..##.##..#.#..###.##.. +#.##.##..##.#.###.......# diff --git a/2017/day-22/solution.jl b/2017/day-22/solution.jl new file mode 100755 index 0000000..7f6e3ea --- /dev/null +++ b/2017/day-22/solution.jl @@ -0,0 +1,74 @@ +#!/usr/bin/env julia +worldSize = 0 + +infected = Set() + +for line in eachline(STDIN) + for (index, c) in enumerate(line) + coord = index - 1 + worldSize * 1im + if c == '#' + push!(infected, coord) + end + end + + worldSize += 1 +end + +function part1(initial, virusPos) + infected = union(Set(), initial) + virusDir = -1im + infections = 0 + + for i = 1:10000 + if in(virusPos, infected) + virusDir *= 1im + delete!(infected, virusPos) + else + push!(infected, virusPos) + virusDir *= -1im + infections += 1 + end + + virusPos += virusDir + end + + println(infections) +end + +function part2(initial, virusPos) + state = Dict() + virusDir = -1im + + for node in initial + state[node] = 'I' + end + + infections = 0 + + for i = 1:10000000 + s = get(state, virusPos, 'C') + + if s == 'C' + state[virusPos] = 'W' + virusDir *= -1im + elseif s == 'W' + state[virusPos] = 'I' + infections += 1 + elseif s == 'I' + state[virusPos] = 'F' + virusDir *= 1im + else + state[virusPos] = 'C' + virusDir *= -1 + end + + virusPos += virusDir + end + + println(infections) +end + +virusPos = (1 + 1im) * trunc(Int, worldSize / 2) + +part1(infected, virusPos) +part2(infected, virusPos)