Implement day 25, in python

This commit is contained in:
2017-12-25 10:56:51 +01:00
parent 2e95da9bdc
commit fb8e091a34
3 changed files with 120 additions and 5 deletions

View File

@@ -4,7 +4,7 @@ Previously, I have been moderately successful in learning Rust and
Python from doing these challegnes. For this year, I'm upping the ante Python from doing these challegnes. For this year, I'm upping the ante
and using 25 different programming languages. and using 25 different programming languages.
The current plan, in no particular order: These are the languages that I have used, in alphabetical order:
- [x] AWK - [Day 04](./day-04/solution.awk) - [x] AWK - [Day 04](./day-04/solution.awk)
- [x] Bash/shell script - [Day 02](./day-02/solution.sh) - [x] Bash/shell script - [Day 02](./day-02/solution.sh)
@@ -23,14 +23,11 @@ The current plan, in no particular order:
- [x] Lua - [Day 18](./day-18/solution.lua) - [x] Lua - [Day 18](./day-18/solution.lua)
- [x] Matlab - [Day 06](./day-06) - [x] Matlab - [Day 06](./day-06)
- [x] Node.js - [Day 12](./day-12/solution.js) - [x] Node.js - [Day 12](./day-12/solution.js)
- [ ] Objective C
- [x] Perl - [Day 05](./day-05/solution.pl) - [x] Perl - [Day 05](./day-05/solution.pl)
- [x] PHP - [Day 19](./day-19/solution.php) - [x] PHP - [Day 19](./day-19/solution.php)
- [ ] Python - [x] Python - [Day 25](./day-25/solution.py)
- [x] R - [Day 20](./day-20/solution.r) - [x] R - [Day 20](./day-20/solution.r)
- [x] Ruby - [Day 08](./day-08/solution.rb) - [x] Ruby - [Day 08](./day-08/solution.rb)
- [x] Rust - [Day 14](./day-14/solution.rs) - [x] Rust - [Day 14](./day-14/solution.rs)
- [x] Scala - [Day 11](./day-11/solution.scala) - [x] Scala - [Day 11](./day-11/solution.scala)
- [x] Swift - [Day 17](./day-17/solution.swift) - [x] Swift - [Day 17](./day-17/solution.swift)
… and then I will be done!

62
2017/day-25/input.txt Normal file
View File

@@ -0,0 +1,62 @@
Begin in state A.
Perform a diagnostic checksum after 12399302 steps.
In state A:
If the current value is 0:
- Write the value 1.
- Move one slot to the right.
- Continue with state B.
If the current value is 1:
- Write the value 0.
- Move one slot to the right.
- Continue with state C.
In state B:
If the current value is 0:
- Write the value 0.
- Move one slot to the left.
- Continue with state A.
If the current value is 1:
- Write the value 0.
- Move one slot to the right.
- Continue with state D.
In state C:
If the current value is 0:
- Write the value 1.
- Move one slot to the right.
- Continue with state D.
If the current value is 1:
- Write the value 1.
- Move one slot to the right.
- Continue with state A.
In state D:
If the current value is 0:
- Write the value 1.
- Move one slot to the left.
- Continue with state E.
If the current value is 1:
- Write the value 0.
- Move one slot to the left.
- Continue with state D.
In state E:
If the current value is 0:
- Write the value 1.
- Move one slot to the right.
- Continue with state F.
If the current value is 1:
- Write the value 1.
- Move one slot to the left.
- Continue with state B.
In state F:
If the current value is 0:
- Write the value 1.
- Move one slot to the right.
- Continue with state A.
If the current value is 1:
- Write the value 1.
- Move one slot to the right.
- Continue with state E.

56
2017/day-25/solution.py Executable file
View File

@@ -0,0 +1,56 @@
#!/usr/bin/python
import fileinput
import re
def read_action(it):
to_write = 1 if "1" in next(it) else 0
offset = 1 if "right" in next(it) else -1
goto = next(it).strip()[-2]
return (to_write, offset, goto)
def read_input():
in_iter = fileinput.input()
initial = next(in_iter).strip()[-2]
runs = int(next(in_iter).split(" ")[-2])
states = {}
for line in in_iter:
line = line.strip()
if not line:
continue
state = line[-2]
next(in_iter)
if_0 = read_action(in_iter)
next(in_iter)
if_1 = read_action(in_iter)
states[state] = (if_0, if_1)
return initial, runs, states
state, steps, states = read_input()
ones = set()
pos = 0
for _ in range(steps):
if pos in ones:
instr = states[state][1]
else:
instr = states[state][0]
if instr[0] == 1:
ones.add(pos)
else:
ones.discard(pos)
pos += instr[1]
state = instr[2]
print(len(ones))